ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FRClient.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FRClient.cc
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 //
27 //
28 // FRClient.cc
29 // FukuiRenderer Client
30 // Yasuhide Sawada & Satoshi Tanaka
31 
32 
33 //=================//
34 #ifndef WIN32
35 //=================//
36 
37 
38 //=================//
39 #ifdef G4VIS_BUILD_VRML_DRIVER
40 //=================//
41 
42 
43 #include "G4ios.hh"
44 #include <sys/time.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <netdb.h>
50 #include <fcntl.h>
51 
52 #include <unistd.h>
53 #include <string.h>
54 #include <stdio.h>
55 
56 #include "G4VisManager.hh"
57 #include "FRClient.h"
58 
59 FRClient::FRClient()
60 {
61  fd = -1;
62  create();
63 }
64 
65 FRClient::~FRClient()
66 {
67  close();
68 }
69 
70 int FRClient::create()
71 {
72  /* stream socket */
73  fd = socket(AF_INET, SOCK_STREAM, 0);
74  if (fd < 0)
75  fputs("error: socket.\n", stderr);
76  return fd;
77 }
78 
79 
80 
81 int FRClient::connect(const char *hostname, int port_)
82 {
83  // local variables
84  struct sockaddr_in sa;
85  struct hostent *hp;
86 
87  // set port ( sa.sin_family, sa.sin_port )
88  port = port_; // Store port number to data member
89  memset( (char *)&sa, '\0', sizeof(sa)) ;
90  sa.sin_family = AF_INET;
91  sa.sin_port = htons(port);
92 
93  // set server host ( sa.sin_addr )
94  if (hostname == NULL) {
95  hostname = "localhost";
96  // reset arg
97  }
98  hp = gethostbyname(hostname) ;
99  if ( !hp ) {
101  G4cout << "ERROR: gethostbyname() failed" << G4endl;
102  return -1;
103  }
104 
105  memcpy( (char * )&sa.sin_addr, (char * )hp->h_addr, hp->h_length );
106 
107  // make connection to server
108  if (::connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
109  fputs("error: connect\n", stderr);
110  return -1;
111  }
112 
113  // return file descripter (data member)
114  return fd;
115 }
116 
117 
118 
119 int FRClient::send(const char *sendbuf)
120 {
121  int len = strlen(sendbuf);
122 
123  if (::send(fd, sendbuf, len, 0) < 0) {
124  fputs("error: Send()\n", stderr);
125  len = -1;
126  }
127  return len;
128 }
129 
130 int FRClient::receive(char *recvbuf)
131 {
132  int len;
133 
134  memset(recvbuf, '\0', FRSendLength + 1);
135  len = ::recv(fd, recvbuf, FRSendLength, 0);
136  if(len < 0) {
137  fputs("error: Receive()\n", stderr);
138  len = -1;
139  }
140  return len;
141 }
142 
143 int FRClient::close()
144 {
145  /*
146  shutdown :argument '2' means shutdown both send and receive.
147  */
148  if (::shutdown(fd, 2) < 0) {
149  fputs("error: shutdown\n", stderr);
150  return -1;
151  }
152  ::close(fd);
153  return 0;
154 }
155 
156 #endif //G4VIS_BUILD_VRML_DRIVER
157 
158 #endif // WIN32