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