| 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,v 1.7 2006/06/29 21:25:49 gunter Exp $
|
|---|
| 28 | // GEANT4 tag $Name: $
|
|---|
| 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 |
|
|---|
| 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 ) {
|
|---|
| 101 | G4cerr << "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
|
|---|