| NewProgrammer (56) | |||
|
Hi everyone, I'm attempting to create a client-server application. However, an exception keeps being thrown whenever accept() is called. Here's the code segment:
Apparently, since the ErrorMessage was thrown, s_sock must be a negative number. But I passed the same portnumber and "localhost" as parameters to the Client program, so I am not sure what else could be the issue. | |||
|
Last edited on
|
|||
| kbw (5375) | |||
|
accept() returns a socket for the new connection. It also returns the address of the connection. If the case of TCP, you know in advance (apriori) that the address type is a sockaddr_in. So how does accept know that you passed it a sockaddr_in and not some other kind of address? It knows by the size that you pass in. It also returns the size of the structure it used. And it does all this in the same parameter. So your code should look like:
| |||
|
|
|||
| NewProgrammer (56) | |
|
That worked. Thank you, but I am confused as to where I went wrong. We handled the first two arguments identically (except for naming). The only difference in our third arguments, aside from naming, is you performed sizeof() before passing in the argument instead of simultaneously like I did. Where did I go wrong? Thanks again. NP | |
|
|
|
| computerquip (1892) | |
| The function expects a pointer, not an integer. Just casting an integer to a pointer doesn't fix this. | |
|
|
|
| NewProgrammer (56) | |
|
Understood. NP | |
|
|
|