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

    Socket connect error 10049

    Hi, so I'm learning socket programming in Windows XP. I'm trying to set up a simple server/client interaction, but I can't seem to get it working. Whenever I run the client, I get a "Socket connect error 10049" message printed to the console (meaning WSAGetLastError() is returning the error number 10049). Error 10049 translates to:

    Code:
    WSAEADDRNOTAVAIL
    10049
    0x2741
    The requested address is not valid in its context.
    NOTE: The key part of the code that's causing the trouble is at the bottom of the code if you don't want to read all of that.

    Here's the code of the client:

    Code:
    #include <iostream>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <cstring>
    #include <Mstcpip.h>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    WSADATA wsaData;
    
    int main(int argc, char * argv[]) {
    	int wsaReturnVal = WSAStartup(MAKEWORD(1, 1), &wsaData);
    	if (wsaReturnVal) {
    		cout << "Error number: " << wsaReturnVal << endl;
    		exit(1);
    	}
    
    	char * fpoint = strrchr(argv[0], '\\');
    	if (argc != 2) {
    		printf("Usage: <%s> <\"string\"|ping>", fpoint+1);
    		exit(1);
    	}
    
    	struct sockaddr_in serv;
    	SOCKET sockfd;
    
    	memset(serv.sin_zero, '\0', sizeof(serv.sin_zero));
    
    	serv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    	serv.sin_family = AF_INET;
    	serv.sin_port = htons(3490);
    
    	sockfd = socket(PF_INET, SOCK_STREAM, 0);
    
    	if (sockfd == SOCKET_ERROR) {
    		printf("Socket create error %d", WSAGetLastError());
    		exit(1);
    	}
    
    	if (connect(sockfd, (struct sockaddr *)&serv, sizeof(sockaddr_in)) == SOCKET_ERROR) {
    		printf("Socket connect error %d", WSAGetLastError());
    		exit(1);
    	}
    
    	printf("Connected to server...\n");
    
    	closesocket(sockfd);
    
    	if (WSACleanup()) {
    		cout << "Cleanup Error." << endl;
    		exit(1);
    	}
    }
    And here is the code of the server:

    Code:
    #include <iostream>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <cstring>
    #include <Mstcpip.h>
    
    #define MYPORT 3490
    #define BACKLOG 10
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    WSADATA wsaData;
    
    int main(int argc, char * argv[]) {
    	int wsaReturnVal = WSAStartup(MAKEWORD(1, 1), &wsaData);
    	if (wsaReturnVal) {
    		cout << "Error number: " << wsaReturnVal << endl;
    		exit(1);
    	}
    
    	struct sockaddr_in serv, their_addr;
    	SOCKET sockfd, newfd = NULL;
    
    	memset(&serv, 0, sizeof(serv));
    
    	serv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    	serv.sin_family = AF_INET;
    	serv.sin_port = htons(MYPORT);
    
    	if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == SOCKET_ERROR) {
    		printf("Socket create error %d", WSAGetLastError());
    		exit(1);
    	}
    
    	printf("Socket created...\n");
    
    	if (bind(sockfd, (sockaddr *)&serv, sizeof(sockaddr_in)) == SOCKET_ERROR) {
    		printf("Socket bind error %d", WSAGetLastError());
    		exit(1);
    	}
    	printf("Bound socket to address: %s...\n", serv.sin_addr);
    
    	if (listen(sockfd, BACKLOG) == -1) {
    		printf("Socket listen error %d", WSAGetLastError());
    		exit(1);
    	}
    
    	printf("Listening on socket...\n");
    
    	int addr_size = sizeof(their_addr);
    	if ((newfd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size)) == INVALID_SOCKET) {
    		printf("Accept error %d", WSAGetLastError());
    		closesocket(newfd);
    		exit(1);
    	}
    
    	closesocket(sockfd);
    
    	closesocket(newfd);
    
    	if (WSACleanup()) {
    		printf("Cleanup Error %d", WSAGetLastError());
    		exit(1);
    	}
    
    }
    NOTE: The server works perfectly fine (other than the address it prints, instead of printing the address it prints "(null)", but I ran a netstat to ensure the socket was working correctly, and it was infact in listening state on port 3490. However I cannot get the client to connect to it... Ok thanks!

    EDIT: The following code was taken out of the Client program, but this is where the error lies. Somewhere in here... Because the error is printed at that point, so anything before or up to it is the troublesome code:

    Code:
    	struct sockaddr_in serv;
    	SOCKET sockfd;
    
    	memset(serv.sin_zero, '\0', sizeof(serv.sin_zero));
    
    	serv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
    	serv.sin_family = AF_INET;
    	serv.sin_port = htons(3490);
    
    	sockfd = socket(PF_INET, SOCK_STREAM, 0);
    
    	if (sockfd == SOCKET_ERROR) {
    		printf("Socket create error %d", WSAGetLastError());
    		exit(1);
    	}
    
    	if (connect(sockfd, (struct sockaddr *)&serv, sizeof(sockaddr_in)) == SOCKET_ERROR) {
    		printf("Socket connect error %d", WSAGetLastError());
    		exit(1);
    	}
    Last edited by pobri19; October 29th, 2008 at 05:44 AM.

  2. #2
    Join Date
    Aug 2007
    Location
    Birmingham, UK
    Posts
    360

    Re: Socket connect error 10049

    Your client tries to connect to address INADDR_ANY, which makes no sense to the network stack. It is like dropping a letter in the post with no address on it. You need to specify a proper IP address for the socket so that it knows where to connect to. INADDR_ANY is only used with bind for incoming connections, like you are doing with your server.

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