Fail to accept connection
I am writing a server-client program, normally the server can accept a client connection using accept(). But I want to see some data structure about the server so I add some code for printing the data structure (which is a 2D array with about hundred entry) before the server begins to create socket, bind , listen and accept.
The create, bind and listen goes all right but accept fail with errno set to EBADF. So what should I do to eliminate the error??? Thx in advance |
You should pass accept() a valid socket.
|
Thx.
My code is like that listen(listenSockFD, (other needed argument)) on return, listen() gives 0 (ie valid), and listenSockFD is 3 (this is the value I usually get) then sessionFD = accept(listenSockFD, (other needed argument)) On return, sessionFD = -1 which mean failure what can I do to ensure the file descriptor valid? |
You should have something like: listenSockFD = socket(....)
|
Thx a lot
I have that but I was just lazy to include it in my last reply. I'll give more details when I'm back home as the ftp server has been down |
Here is an extract from my code
listenSockFD = socket(AF_INET, SOCK_STREAM, 0); // usually 3 is returned bind(listenSockFD, (struct sockaddr*)&serverAddr, sizeof(serverAddr)); //0 return listen(listenSockFD, 10); // 0 return sessionFD = accept(listenSockFD, (struct sockaddr*)&clientAddr, &clientLen); // return -1, this is where error occur, with errno set to EBADF. Error only occurs if I print a 2D table before creating socket. If I do not print the 2D table, there won't be error really need to help thx a lot |
Maybe the problem isn't in your socketing code then. Are you running out of bounds with your 2D array perhaps?
|
thx a lot~
i solve the problem after using bzero() on the address structure and buffer |
All times are GMT -5. The time now is 06:32 AM. |