Tech Support > Computers & Technology > Programming > Windows server, Unix client, connection refused on client connect.
Windows server, Unix client, connection refused on client connect.
Posted by Dan C on September 23rd, 2003


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.

Posted by Wiseguy on September 24th, 2003


cristea_dan@mymail.ro (Dan C) tried to express:
I think you have things somewhat backwards. The correct scheme should be:

UNIX server
Windows client

;^P

-Wiseguy


Posted by David on September 24th, 2003


On Tue, 23 Sep 2003 08:25:58 UTC, cristea_dan@mymail.ro (Dan C) wrote:

It seems that the server isn't responding in the time allotted.
You may be able to solve the problem by lengthening the client
timeout. It would also be prudent to figure out why the server
takes so long to establish a connection. I am presuming that
client-server are reasonably close together and not located
on different planets.

What does the server side have to say for itself when this problem
occurs? What do the traces show?

David

Posted by Dan C on September 25th, 2003


"David" <FlyLikeAnEagle@United.Com> wrote in message news:<rOdGr40LMPU3-pn2-fMVlsUErjTFL@localhost>...
Hi.

Server DOES respond: it refuses my client. I can't figure out why. The
server does not return from ACCEPT when this hapends. I don't have any
firewall & sometimes client is connecting succesefully (75% of the
time). Timeout is big enough (~10 s)