WSAEFAULT on "connect" for (network-)client

Hi guys,

I'm not too familiar with network programming. In order to get more experience, I've been trying to establish a client-server connection. As for the server, it runs smoothly and is patiently waiting for a connection.

The client, however, always fails to connect to the server whenever I call the "connect" function with the Errorcode 10014. According to Microsoft, it happens whenever the pointer of 'sockaddr_in' is invalid or the size of the structure was allocated with too little memory. I don't see anything which could possibly lead to this error.

Here's the code (so far):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <winsock2.h>
#include <Windows.h>
#include <WS2tcpip.h>
#include <iostream>

int main()
{
	WSADATA wsa;
	if(WSAStartup(MAKEWORD(2,2),&wsa) == SOCKET_ERROR) {
		return -1;
	}

	SOCKET server_sock = socket( AF_INET, SOCK_STREAM, 0 );
	if (server_sock == SOCKET_ERROR) {
		printf("SOCKET Error: %i", WSAGetLastError());
		return -2;
	}

	/*struct hostent *host_entry;
	if ((host_entry = gethostbyname("192.168.2.103")) == NULL)  {
		return -3;
	}*/

	struct sockaddr_in server;
	server.sin_family = AF_INET;
	server.sin_port = htons(9998);
	inet_pton(AF_INET, "192.168.2.103", &server.sin_addr);

	//It always ends here with error code #10014, 
	if (::connect(server_sock, (sockaddr*)&server, sizeof(server) == SOCKET_ERROR )) {
		printf("CONNECT Error: %i", WSAGetLastError());
		Sleep(10000);
		return -4;
	}
	/**
	... Other stuff.
	*/

    return 0;
}


Help would be greatly appreciated. Thanks in advance. :-)
The line should be:
 
if (::connect(server_sock, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR ) {
You have the parenthesis in the wrong place. It's always clearer to do:
1
2
ret = ::connect(server_sock, (sockaddr*)&server, sizeof(server));
if (ret == SOCKET_ERROR) {
Thanks for pointing that out. I've been wondering what possibly could've gone wrong, that I didn't think I made a mistake - small, yet grave enough - with the parenthesis.

Thanks again.
Topic archived. No new replies allowed.