accept error

Hello guys, i'm trying to use the function accept() in a thread of my class, but i don't know what's happens, the function ever returns -1 and when i call perror("erro"); but the output is : "erro: no error", here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void Server::Initialize()
{
    WSADATA wsa;

    if(WSAStartup(MAKEWORD(2, 2), &wsa) == -1)
        throw(Error("Fail to initialize sockets API."));

    if((sock=socket(AF_INET, SOCK_STREAM, 0))== -1)
        throw(Error("Fail to create socket."));

    if(bind(sock, (struct sockaddr*)&ipHdr, sizeof(struct sockaddr)) == -1 || listen(sock, maxPlayer) == -1)
        throw(Error("Fail to put on the listening mode."));

    threadHandle = CreateThread(0, 0, (Thread)&Server::waitConnection, NULL, 0, &threadId);

    if(!threadHandle)
        throw(Error("Fail to create thread."));

    std::cout<<"Server is up and running"<<std::endl;
}


and here is the function to wait the connections:
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
DWORD WINAPI Server::waitConnection(LPVOID param)
{
    int tamIpHdr;
    int sockD;
    struct sockaddr_in ip_hdr;

    if(listen(this->sock, maxConnections/*5*/) == -1)
    {
        std::cout<<"Fail to setup the connections receive."<<std::endl;
        perror("listen");
        exit(0);
    }

    while(contConnections < maxConnections)
    {
        if((sockD=accept(this->sock, (struct sockaddr*)&ip_hdr, &tamIpHdr)) == SOCKET_ERROR)
        {
            std::cout<<"Fail to accept connection!"<<std::endl;
            perror("accept");//no error :/
            continue;
        }

        std::cout<<"New connection from: "<<inet_ntoa(ip_hdr.sin_addr)<<std::endl;

        infoConexao info = {sockD, ip_hdr};
        DWORD id;
        CreateThread(0, 0, (Thread)&Server::handleConnection, (LPVOID)&info, 0, &id);
    }

    return 0;
}


i put the function to waitConnection in a thread because the server do other things while await for connections.

if someone can help me i'll thank you.
thanks for attention.
=]
Something tells me that this would be much easier for you if you consolidated all of this (Initialize and waitConnection not necessarily handleConnection) into the same function. For example, this might prevent you from doing things like passing an uninitialized variable as the address length argument to your accept function. If you don't initialize a variable, then you don't know what's in it. So if for some random reason it contains 2 and the length required is 5 then you're going to get 'WSAFAULT' as an error.
Ok, i did, the error has changed: "error: result too large" you know what it is? i just ctrl+c and ctrl+v from waitConnection to Initialize function, and i created a thread to the function in main, but i take this error. and one more question: if the value of the third parameter for accept() is lower of what sizeof(struct sockaddr) them the function will return -1? because it's an error, right?
You do realize that it would be faster to test these questions then it would be to wait for me to respond right? Also, if all you did was copy and paste then you still did not initialize your variable 'ip_hdr' so it is still holding some random value. Technically, "accept()" would return 'INVALID_SOCKET' which is an unsigned integer with a value other than 0.
but if the accept use the pointer to give me the information of the client conected, i dont need to initialize the ip_hdr, just pass the pointer and he will give me the information, like they do int third parameter.

Some tutorials just pass NULL where i'm passing ip_hdr:

accept(sock, NULL, NULL);

you think using threads can affect the use of the variables of the class? because i look for others projects here, and they do the same thing, the only diference is the use of the thread
I'm only suggesting the consolidation of your code to make it easier for you too look at. But there is a difference between a '_Out_' and '_InOut_' argument type. '_Out_' means that the function ignores what you send in and simply overwrites it, '_InOut_', which this argument is, means the function considers the argument you pass in, in this case it sees it as a limitation you are imposing. If you really look at other projects here, most people don't pass uninitialized variables as arguments, in or out.
You need to initialise the parameters you pass to accept. I don't see where you set the record size.
Topic archived. No new replies allowed.