[834] | 1 | // |
---|
| 2 | // ******************************************************************** |
---|
| 3 | // * License and Disclaimer * |
---|
| 4 | // * * |
---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
| 7 | // * conditions of the Geant4 Software License, included in the file * |
---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
| 9 | // * include a list of copyright holders. * |
---|
| 10 | // * * |
---|
| 11 | // * Neither the authors of this software system, nor their employing * |
---|
| 12 | // * institutes,nor the agencies providing financial support for this * |
---|
| 13 | // * work make any representation or warranty, express or implied, * |
---|
| 14 | // * regarding this software system or assume any liability for its * |
---|
| 15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
| 16 | // * for the full disclaimer and the limitation of liability. * |
---|
| 17 | // * * |
---|
| 18 | // * This code implementation is the result of the scientific and * |
---|
| 19 | // * technical work of the GEANT4 collaboration. * |
---|
| 20 | // * By using, copying, modifying or distributing the software (or * |
---|
| 21 | // * any work based on the software) you agree to acknowledge its * |
---|
| 22 | // * use in resulting scientific publications, and indicate your * |
---|
| 23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
| 24 | // ******************************************************************** |
---|
| 25 | // |
---|
| 26 | // |
---|
[1347] | 27 | // $Id: FRClient.cc,v 1.8 2010/11/11 00:14:50 akimura Exp $ |
---|
| 28 | // GEANT4 tag $Name: geant4-09-04-ref-00 $ |
---|
[834] | 29 | // |
---|
| 30 | // FRClient.cc |
---|
| 31 | // FukuiRenderer Client |
---|
| 32 | // Yasuhide Sawada & Satoshi Tanaka |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | //=================// |
---|
| 36 | #ifndef WIN32 |
---|
| 37 | //=================// |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | //=================// |
---|
| 41 | #ifdef G4VIS_BUILD_VRML_DRIVER |
---|
| 42 | //=================// |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | #include "G4ios.hh" |
---|
| 46 | #include <sys/time.h> |
---|
| 47 | #include <sys/types.h> |
---|
| 48 | #include <sys/socket.h> |
---|
| 49 | #include <netinet/in.h> |
---|
| 50 | #include <arpa/inet.h> |
---|
| 51 | #include <netdb.h> |
---|
| 52 | #include <fcntl.h> |
---|
| 53 | |
---|
| 54 | #include <unistd.h> |
---|
| 55 | #include <string.h> |
---|
| 56 | #include <stdio.h> |
---|
| 57 | |
---|
[1347] | 58 | #include "G4VisManager.hh" |
---|
[834] | 59 | #include "FRClient.h" |
---|
| 60 | |
---|
| 61 | FRClient::FRClient() |
---|
| 62 | { |
---|
| 63 | fd = -1; |
---|
| 64 | create(); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | FRClient::~FRClient() |
---|
| 68 | { |
---|
| 69 | close(); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | int FRClient::create() |
---|
| 73 | { |
---|
| 74 | /* stream socket */ |
---|
| 75 | fd = socket(AF_INET, SOCK_STREAM, 0); |
---|
| 76 | if (fd < 0) |
---|
| 77 | fputs("error: socket.\n", stderr); |
---|
| 78 | return fd; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | int FRClient::connect(const char *hostname, int port_) |
---|
| 84 | { |
---|
| 85 | // local variables |
---|
| 86 | struct sockaddr_in sa; |
---|
| 87 | struct hostent *hp; |
---|
| 88 | |
---|
| 89 | // set port ( sa.sin_family, sa.sin_port ) |
---|
| 90 | port = port_; // Store port number to data member |
---|
| 91 | memset( (char *)&sa, '\0', sizeof(sa)) ; |
---|
| 92 | sa.sin_family = AF_INET; |
---|
| 93 | sa.sin_port = htons(port); |
---|
| 94 | |
---|
| 95 | // set server host ( sa.sin_addr ) |
---|
| 96 | if (hostname == NULL) { |
---|
| 97 | hostname = "localhost"; |
---|
| 98 | // reset arg |
---|
| 99 | } |
---|
| 100 | hp = gethostbyname(hostname) ; |
---|
| 101 | if ( !hp ) { |
---|
[1347] | 102 | if (G4VisManager::GetVerbosity() >= G4VisManager::errors) |
---|
| 103 | G4cout << "ERROR: gethostbyname() failed" << G4endl; |
---|
| 104 | return -1; |
---|
[834] | 105 | } |
---|
| 106 | |
---|
| 107 | memcpy( (char * )&sa.sin_addr, (char * )hp->h_addr, hp->h_length ); |
---|
| 108 | |
---|
| 109 | // make connection to server |
---|
| 110 | if (::connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) { |
---|
| 111 | fputs("error: connect\n", stderr); |
---|
| 112 | return -1; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | // return file descripter (data member) |
---|
| 116 | return fd; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | int FRClient::send(const char *sendbuf) |
---|
| 122 | { |
---|
| 123 | int len = strlen(sendbuf); |
---|
| 124 | |
---|
| 125 | if (::send(fd, sendbuf, len, 0) < 0) { |
---|
| 126 | fputs("error: Send()\n", stderr); |
---|
| 127 | len = -1; |
---|
| 128 | } |
---|
| 129 | return len; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | int FRClient::receive(char *recvbuf) |
---|
| 133 | { |
---|
| 134 | int len; |
---|
| 135 | |
---|
| 136 | memset(recvbuf, '\0', FRSendLength + 1); |
---|
| 137 | len = ::recv(fd, recvbuf, FRSendLength, 0); |
---|
| 138 | if(len < 0) { |
---|
| 139 | fputs("error: Receive()\n", stderr); |
---|
| 140 | len = -1; |
---|
| 141 | } |
---|
| 142 | return len; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | int FRClient::close() |
---|
| 146 | { |
---|
| 147 | /* |
---|
| 148 | shutdown :argument '2' means shutdown both send and receive. |
---|
| 149 | */ |
---|
| 150 | if (::shutdown(fd, 2) < 0) { |
---|
| 151 | fputs("error: shutdown\n", stderr); |
---|
| 152 | return -1; |
---|
| 153 | } |
---|
| 154 | ::close(fd); |
---|
| 155 | return 0; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | #endif //G4VIS_BUILD_VRML_DRIVER |
---|
| 159 | |
---|
| 160 | #endif // WIN32 |
---|