Why does my server cannot respond to multiple clients simultaneously even after using fork function?
Hi, I'm a newbie here. I'm trying to create an SMTP server that can respond to multiple clients simultaneously. I use the fork function in socket programming. Below is the server program (not including the variables).
smtp.c Code:
... The result turns out like this: Quote:
Quote:
|
Hi, and welcome to LQ!
Although the rules here ask that you not post homework or school assignments verbatim, members are happy to help if you are having difficulty with specific problems. Posting whole code and asking others to debug it for you is not a good way to get help. Everyone volunteers their time, so working up a simplest example which reproduces the problem for others is always appreciated! And it is the best learning exercise for you as well - very often in the process of explaining the problem to others in simplest terms you will understand it well enough to fix it yourself! What have you done to troubleshoot the problem? Are any error messages produced when the connection fails, and if so, what are they? Please review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help! |
Why not wrap some of that code into functions so you don't have one huge main one?
The benefit of it that if a smaller function works as intended, it's less noise, less to think about vs it being expanded. This also helps with debugging, especially variable scope problems since it's easy to overlook one of the many variables required for a huge spaghetti function. That is because inside the then encapsulated function, the variable it needs must be there and set correctly, which is easier to manage if the scope of the variable is, well, constrained to only the function. So if you had a variable like "sock", and it were worked with in 10 places in the code, then it would be easy for that to mess up in one huge spaghetti function, vs 1 or 2 times per small function, which you feed the sock variable as an argument, and have it returned. Code:
int rock(int sock) |
Quote:
You need to remove that waitpid from the main loop, and handle child reaping with a SIGCHLD signal handler. Beej's excellent guide may be of interest to you: http://beej.us/guide/bgnet/html/ |
Thank you for the advice! I tried to use SIGCHLD, but it did not work. Maybe there's something wrong at the client program. I'll just do my best.
|
Then periodically try some non-blocking version of wait:
Code:
pid_t exitedchild= waitpid(-1, &wstatus, WNOHANG); |
I understand that the flow of the socket programming and fork function is correct, but where should I put the
Code:
pid_t exitedchild= waitpid(-1, &wstatus, WNOHANG); |
An option is calling it right after (or before) 'accept'.
|
All times are GMT -5. The time now is 11:58 AM. |