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:

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:

#include
#include
#include
#include
#include

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:

#include
#include
#include
#include
#include

#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:

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);
}