FRClient.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // FRClient.cc
00030 // FukuiRenderer Client
00031 // Yasuhide Sawada & Satoshi Tanaka
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         /* stream socket */
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         // local variables
00085         struct sockaddr_in sa;
00086         struct hostent *hp;
00087 
00088         // set port ( sa.sin_family,  sa.sin_port )
00089         port = port_;  // Store port number to data member
00090         memset( (char *)&sa, '\0', sizeof(sa)) ;
00091         sa.sin_family = AF_INET;
00092         sa.sin_port = htons(port);
00093 
00094         // set server host ( sa.sin_addr )
00095         if (hostname == NULL) {
00096                 hostname = "localhost"; 
00097                         // reset arg 
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         // make connection to server
00109         if (::connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
00110                 fputs("error: connect\n", stderr);
00111                 return -1;
00112         }
00113 
00114         // return file descripter (data member)
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                 shutdown :argument '2' means shutdown both send and receive.
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

Generated on Mon May 27 17:47:35 2013 for Geant4 by  doxygen 1.4.7