28 # define _CRT_SECURE_NO_DEPRECATE // vs8
29 # pragma warning (disable: 4251) // STL MAP DLL warning noise (JW)
32 #include "CScommClient.hh"
38 # include <sys/poll.h>
39 # include <sys/socket.h>
40 # include <netinet/in.h>
43 # include <netinet/tcp.h>
50 #include <Base/Debug/Error.hh>
55 CScommClient::CScommClient(
bool threaded) :
CScommBase(threaded)
59 if (gethostname(hostName,255)==0)
66 struct hostent *roothostent;
67 struct sockaddr_in control_addr;
68 unsigned int counter = 0;
71 roothostent = gethostbyname(serverName.c_str());
72 if (roothostent == NULL) {
73 BIASERR(
"Host not found: "<<serverName);
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);
83 socketFd = (int)socket(AF_INET, SOCK_STREAM, 0);
85 bool * flag =
new bool(
true);
86 int length =
sizeof(bool);
87 setsockopt(socketFd, IPPROTO_TCP, TCP_NODELAY, (
char *) flag, length);
90 logOut_<<
"Trying to connect with server "<<serverName<<endl;
93 while ( (res<0) && (counter<(timeOut/100)) ) {
94 control_addr.sin_port = htons((u_short) connectPort_);
96 logOut_<<
"Trying to connect with server "<<serverName<<
" on port "<<connectPort_<<endl;
97 res=connect(socketFd,(
struct sockaddr *)&control_addr,
98 sizeof(control_addr));
102 if ((counter % 10) ==0) connectPort_=D_CS_DEFAULT_PORT;
110 if (counter>=(timeOut/100)) {
111 BIASERR(
"Time out connecting to server "<<serverName);
118 BIASERR(
"Error creating receive thread for client "<<
name_);
123 verboseOut_<<
"Successfully connected to server "<<serverName<<
" on port "<<connectPort_<<endl;
127 sprintf(firstMsg,
"[CLIENTNAME] %s",
name_.c_str());
128 BIASDOUT(D_CS_CONNECT,
"sending \""<<firstMsg<<
"\"");
130 res = send(
commPartners_[res].fd, firstMsg, (
int)strlen(firstMsg)+1, 0);
133 res = send(
commPartners_[res].fd, &firstMsg, strlen(firstMsg)+1, 0);
135 res = send(
commPartners_[res].fd, &firstMsg, strlen(firstMsg)+1, MSG_NOSIGNAL);
139 BIASERR(
"Failed to send client name to server ('"<<strerror(errno)<<
"')!");
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_
this class CScomm (ClientServer communciation) handles data comunication via TCP/IP for one server an...