Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Basic concurrent client/server architecture: There's a main loop listening for requests on a port (for example 3000), after accepting the connection the server spawns a child process that ends up having access to file descriptors where data can be read.

If we have multiple clients connected to the server, the server will have a child process per request. So S1 (child server process) reads data from C1 (a client), S2 reads from C2 and so on. My question is how is it possible that all clients (C1, C2...) are sending information to the same port (3000) and yet the server processes (S1, S2...) are reading only the information sent from the client assigned to them? Where is the multiplexing being done and how?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

A TCP/IP connection has both a source port and a destination port, so if the same server connects to another server on port 3000 multiple times, the Linux kernel can sort out the connections because each one has a unique IP + source port + destination port.

This can be seen with the output of netstat when there are active TCP connections, which shows both the local source port and foreign destination port.

As an aside, doing a fork() for each connection is a really bad idea for a server getting any significant load; fork() is a slow, resource-intensive system call. There's a reason nginx is becoming popular; it uses a difficult-to-program fork()-free model for delivering static content.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.