ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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?
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.