views:

30

answers:

4

Hey guys,

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

+2  A: 

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

mojuba
Ah damn, in my source code the server Socket is named m_Server not s :)
Aurus
A: 

I debugged a little bit and every Fail Socket i get has got the value: 4294967295.

Aurus
... which is -1, which is an indication of an error
mojuba
A: 

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.

Aurus
A: 

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.

Jörgen Sigvardsson
WSAEINVAL -> 10022
Aurus
Oh crap sorry guys i forgot to use listen() -.-
Aurus