|
-
March 30th, 2005, 01:49 AM
#1
Non-blocking socket - connection problem
I have created a Dialog application. Acting as Server. And if I click a button the server will start listening for connections from the client.
When I click the button to start listening ,
(I am using the normal socket functions), so after issuing accept the dialog app hangs. So I modified the code in such a way that the socket call for accept does not block using the ioctl socket function on listening socket.
Now the dialog app did not hang.
My problem is , when I tried to connect with the client the connect is successful but the server side cannot accept the incoming connection instead I got an error when I used GetLastError as following:
"A non-blocking socket operation could not be completed immediately."
I want to know how should I able to achieve connection (or) How should I make the accept call succeeded.
/*************************************************/
Here is the part of Server code:
ULONG NonBlock;
listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Address.sin_family = AF_INET;
Address.sin_port = htons(listenPort);
Address.sin_addr.s_addr = htonl(INADDR_ANY);
//bind the socket id to the local address
if (bind(listenSocket,(struct sockaddr *) &WdxAddress, sizeof(WdxAddress)) == SOCKET_ERROR)
{
}
if (listen(listenSocket, 100) == SOCKET_ERROR)
{
}
NonBlock = 1;
if (ioctlsocket(listenSocket, FIONBIO, &NonBlock) == SOCKET_ERROR)
{
return;
}
clientAddressLength = sizeof(clientAddress);
if ((connectSocket = accept(listenSocket,(struct sockaddr *) &clientAddress, &clientAddressLength)) < 0)
{
}
/*************************************************/
Here is the Client code:
hostInfo = gethostbyname(szIPAddress);
serverAddress.sin_family = hostInfo->h_addrtype;
memcpy((char *) &serverAddress.sin_addr.s_addr,
hostInfo->h_addr_list[0], myApp->hostInfo->h_length);
serverAddress.sin_port = htons(myApp->serverPort);
if (connect(clientSocket,(struct sockaddr *) &serverAddress,
sizeof(serverAddress)) < 0)
{
return FALSE;
}
/*************************************************/
Can anybody help me in this regard?????
I dont want my Server Dialog app to be hanging until there is a connection is made by the client. Because I want to do some other tasks also with the dialog app meanwhile waiting for connections.
-
March 30th, 2005, 04:39 AM
#2
Re: Non-blocking socket - connection problem
You could run the blocking system calls in another thread. That way your dialog thread still runs and your networking thread sleeps until something happens on the network.
Another approach (if you're running a os that supports signals) is to use asynchronous io and set up a signal handler for the SIGIO signal and do the networking from that handler.
If you try and take a cat apart to see how it works,
the first thing you'll have on your hands is a nonworking cat
-
March 31st, 2005, 09:09 PM
#3
Re: Non-blocking socket - connection problem
Thank you very much.
Now I am using the thread to listen for the incoming connections. So my dialog app is having no problem. It can respond , meanwhile waiting for connection from client.
Thank you again for your tips.
-
April 12th, 2005, 11:33 AM
#4
Re: Non-blocking socket - connection problem
Is this the preferred method for setting a socket to non blocking? I've added it to my code and it errors. Any ideas? Everything works fine as a blocking socket.
unsigned long noblock;
if ( (sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET )
handle_error();
//Set to non-blocking socket
noblock = 1;
if ( (ioctlsocket(sock, FIONBIO, &noblock)) == INVALID_SOCKET )
handle_error();
-
April 12th, 2005, 11:45 AM
#5
Re: Non-blocking socket - connection problem
nevermind. i moved it down after the "connect" part of the socket initialize code and its working fine.
sorry
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|