Hi
I developed a client-server application.
//**************** client(UNIX) ****************
// connect with timeout:
// 1. set socket NONBLOCKING
// 2. connect with timeout using select
// 3. set socket to BLOCKING againg
int my_connect(int sockremote, long sock_addr, short port, int
timeout)
{
int res;
struct sockaddr_in addr;
long arg;
fd_set myset;
struct timeval tv;
int valopt;
socklen_t lon;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = sock_addr;
// Set non-blocking
if( (arg = fcntl(sockremote, F_GETFL, NULL)) < 0)
{
sprintf(error, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno));
record_err(error);
child_exit(1);
}
arg |= O_NONBLOCK;
if( fcntl(sockremote, F_SETFL, arg) < 0)
{
sprintf(error, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno));
record_err(error);
child_exit(1);
}
// Trying to connect with timeout
res = connect(sockremote, (struct sockaddr *)&addr, sizeof(addr));
if (res < 0)
{
if ((errno == EINPROGRESS) || (errno == EWOULDBLOCK))
{
//-------------------------------------
//EINPROGRESS in connect() - selecting
//-------------------------------------
// printf("Error in first connect: %s\n", strerror(errno));
do
{
tv.tv_sec = (long) (timeout/1000000);
tv.tv_usec = (long) (timeout%1000000);
FD_ZERO(&myset);
FD_SET(sockremote, &myset);
res = select(sockremote + 1, NULL, &myset, NULL, &tv);
if (res < 0 && errno != EINTR)
{
//------------------------
// err conecting
//------------------------
printf("Error connecting!\n");
return -2;
}
else
if ((res == 1) && FD_ISSET(sockremote, &myset))
{
//------------------------------
// Socket selected for write
//------------------------------
lon = sizeof(int);
if (getsockopt(sockremote, SOL_SOCKET, SO_ERROR,
(void*)(&valopt), &lon) < 0)
{
//-----------------------------
// err in getsockopt
//-----------------------------
printf("Error in getsockopt\n");
return -2;
}
// Check the value returned...
if (valopt)
{
//---------------------------------------
// err in delayed connection()
//---------------------------------------
printf("Error in delayed connection: %s ; valopt = %d\n",
strerror(valopt), valopt);
return -1;
}
break;
}
else
{
//------------------------------------
//Timeout in select() - Cancelling!
//------------------------------------
// printf("Timeout in select!\n");
return -1;
}
}
while (1);
}
else
{
if(res != 0)
{
//-----------------------
// err connecting
//-----------------------
return -2;
}
}
}
// Set to blocking mode again...
if( (arg = fcntl(sockremote, F_GETFL, NULL)) < 0)
{
sprintf(error, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno));
record_err(error);
child_exit(1);
}
arg &= (~O_NONBLOCK);
if( fcntl(sockremote, F_SETFL, arg) < 0)
{
sprintf(error, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno));
record_err(error);
child_exit(1);
}
printf("CONNECTED!! - ");
// Connected OK!!
return 0;
}
//****************** SERVER (windows) ***************
//with recv & send timeout = 10 s
SOCKET establish(int portnum)
{
SOCKET new_socket;
struct sockaddr_in sa;
sa.sin_family = AF_INET; /* this is our host address */
sa.sin_port = htons(portnum); /* this is our port number */
sa.sin_addr.s_addr = htonl(INADDR_ANY);
//*******************************
//create socket (new "telephone")
//*******************************
new_socket = socket(AF_INET, SOCK_STREAM, 0);
if (new_socket == INVALID_SOCKET)
{
#ifdef MYDEBUG
fprintf(fp_debug, "Socket could not be created\n");
fflush(fp_debug);
#endif
return INVALID_SOCKET;
}
#ifdef MYDEBUG
fprintf(fp_debug, "Socket created\n");
fflush(fp_debug);
#endif
//reuse addr
setsockopt(new_socket, SOL_SOCKET, SO_REUSEADDR, "true", 5);
//set timeouts
int rcv_timeout_ms = 10000;
int snd_timeout_ms = 10000;
setsockopt(new_socket, SOL_SOCKET, SO_RCVTIMEO,
(LPCSTR)&rcv_timeout_ms, sizeof(int));
setsockopt(new_socket, SOL_SOCKET, SO_SNDTIMEO,
(LPCSTR)&snd_timeout_ms, sizeof(int));
//************************************************** **********************************
//bind the socket to the internet address (assign telephone number to
new "telephone")
//************************************************** ***********************************
if (bind(new_socket, (struct sockaddr *)&sa, sizeof(struct
sockaddr_in)) == SOCKET_ERROR)
{
#ifdef MYDEBUG
fprintf(fp_debug, "Could not bind socket to the internet address\n");
fflush(fp_debug);
#endif
closesocket(new_socket);
return(INVALID_SOCKET);
}
//************************************************** ****************************
//listen to the socket (turn on the ringer so that you can hear the
phone call)
//************************************************** ****************************
listen(new_socket, 20); /* max # of queued connects */
return(new_socket);
}
main()
{
................
for (;
/* loop for phone calls */
{
#ifdef MYDEBUG
fprintf(fp_debug, "Waiting for calls .... \n");
fflush(fp_debug);
#endif
SOCKET client_socket = accept(server_socket, NULL, NULL);
if (client_socket == INVALID_SOCKET)
{
#ifdef MYDEBUG
fprintf(fp_debug, "Error waiting for new connection\n");
fflush(fp_debug);
#endif
exit(1);
}
#ifdef MYDEBUG
fprintf(fp_debug, "Accepting one call ... \n");
fflush(fp_debug);
#endif
do_something(client_socket);
closesocket(client_socket);
}
}
//************************************************** ***************************************
when the client tryes to connect get a lot of "Error in delayed
connection: connection refused.
WHY ?!
10x.