Thread: Socket Error 10049

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Socket 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);
    	}
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Fixed >< Apparently INADDR_ANY is null... So I used INADDR_LOOPBACK instead.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. Socket Help - Multiple Clients
    By project95talon in forum C Programming
    Replies: 5
    Last Post: 11-17-2005, 02:51 AM
  3. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 10:33 AM
  4. problem closing socket
    By Wisefool in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-29-2003, 12:19 PM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM