|
/* --------------------------- Create a TCP socket --------------------------- */ s1 = socket(AF_INET, SOCK_STREAM, 0); .... /* --------------------------------------------------------- Bind to a network address so that the server can listen for connection requests -------------------------------------------------------- */ if ( bind(s1, (struct sockaddr *) &server, sizeof(server) ) < 0 ) { perror("bind:"); exit(1); } /* ------------------------------------------- Listen for connection requests on s1 -------------------------------------------- */ listen(s1, 5); while (1) { len = sizeof(from); /* ----------------------------------------------- Accept connection requests ----------------------------------------------- */ if ( (s2 = accept(s1, (struct sockaddr *) &from, &len)) < 0 ) { perror("accept: "); exit(1); } /* ---------------------------------------------- You arrive here when the server has accepted a connection request from one client Use s2 to communicate with the client ---------------------------------------------- */ if ( fork() == 0 ) { /* ---------------------------- Child process (the clone) ---------------------------- */ close(s1); // Don't need to listen /* --------------------------- ---+ Processing loop | --------------------------- */ | while ( read( s2, buf, .... ) > 0 ) | Change this { | to do process data in buf | other tasks write(s2, reply, ...); | } ---+ close(s2); exit(0); } else { /* ------------------------------- Parent process (the original) ------------------------------- */ close(s2); // We loop back up and accept the next connection request } } |
(While the parent process goes back and executed another accept call)
while (1)
{
s2 = accept(s1, (struct sockaddr *) &from, &len));
/* ---------------------
fork()
--------------------- */
if ( fork() == 0 )
{
/* =====================================
Code executed by the child process
===================================== */
close(s1); // Child does not use Listen socket
/* =========================================
Use data socket and comm. with client
========================================= */
while (read(s2, &c, 1) > 0)
{ putchar(c);
fflush(stdout);
}
close(s2);
printf("\n\n...Done...\n\n");
exit(0); // Child process exits !!
}
else
{
close(s2);
}
}
|
How to run the program:
|
(While the parent process goes back and executed another accept call)
while (1) { s2 = accept(s1, (struct sockaddr *) &from, &len)); if ( fork() == 0 ) { /* ====================================== Code executed by the child process ====================================== */ close(s1); // Child does not use the Listen socket /* ========================================= Use data socket and comm. with client ========================================= */ while ( 1 ) { int x1, y1, z1; if ( read( s2, &x1, 4) <= 0 ) break; x = ntohl(x1); // Convert to host order if ( read( s2, &y1, 4) <= 0 ) break; y = ntohl(y1); // Convert to host order z = x + y; printf("Client data: %d, %d, send sum = %d\n", x, y, z); z1 = htonl(z); write ( s2, &z1, 4 ); } close(s2); printf("\n\n...Done...\n\n"); exit(0); } else { close(s2); } } |
How to run the program:
|