winsock problem

This is a discussion on winsock problem within the Networking/Device Communication forums, part of the General Programming Boards category; I'm getting a "socket not connected" error, but I can't figure out whats wrong with my code. Server Code: #include ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    33

    winsock problem

    I'm getting a "socket not connected" error, but I can't figure out whats wrong with my code.

    Server
    Code:
    #include <winsock2.h>
    #include <iostream>
    
    char *welcome = "Test message";
    
    int main()
    {
    	WSADATA WSAdata;
    
    	WSAStartup(MAKEWORD(2,2), & WSAdata);
    
    	sockaddr_in addr;
    
    	addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    	addr.sin_family = AF_INET;
    	addr.sin_port = htons(5432);
    
    	SOCKET Sock = socket(AF_INET, SOCK_STREAM, 0);
    
    
    	if(bind(Sock, (SOCKADDR*) &addr, sizeof(addr)) == -1)
    	{
    		std::cout << "Error in binding\n";
    		WSACleanup();
    		return 1;
    	}
    
    	if(listen(Sock,5) == -1)
    	{
    		std::cout << "Error in listening\n";
    		WSACleanup();
    		return 1;
    	}
    
    
    	int size = sizeof(addr);
    
    	if(accept(Sock, (LPSOCKADDR) &addr, &size) == -1)
    	{
    		std::cout << "Failure in connection\n";
    		WSACleanup();
    		return 1;
    	}
    
    		std::cout << "Client connected!\n";
    		std::cout << "Ip: " << inet_ntoa(addr.sin_addr) << "\n";
    
    
    	if(send(Sock, welcome, strlen(welcome) , 0) == -1)
    	{
    		std::cout << "Error while sending\n";
    		std::cout << WSAGetLastError();
    		WSACleanup();
    		return 1;
    	}			
    	else
    		std::cout << "Sending Message!\n";
    
    
    	shutdown(Sock,2);
    	WSACleanup();
    
    	return 0;
    }
    Client
    Code:
    #include <winsock2.h>
    #include <iostream>
    
    
    int main()
    {
    	WSADATA WSAdata;
    
    	WSAStartup(MAKEWORD(2,2), & WSAdata);
    
    	sockaddr_in client;
    
    	client.sin_addr.s_addr = inet_addr("127.0.0.1");
    	client.sin_family = AF_INET;
    	client.sin_port = htons(5432);
    
    	SOCKET Sock = socket(AF_INET, SOCK_STREAM, 0);
    
    	
    	if(connect(Sock, (SOCKADDR*) &client, sizeof(client)) == SOCKET_ERROR)
    	{
    		std::cout << "Error in connecting\n";
    		std::cout << WSAGetLastError();
    		WSACleanup();
    		return 1;
    	}
    
    	char  buff[40] = "";
    
    	if(recv(Sock, buff, strlen(buff), 0) == -1)
    		std::cout << WSAGetLastError();
    
    	
    
    	WSACleanup();
    
    
    
    
    	return 0;
    }
    Thanks
    “Focused, hard work is the real key to success. Keep your eyes on the goal, and just keep taking the next step towards completing it. " -John Carmack

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    > I'm getting a "socket not connected" error
    That doesn't correspond to any of the error messages in your code.

    > WSAGetLastError();
    This should tell you why - what did you get printed for this when it raised the error?
    Go look it up in the manual to find out more.

    Do you have a firewall running on your local machine for example?

    > if(recv(Sock, buff, strlen(buff), 0) == -1)
    buff is "", so strlen buff is 0
    Try sizeof(buff) instead

    Better yet, use sizeof(buff)-1, so you can append the \0 to make it a proper C string

    Code:
    if( (n=recv(Sock, buff, sizeof(buff)-1, 0)) != -1) {
        buff[n] = '\0';
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    33
    Doesn't work. The exact error codes are 10057 for the server, and 10054 for client (I believe this is conn. reset by peer). I don't have a firewall, and as you probably noticed, am just doing this via simple loopback on my own local ip.
    “Focused, hard work is the real key to success. Keep your eyes on the goal, and just keep taking the next step towards completing it. " -John Carmack

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    33
    Thanks for the help. I fixed the problem.
    “Focused, hard work is the real key to success. Keep your eyes on the goal, and just keep taking the next step towards completing it. " -John Carmack

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 12:11 PM
  2. Winsock packet problem
    By Rare177 in forum Windows Programming
    Replies: 7
    Last Post: 10-05-2004, 05:53 PM
  3. Winsock Problem
    By Noxir in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2004, 11:50 AM
  4. WinSock Problem
    By loobian in forum C++ Programming
    Replies: 1
    Last Post: 02-09-2002, 11:25 AM
  5. Small Winsock problem...
    By SyntaxBubble in forum C++ Programming
    Replies: 0
    Last Post: 02-09-2002, 10:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21