Reputation: 705
I am currently make a Server, I learned to make something like this:
while(true)
{
SOCKET s = accept(s, ....)
// do something with the connection
printf("connection\n");
}
I learned that it will stuck at accept(..) while there isnt a connection. In my Program there isnt any connection yet, but it get overflowed with connection ?? I mean my Console got spammed with "connection".
So whats wrong?
THX Guys i fixed it now :)
Upvotes: 0
Views: 7243
Reputation: 4887
Call WSAGetLastError() and see what it returns. This error code can then be input into the Error Lookup tool (you'll find it in the tools menu of Visual Studio). You have most likely initialized your server socket incorrectly.
Upvotes: 0
Reputation: 705
I added some checks:
while(true)
{
SOCKET s =
accept(m_Socket, (sockaddr*)&from, &fromlen);
if(s != SOCKET_ERROR)
{
printf("Client connected to the Server. ");
Client* client = new Client(s); // Memory Leak here .. i know
}
}
My CPU is at 16%, although there isnt any Client connected.
Upvotes: 0
Reputation: 705
I debugged a little bit and every Fail Socket i get has got the value: 4294967295.
Upvotes: 0
Reputation: 12217
Most likely it returns immediately with an error which you don't seem to be checking. E.g. it may be that s wasn't properly created, etc.
Edit: just noticed that you are assigning the result of accept() to the same 's', which is terribly wrong. Your 's' is a general listening socket presumably created by socket(), bound by bind() and set to listening by listen(), whereas the return value of accept() is another socket which you should use for transfering data.
Take a look at this for example (just found by Googling): http://www.cs.odu.edu/~cs476/fall03/lectures/sockets.htm
Upvotes: 2