LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Fail to accept connection (https://www.linuxquestions.org/questions/programming-9/fail-to-accept-connection-249856/)

PoorGuy 11-01-2004 11:19 AM

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

itsme86 11-01-2004 11:23 AM

You should pass accept() a valid socket.

PoorGuy 11-01-2004 09:25 PM

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?

itsme86 11-01-2004 09:51 PM

You should have something like: listenSockFD = socket(....)

PoorGuy 11-01-2004 10:58 PM

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

PoorGuy 11-02-2004 10:40 AM

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

itsme86 11-02-2004 10:42 AM

Maybe the problem isn't in your socketing code then. Are you running out of bounds with your 2D array perhaps?

PoorGuy 11-04-2004 09:10 AM

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.