Why does my server cannot respond to multiple clients simultaneously even after using fork function?
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.
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).
To test it, I open two terminals, for example, A and B. At A's terminal, I write emails to C and B. At the same time, I write emails at B's terminal to A and C. After entering '.' at A's terminal to send the emails, I tried to send B's emails without exiting from A's terminal.
The result turns out like this:
Quote:
(SMTP)Waiting for connection
Connection request to C
Connection request to B
(SMTP)Waiting for connection
Connection request to
(SMTP)Waiting for connection
But I want the result to be like this:
Quote:
(SMTP) Waiting for connection
Connection request to C
Connection request to B
(SMTP) Waiting for connection
Connection request to A
Connection request to C
(SMTP) Waiting for connection
Are there any mistakes in the flow of the coding? It would be great if there's anyone that can help so that I can improve my understanding of socket and fork. Thank you in advance.
Last edited by intstudent123; 01-21-2020 at 05:11 PM.
Reason: Details
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)
{
return sock + 5;
}
int rockem_sockem(int sock){
return rock(sock);
}
int mockem_mockem(int sock){
return sock + 10;
}
int trocken_bocken(int sock){
return sock + 20;
}
int main(){
int sock = 0;
sock = rockem_sockem(sock);
sock = mockem_mockem(sock);
sock = trocken_bocken(sock);
//etc
return 0;
}
Now you have several encased places where working with sock might fail instead of sock being strewn all over the place like actual socks, having 10 different loops, functions and whatnots all meddle with it in one big place.
Are there any mistakes in the flow of the coding? It would be great if there's anyone that can help so that I can improve my understanding of socket and fork. Thank you in advance.
Unless I'm reading it wrong, you have a waitpid(child_pid,...) call within your accept() main loop. That's going to serialise client processing. Your server won't accept() a new client until the last one has been processed fully, effectively negating the purpose of forking the client processing to the background.
You need to remove that waitpid from the main loop, and handle child reaping with a SIGCHLD signal handler.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.