00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef WIN32
00036
00037
00038
00039
00040 #ifdef G4VIS_BUILD_VRML_DRIVER
00041
00042
00043
00044 #include "G4ios.hh"
00045 #include <sys/time.h>
00046 #include <sys/types.h>
00047 #include <sys/socket.h>
00048 #include <netinet/in.h>
00049 #include <arpa/inet.h>
00050 #include <netdb.h>
00051 #include <fcntl.h>
00052
00053 #include <unistd.h>
00054 #include <string.h>
00055 #include <stdio.h>
00056
00057 #include "G4VisManager.hh"
00058 #include "FRClient.h"
00059
00060 FRClient::FRClient()
00061 {
00062 fd = -1;
00063 create();
00064 }
00065
00066 FRClient::~FRClient()
00067 {
00068 close();
00069 }
00070
00071 int FRClient::create()
00072 {
00073
00074 fd = socket(AF_INET, SOCK_STREAM, 0);
00075 if (fd < 0)
00076 fputs("error: socket.\n", stderr);
00077 return fd;
00078 }
00079
00080
00081
00082 int FRClient::connect(const char *hostname, int port_)
00083 {
00084
00085 struct sockaddr_in sa;
00086 struct hostent *hp;
00087
00088
00089 port = port_;
00090 memset( (char *)&sa, '\0', sizeof(sa)) ;
00091 sa.sin_family = AF_INET;
00092 sa.sin_port = htons(port);
00093
00094
00095 if (hostname == NULL) {
00096 hostname = "localhost";
00097
00098 }
00099 hp = gethostbyname(hostname) ;
00100 if ( !hp ) {
00101 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00102 G4cout << "ERROR: gethostbyname() failed" << G4endl;
00103 return -1;
00104 }
00105
00106 memcpy( (char * )&sa.sin_addr, (char * )hp->h_addr, hp->h_length );
00107
00108
00109 if (::connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
00110 fputs("error: connect\n", stderr);
00111 return -1;
00112 }
00113
00114
00115 return fd;
00116 }
00117
00118
00119
00120 int FRClient::send(const char *sendbuf)
00121 {
00122 int len = strlen(sendbuf);
00123
00124 if (::send(fd, sendbuf, len, 0) < 0) {
00125 fputs("error: Send()\n", stderr);
00126 len = -1;
00127 }
00128 return len;
00129 }
00130
00131 int FRClient::receive(char *recvbuf)
00132 {
00133 int len;
00134
00135 memset(recvbuf, '\0', FRSendLength + 1);
00136 len = ::recv(fd, recvbuf, FRSendLength, 0);
00137 if(len < 0) {
00138 fputs("error: Receive()\n", stderr);
00139 len = -1;
00140 }
00141 return len;
00142 }
00143
00144 int FRClient::close()
00145 {
00146
00147
00148
00149 if (::shutdown(fd, 2) < 0) {
00150 fputs("error: shutdown\n", stderr);
00151 return -1;
00152 }
00153 ::close(fd);
00154 return 0;
00155 }
00156
00157 #endif //G4VIS_BUILD_VRML_DRIVER
00158
00159 #endif // WIN32