13 #if defined (_WIN32) && !defined (__CYGWIN__)
14 static bool first_time =
true;
18 COUT<<
"Initializing Winsock"<<std::endl;
19 WORD wVersionRequested = MAKEWORD( 1, 1 );
21 int res = WSAStartup( wVersionRequested, &wsaData );
23 COUT<<
"Error initializing Winsock"<<std::endl;
33 memset( &remote_ip, 0,
sizeof (
AddressIP) );
35 struct hostent *he = NULL;
37 if (host[0] < 48 || host[0] > 57) {
38 COUT<<
"Resolving host name... ";
39 if ( ( he = gethostbyname( host ) ) == NULL ) {
40 cerr<<
"Could not resolve hostname"<<std::endl;
43 memcpy( &remote_ip.sin_addr.s_addr, he->h_addr_list[0], he->h_length );
44 cerr<<
"found : "<<inet_ntoa( remote_ip.sin_addr )<<std::endl;
46 COUT<<
"Error inet_aton"<<std::endl;
50 remote_ip.sin_port = htons( port );
51 remote_ip.sin_family = AF_INET;
62 return bind( fd, (sockaddr*) &remote_ip,
sizeof (
struct sockaddr_in) ) !=
SOCKET_ERROR;
70 if ( ( local_fd =
VsnetOSS::socket( PF_INET, isTCP ? SOCK_STREAM : SOCK_DGRAM, 0 ) ) == -1 ) {
71 COUT<<
"Could not create socket"<<std::endl;
75 COUT<<
"Connecting to "<<inet_ntoa( remote_ip.sin_addr )<<
" on port "<<ntohs( remote_ip.sin_port )<<std::endl;
83 #if defined (_WIN32) && !defined (__CYGWIN__)
84 if (::connect( local_fd, (sockaddr*) &remote_ip,
sizeof (
struct sockaddr) ) ==
SOCKET_ERROR)
86 if (::connect( local_fd, (sockaddr*) &remote_ip,
sizeof (
struct sockaddr) ) < 0)
89 #if defined (_WIN32) && !defined (__CYGWIN__)
90 int lasterr = WSAGetLastError();
91 if (lasterr != WSAEINPROGRESS && lasterr != WSAEWOULDBLOCK)
93 if (errno != EINPROGRESS && errno != EWOULDBLOCK)
96 perror(
"Can't connect to server " );
103 if ( !
bindFd( local_fd, remote_ip ) ) {
104 perror(
"Can't bind socket" );
119 COUT<<
"Could not create socket"<<std::endl;
123 char *buf = (
char*) &newval;
124 setsockopt( local_fd, SOL_SOCKET, SO_REUSEADDR, buf,
sizeof (
int) );
127 COUT<<
"Bind on "<<ntohl( local_ip.sin_addr.s_addr )<<
", port "
128 <<ntohs( local_ip.sin_port )<<std::endl;
129 if (bind( local_fd, (sockaddr*) &local_ip,
sizeof (
struct sockaddr_in) ) ==
SOCKET_ERROR) {
130 perror(
"Problem binding socket" );
134 COUT<<
"Accepting max : "<<SOMAXCONN<<std::endl;
136 perror(
"Problem listening on socket" );
155 COUT<<
"enter "<<__PRETTY_FUNCTION__<<std::endl;
160 if (remote_ip.sin_port == 0) {
165 if (local_fd == -1) {
169 COUT<<
"Connected to "<<inet_ntoa( remote_ip.sin_addr )<<
":"<<srv_port<<std::endl;
171 SOCKETALT ret( local_fd, SOCKETALT::TCP, remote_ip, set );
173 COUT<<
"WARNING: TCP client socket may be in blocking mode"<<std::endl;
174 COUT<<
"SOCKETALT FD: "<<ret.
get_fd()<<std::endl;
180 COUT<<
"enter "<<__PRETTY_FUNCTION__<<std::endl;
184 if (local_ip.sin_port == 0)
189 COUT<<
"Listening on socket "<<local_fd<<std::endl
190 <<
"*** ServerSocket FD : "<<local_fd<<std::endl;
208 COUT<<
" enter "<<__PRETTY_FUNCTION__<<std::endl;
216 UDP_pool_type::iterator iter(
UDP_pool.find( local_ip ) );
225 if (local_fd == -1) {
229 SOCKETALT ret( local_fd, SOCKETALT::UDP, remote_ip, set );
232 COUT<<
"Could not set socket to nonblocking state";
236 COUT<<
"Bind on localhost, "<<ret<<std::endl;
242 COUT<<
"enter "<<__PRETTY_FUNCTION__<<std::endl;
248 if (local_fd == -1) {
252 SOCKETALT ret( local_fd, SOCKETALT::UDP, local_ip, set );
255 COUT<<
"Setting server socket mode to nonblocking failed";
260 COUT<<
"Bind on localhost, "<<ret<<std::endl;