CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    Help with a blocking function accept

    I created a TCP server program. After the function accept is called, it is blocking and doesn't return. I wander what might cause the blocking?

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Help with a blocking function accept

    accept will block if there is no connection present, and the socket is blocking.

    Viggy

  3. #3
    Join Date
    Jul 2005
    Posts
    1,030

    Re: Help with a blocking function accept

    Here is my code,
    Code:
    //TCP client
    #include <WinSock2.h>
    #include <WS2tcpip.h>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char* str = "hi, there";
    	int len = strlen(str);
    	char buffer[32];
    
    	sockaddr_in s;
    	memset(&s, 0, sizeof(sockaddr_in));
    
    	s.sin_family = AF_INET;
    	s.sin_addr.s_addr = inet_addr("127.0.0.1");
    	s.sin_port = htons(7);
    	WSADATA wsadata;
    
    	WSAStartup(MAKEWORD(2,2), &wsadata);
    
    	SOCKET sock = socket(PF_INET,SOCK_STREAM, IPPROTO_TCP);
    
    	if(sock == INVALID_SOCKET)
    	{
    		WSACleanup();
    		return 0;
    	}
    
    	if(connect(sock, (SOCKADDR*)&s, sizeof(s)) == SOCKET_ERROR)
    	{
    		WSACleanup();
    		return 0;
    	}
    
    	int strlength = strlen(str);
    
    	if(send(sock, str, strlength, 0) != strlength)
    	{
    		WSACleanup();
    		return 0;
    	}
    
    	int totalBytesReceived = 0;
    	int recvBytes = 0;
    
    	while(totalBytesReceived < len)
    	{
    		recvBytes = recv(sock, buffer, 32, 0);
    	}
    	return 0;
    }
    
    //TCP server
    #include <WinSock.h>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	sockaddr_in s;
    	sockaddr_in clientS;
    	int size = sizeof(clientS);
    
    	s.sin_addr.s_addr = htonl(INADDR_ANY);
    	s.sin_port = htons(7);
    	s.sin_family = AF_INET;
    
    	WSADATA wsaData;
    
    	WSAStartup(MAKEWORD(2,2), &wsaData);
    
    	SOCKET sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    	if(sock == INVALID_SOCKET)
    	{
    		WSACleanup();
    		return 0;
    	}
    
    	if(bind(sock, (sockaddr*)&s, sizeof(s)) == SOCKET_ERROR)
    	{
    		WSACleanup();
    		return 0;
    	}
    
    	if(listen(sock, 5) == SOCKET_ERROR)
    	{
    		WSACleanup();
    		return 0;
    	}
    
    	for(;;)
    	{
    		if(accept(sock, (sockaddr*)&clientS,&size) == INVALID_SOCKET)
    		{
    			WSACleanup();
    			return 0;
    		}
    	}
    
    	WSACleanup();
    	return 0;
    }
    In the client side, the function call recv is also blocking. Can you find out anything with the code. Thanks.
    Quote Originally Posted by MrViggy View Post
    accept will block if there is no connection present, and the socket is blocking.

    Viggy

  4. #4
    Join Date
    Jul 2005
    Posts
    266

    Re: Help with a blocking function accept

    You are using blocking sockets (the default ones) which means operations like accept and recv will block until they get something. If you don't that you can set the sockets to non-blocking. Anyway you should read up about it for example here -> http://tangentsoft.net/wskfaq/articl...trategies.html.

  5. #5
    Join Date
    Jul 2005
    Posts
    1,030

    Re: Help with a blocking function accept

    Actually I tried to use echo service in windows. I guess echo service is disabled, so that is why operation like accept and recv always block. How'd I know whether echo service is enabled in my computer(Windows XP)?
    Quote Originally Posted by kolkoo View Post
    You are using blocking sockets (the default ones) which means operations like accept and recv will block until they get something. If you don't that you can set the sockets to non-blocking. Anyway you should read up about it for example here -> http://tangentsoft.net/wskfaq/articl...trategies.html.

  6. #6
    Join Date
    Jul 2005
    Posts
    1,030

    Re: Help with a blocking function accept

    Can any guru here point out in the following code why the operations like accept and recv are blocking? Why didn't they get anything? From TCP client, I already sent the string to echo service port. I should expect to get the string back, right? BTW, I typed in the command "echo" under command line and it returns "echo is on.". Does it mean I have echo service enabled? Thanks for your help.
    Quote Originally Posted by LarryChen View Post
    Here is my code,
    Code:
    //TCP client
    #include <WinSock2.h>
    #include <WS2tcpip.h>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char* str = "hi, there";
    	int len = strlen(str);
    	char buffer[32];
    
    	sockaddr_in s;
    	memset(&s, 0, sizeof(sockaddr_in));
    
    	s.sin_family = AF_INET;
    	s.sin_addr.s_addr = inet_addr("127.0.0.1");
    	s.sin_port = htons(7);
    	WSADATA wsadata;
    
    	WSAStartup(MAKEWORD(2,2), &wsadata);
    
    	SOCKET sock = socket(PF_INET,SOCK_STREAM, IPPROTO_TCP);
    
    	if(sock == INVALID_SOCKET)
    	{
    		WSACleanup();
    		return 0;
    	}
    
    	if(connect(sock, (SOCKADDR*)&s, sizeof(s)) == SOCKET_ERROR)
    	{
    		WSACleanup();
    		return 0;
    	}
    
    	int strlength = strlen(str);
    
    	if(send(sock, str, strlength, 0) != strlength)
    	{
    		WSACleanup();
    		return 0;
    	}
    
    	int totalBytesReceived = 0;
    	int recvBytes = 0;
    
    	while(totalBytesReceived < len)
    	{
    		recvBytes = recv(sock, buffer, 32, 0);
    	}
    	return 0;
    }
    
    //TCP server
    #include <WinSock.h>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	sockaddr_in s;
    	sockaddr_in clientS;
    	int size = sizeof(clientS);
    
    	s.sin_addr.s_addr = htonl(INADDR_ANY);
    	s.sin_port = htons(7);
    	s.sin_family = AF_INET;
    
    	WSADATA wsaData;
    
    	WSAStartup(MAKEWORD(2,2), &wsaData);
    
    	SOCKET sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    	if(sock == INVALID_SOCKET)
    	{
    		WSACleanup();
    		return 0;
    	}
    
    	if(bind(sock, (sockaddr*)&s, sizeof(s)) == SOCKET_ERROR)
    	{
    		WSACleanup();
    		return 0;
    	}
    
    	if(listen(sock, 5) == SOCKET_ERROR)
    	{
    		WSACleanup();
    		return 0;
    	}
    
    	for(;;)
    	{
    		if(accept(sock, (sockaddr*)&clientS,&size) == INVALID_SOCKET)
    		{
    			WSACleanup();
    			return 0;
    		}
    	}
    
    	WSACleanup();
    	return 0;
    }
    In the client side, the function call recv is also blocking. Can you find out anything with the code. Thanks.

  7. #7
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Help with a blocking function accept

    See posts 2 and 4 above.

  8. #8
    Join Date
    Jul 2005
    Posts
    1,030

    Re: Help with a blocking function accept

    My questions are based on the previous post. If there is no connection, then accept or recv are blocking. So my question is why there isn't connection. I posted my code and hopefully any guru here will point out what I did wrong or what I missed.
    Quote Originally Posted by hoxsiew View Post
    See posts 2 and 4 above.

  9. #9
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Help with a blocking function accept

    I'm not sure what you are asking. accept() is supposed to block until a connection is actually present.

    See MSDN remarks on accept():

    http://msdn.microsoft.com/en-us/libr...26(VS.85).aspx

    The accept function can block the caller until a connection is present if no pending connections are present on the queue, and the socket is marked as blocking. If the socket is marked as nonblocking and no pending connections are present on the queue, accept returns an error as described in the following. After the successful completion of accept returns a new socket handle, the accepted socket cannot be used to accept more connections. The original socket remains open and listens for new connection requests.
    and for recv(), if there is no connection, you should get an error. Again, see MSDN:

    http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx

    The recv function is used to read incoming data on connection-oriented sockets, or connectionless sockets. When using a connection-oriented protocol, the sockets must be connected before calling recv. When using a connectionless protocol, the sockets must be bound before calling recv.
    As for echo, windows machines don't normally run an echo server. "echo" in a command prompt, is a command to turn echo on or off in the terminal and applies only to that instance of cmd.exe.

    As for you code snippit above, read the MSDN documentation, add some error checking and do some debugging.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured