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