CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2008
    Posts
    29

    New To Sockets. [WinSock]

    I'm just making a simple socket program that the client can send strings to the server.

    Server:
    Code:
    #include <iostream>
    #include <winsock2.h>
    
    int main()
    {
    	WORD	SocketVersion;
    	WSADATA WsaData;
    	int		WsaErr;
    	bool	InternalError(false);
    	bool	ActiveAccept(true);
    	bool	ActiveApp(true);
    
    	SocketVersion = MAKEWORD(2, 2);
    	WsaErr = WSAStartup(SocketVersion, &WsaData);
    
    	std::cout << "\n";
    
    	if(WsaErr == 0)
    	{
    		if(LOBYTE(WsaData.wVersion) == 2 || HIBYTE(WsaData.wVersion) == 2)
    		{
    			SOCKET My_Socket;
    			My_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    
    			if(My_Socket != INVALID_SOCKET)
    			{
    				sockaddr_in Server;
    				Server.sin_family		 = AF_INET;
    				Server.sin_addr.s_addr	 = inet_addr("127.0.0.1");
    				Server.sin_port			 = htons(55555);
    
    				if(bind(My_Socket, (SOCKADDR*)&Server, sizeof(Server)) != SOCKET_ERROR)
    				{
    					if(listen(My_Socket, 1) != SOCKET_ERROR)
    					{
    						SOCKET AcceptConnection;
    						AcceptConnection = SOCKET_ERROR;
    						
    						std::cout << " Server: The Server Has Been Started Up Successfully!\n";
    						
    						while(ActiveAccept == true)
    						{
    							AcceptConnection = accept(My_Socket, NULL, NULL);
    							if(AcceptConnection != SOCKET_ERROR)
    							{
    								ActiveAccept = false;
    							}
    						}
    						
    						int ClientConnected = 1;
    						My_Socket = AcceptConnection;
    
    						//######## MAIN LOOP
    						while(ActiveApp == true)
    						{
    							if(ClientConnected == 1)
    							{
    								std::cout << " Server: Client Connected!\n";
    								ClientConnected = 0;
    							}
    							//######## START SEND RECEIVE HERE
    
    							static char Message[5000] = "";
    							
    							if(recv(My_Socket, Message, 5000, 0) != SOCKET_ERROR)
    							{
    								std::cout << " Server->Message: " << Message << "\n";
    							}
    							std::cout << "test";
    
    							Sleep(1000);
    						}
    					}
    					else
    					{
    						std::cout << " Server: " << WSAGetLastError() << "\n";
    						InternalError = true;
    					}
    				}
    				else
    				{
    					std::cout << " Server: " << WSAGetLastError() << "\n";
    					InternalError = true;
    				}
    			}
    			else
    			{
    				std::cout << " Server: " << WSAGetLastError() << "\n";
    				InternalError = true;
    			}
    		}
    		else
    		{
    			std::cout << " Server: The DLL in use does not support this version of WinSock!\n";
    			InternalError = true;
    		}
    	}
    	else
    	{
    		std::cout << " Server: There was an error starting WinSock!\n";
    		InternalError = true;
    	}
    
    	// Internal Error Loop
    	if(InternalError == true)
    	{
    		for(int InternErr = 0;InternErr < 3;InternErr++)
    		{
    			if(InternErr == 1)
    			{
    				std::cout << "\n Closing...\n";
    			}
    			Sleep(1000);
    		}
    	}
    }
    Client:
    Code:
    #include <iostream>
    #include <winsock2.h>
    #include <string>
    
    int main()
    {
    	WSADATA WsaData;
    	WORD	SocketVersion;
    	int		SocketCreate;
    	bool	InternalError(false);
    
    	SocketVersion = MAKEWORD(2, 2);
    	SocketCreate = WSAStartup(SocketVersion, &WsaData);
    
    	std::cout << "\n";
    
    	if(SocketCreate == NO_ERROR)
    	{
    		SOCKET My_Socket;
    		My_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    		if(My_Socket != INVALID_SOCKET)
    		{
    			sockaddr_in Client;
    			Client.sin_family		= AF_INET;
    			Client.sin_addr.s_addr	= inet_addr("127.0.0.1");
    			Client.sin_port			= htons(55555);
    
    			if(connect(My_Socket, (SOCKADDR*)&Client, sizeof(Client)) != SOCKET_ERROR)
    			{
    				//######## MAIN LOOP
    				while(1)
    				{
    					//######## START SEND RECEIVE HERE
    					static std::string Message;
    
    					std::cout << " Client->Type A Message To Send To Server: ";
    					getline(std::cin, Message);
    
    					if(Message != "")
    					{
    						if(send(My_Socket, Message.c_str(), 5000, 0) != SOCKET_ERROR)
    						{
    							std::cout << " Client: Message Sent!!!\n";
    							Message = "";
    						}
    					}
    				}
    			}
    			else
    			{
    				std::cout << " Client: Faild To Connect To Server!!!\n";
    				InternalError = true;
    			}
    		}
    		else
    		{
    			std::cout << " Client: " << WSAGetLastError() << "\n";
    			InternalError = true;
    		}
    	}
    	else
    	{
    		std::cout << " Client: There was an error on WSAStartup()!!!\n";
    		InternalError = true;
    	}
    
    	// Internal Error Loop
    	if(InternalError == true)
    	{
    		for(int InternErr = 0;InternErr < 3;InternErr++)
    		{
    			if(InternErr == 1)
    			{
    				std::cout << "\n Closing...\n";
    			}
    			Sleep(1000);
    		}
    	}
    }
    On the server where I receive the data I see that it waits for a response and does not let the loop continue. Thats why I put std::cout << "test" to test it.
    I'm going to want to check other things in the loop to. Which means don't want it pulsing there.

    Is there a way to fix this? Am I doing something wrong?

    Thank you.

  2. #2
    Join Date
    Jan 2003
    Posts
    615

    Re: New To Sockets. [WinSock]

    Your problem is a bit unclear.

    Does the server 'accept' a connection? Does your code reach the recv(..) method? I believe this is a blocking method so if no data is sent the the code will block. How many byte does the client send (see send() return value)?
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  3. #3
    Join Date
    Oct 2005
    Location
    Australia
    Posts
    46

    Re: New To Sockets. [WinSock]

    recv doesn't return until it gets some data. You can change this if you want, but as it stands the execution will stop at the recv line until the client sends something.

  4. #4
    Join Date
    Oct 2008
    Posts
    29

    Re: New To Sockets. [WinSock]

    Yeah like jarro said. What do I do to fix this problem?

  5. #5
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: New To Sockets. [WinSock]

    this is no 'problem'. It is how blocking sockets are supposed to work. If you do not want to be blocked by the call to recv(), read up on the use of select().

    If the client sends a message after the connect() is successful, you will be able to receive it on server side. If it sends it some time later, maybe select() is your friend.

  6. #6
    Join Date
    Oct 2005
    Location
    Australia
    Posts
    46

    Re: New To Sockets. [WinSock]

    You can change it to return immediately by passing MSG_DONTWAIT as the fourth parameter. Alternatively there is a function call to set it permanently, look up non blocking sockets. In that case it returns something else to say that it returned due to non blocking rather than an error.

  7. #7
    Join Date
    Oct 2008
    Posts
    29

    Re: New To Sockets. [WinSock]

    You sure about MSG_DONTWAIT? I get undeclared identifier.

    Thanks.

  8. #8
    Join Date
    Jan 2003
    Posts
    615

    Re: New To Sockets. [WinSock]

    Quote Originally Posted by Clipster View Post
    Yeah like jarro said. What do I do to fix this problem?
    Does the client send data to the client? This should trigger the recv() method to stop blocking.

    Maybe this could help you http://www.scottklement.com/rpg/sock...nblocking.html
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  9. #9
    Join Date
    Oct 2008
    Posts
    29

    Re: New To Sockets. [WinSock]

    Code:
    ***** retrieve flags
             c                   eval      flags = fcntl(sock: F_GETFL)
             c                   if        flags < 0
             C* handle error "unable to retrieve descriptor status flags"
             c                   endif
        
             ***** turn non non-blocking flag:
             c                   eval      flags = flags + O_NONBLOCK
        
             ***** set flags
             c                   if        fcntl(sock: F_SETFL: flags) < 0
             C* handle error "unable to set descriptor status flags"
             c                   endif
    What is this??

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