[MOVED] Winsock 2, accept problem & GetLastError

This topic has been moved by the original poster to:
http://www.cplusplus.com/forum/general/89945/
Last edited on
bump
When creating your address, convert the port byte order from the host's order to the network order using the htons function.

See if that helps.

 
address->sin_port = htons( port );
take a look at this:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms737625%28v=vs.85%29.aspx

connect() does not return INVALID_SOCKET, i.e. you misinterpret the result. check for SOCKET_ERROR

Edit: Also better do what iHutch105 suggest
Last edited on
Thanks iHutch105 and coder777, it fixed that problem.
But now I get the same style of error from bind()
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
void ServerSocket::bind()
{
	if (index < 0 || index > 99) throw NetCodeException("Index out of bounds( 0-99 )", "bind()");
	address = new INetAddress("127.0.0.1",2700 + index);
	int r = ::bind(sock, (const struct sockaddr*) address->address, sizeof (struct sockaddr));
	if (r == SOCKET_ERROR)
	{
		delete address;
		throw SocketException(WSAGetLastError(), "bind()");
	}
	bound = true;
}

...

try
	{
		ServerSocket * ss = new ServerSocket(0);
		ss->bind(0);
		ss->listen(5);
		Socket* s = ss->accept();
		Packet pack;
		pack.data = (byte*)"Hello wob.";
		pack.size = 10;
		ss->send(s,&pack);
		Packet* p = ss->recv(s);
		printf("Received %s\n",p);
		ss->close(s);
		ss->stop();
		delete p;
	} catch (const SocketException &x)
	{
		print(x.what());
		return;
	}

Error code:
Got error code #0 while bind()
The thing I find peculiar is that WSAGetLastError does not return anything useful.
Last edited on
Hmmmmm.

When creating a server, try changing the way you're constructing the address. See if it makes any difference.

Give this a go...
 
address.sin_addr.s_addr = htonl( INADDR_ANY );

No success, INADDR_ANY is basicly just (long)0 which I have tried..
This
 
int r = ::bind(sock, (const struct sockaddr*) address->address, sizeof (struct sockaddr));

should be this
 
int r = ::bind(sock, (const struct sockaddr*) address->address, sizeof (struct sockaddr_in));


I'm not sure about that address->address thing.

If you don't pass the size of the socket address record, it won't know what kind of address it is. It's an object oriented library implemented in C.
Last edited on
Thanks for the advice, it might save me from future bugs, but it didn't fix the problem.
Last edited on
bump
Hi there,

You may want to try moving the thread into "general C++ programming" or "windows programming" for better luck getting an answer to your question. Just a suggestion.

All the best,
NwN
Well thank you for the suggestion, I'll do that.
Topic archived. No new replies allowed.