Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CScommClient.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 //#include <Base/Common/W32Compat.hh>
26 
27 #ifdef WIN32
28 # define _CRT_SECURE_NO_DEPRECATE // vs8
29 # pragma warning (disable: 4251) // STL MAP DLL warning noise (JW)
30 #endif
31 
32 #include "CScommClient.hh"
33 
34 #ifdef WIN32
35 # include <winsock.h>
36 //# include <winsock2.h>
37 #else //WIN32
38 # include <sys/poll.h>
39 # include <sys/socket.h>
40 # include <netinet/in.h>
41 # include <netdb.h>
42 # include <unistd.h>
43 # include <netinet/tcp.h>
44 #endif //WIN32
45 
46 #include <errno.h>
47 #include <cstdio>
48 #include <cstring>
49 
50 #include <Base/Debug/Error.hh>
51 
52 using namespace std;
53 using namespace BIAS;
54 
55 CScommClient::CScommClient(bool threaded) : CScommBase(threaded)
56 {
57  isServer_=false;
58  char hostName[255];
59  if (gethostname(hostName,255)==0)
60  name_=hostName;
61 }
62 
63 int CScommClient::ConnectServer(string serverName, unsigned int port,
64  unsigned int timeOut)
65 {
66  struct hostent *roothostent;
67  struct sockaddr_in control_addr;
68  unsigned int counter = 0;
69  int socketFd;
70 
71  roothostent = gethostbyname(serverName.c_str());
72  if (roothostent == NULL) {
73  BIASERR("Host not found: "<<serverName);
74  return -2;
75  }
76  connectPort_=port;
77  memset(&control_addr,0 , sizeof(struct sockaddr_in));
78  control_addr.sin_family = AF_INET;
79  memcpy((char *) &control_addr.sin_addr.s_addr,
80  (char *) roothostent->h_addr,
81  roothostent->h_length);
82  control_addr.sin_port = htons((u_short) connectPort_);
83  socketFd = (int)socket(AF_INET, SOCK_STREAM, 0);
84 
85  bool * flag = new bool(true);
86  int length = sizeof(bool);
87  setsockopt(socketFd, IPPROTO_TCP, TCP_NODELAY, (char *) flag, length);
88 
89  if (verbose_)
90  logOut_<<"Trying to connect with server "<<serverName<<endl;
91 
92  int res=-1;
93  while ( (res<0) && (counter<(timeOut/100)) ) {
94  control_addr.sin_port = htons((u_short) connectPort_);
95  if (log_)
96  logOut_<<"Trying to connect with server "<<serverName<<" on port "<<connectPort_<<endl;
97  res=connect(socketFd,(struct sockaddr *)&control_addr,
98  sizeof(control_addr));
99  counter++;
100  if ( (res!=0) && portChange_ ) {
101  connectPort_++;
102  if ((counter % 10) ==0) connectPort_=D_CS_DEFAULT_PORT;
103  }
104 #ifdef WIN32
105  Sleep(1);
106 #else
107  usleep (100);
108 #endif
109  }
110  if (counter>=(timeOut/100)) {
111  BIASERR("Time out connecting to server "<<serverName);
112  return -1;
113  }
114 
115  res = AcceptConn_(socketFd);
116  // all channels are busy
117  if (res <0) {
118  BIASERR("Error creating receive thread for client "<<name_);
119  return -4;
120  }
121  commPartners_[res].name = serverName;
122  if (verbose_)
123  verboseOut_<<"Successfully connected to server "<<serverName<<" on port "<<connectPort_<<endl;
124 
125  //// send client name
126  char firstMsg[255];
127  sprintf(firstMsg, "[CLIENTNAME] %s",name_.c_str());
128  BIASDOUT(D_CS_CONNECT, "sending \""<<firstMsg<<"\"");
129 #ifdef WIN32
130  res = send(commPartners_[res].fd, firstMsg, (int)strlen(firstMsg)+1, 0);
131 #else
132 # ifdef __APPLE__
133  res = send(commPartners_[res].fd, &firstMsg, strlen(firstMsg)+1, 0);
134 # else
135  res = send(commPartners_[res].fd, &firstMsg, strlen(firstMsg)+1, MSG_NOSIGNAL);
136 # endif
137 #endif
138  if (res <= 0) {
139  BIASERR("Failed to send client name to server ('"<<strerror(errno)<<"')!");
140  return -3;
141  } else if (verbose_) {
142  verboseOut_<<"Send client name to server"<<endl;
143  }
144  return 0;
145 }
int ConnectServer(std::string serverName, unsigned int port=D_CS_DEFAULT_PORT, unsigned int timeOut=20000)
for a client: tries to connect to server with hostname serverName.
std::vector< CScommData > commPartners_
Definition: CScommBase.hh:275
this class CScomm (ClientServer communciation) handles data comunication via TCP/IP for one server an...
Definition: CScommBase.hh:73
std::ostream logOut_
Definition: CScommBase.hh:289
std::ostream verboseOut_
Definition: CScommBase.hh:291
std::string name_
Definition: CScommBase.hh:293
int AcceptConn_(int fd)
Definition: CScommBase.cpp:968