Socket server or file server implementation using multiple threads: concept not clear
Please help me clear this concept. Say we have A socket port server implemented using threads. The socket server listens on a socket port and, when a message arrives, create a thread to service the request.
The client code sends a given number of messages to the server. This client code could also be run from different machines by multiple users. I understand that the cl开发者_开发知识库ient code codes are run as seperate processes.Ihat is seperate processes issue requests to the server which is then processed by a server thread.
So, does a client processes stack, user address space, process control block etc pass on to the server thread that processes its request.
Similarly, if it is a file server and a file open request is implemeted by a server thread, then is the fd a part of server file descriptor table or the calling processes.
Would be graatefull to get any link to materials I can read up. Thanks
No, the client and the server are different processes, possibly even running on different machines.
Clients will ask the operating system (through the libraries) to send network messages to servers, whose operating system will unpack them and direct them (through the libraries) into the server process.
Now "client handling threads" are a different thing, they are subcomponents of the server process, and in your setup, one of those threads (the one handling the client on the other side of the network) will receive the data and do whatever is needed (possibly including a reply, if necessary).
In the file server situation, the file descriptor provided by the operating system never "leaves" the file server. The file server clients create whatever they need to mirror the contents of the remote machine. Such mirroring might include file descriptors, but they are definitely not the same file descriptors as those that reside on the server. The client file descriptors are bound to code which takes the requested operation and turns it into a network call, while the server file descriptors (likely) access the blocks on disk directly.
'The socket server listens on a socket port and, when a message arrives, create a thread to service the request.' Nearly. In this type of server design, a new client-server thread is created, (or depooled), when a connection from a client is accepted by the server listening thread. This client-server thread is passed the client-server socket instance that is allocated by the listener thread accept() call. The client-server thread then usually reads from the client-server socket to get messages, HTTP GET/POST, whatever.
'The client code sends a given number of messages to the server. This client code could also be run from different machines by multiple users. I understand that the client code codes are run as seperate processes.Ihat is seperate processes issue requests to the server which is then processed by a server thread.' Yes-ish. There is the possibility that one client on one box may have multiple connections from multiple threads, but you are 99.99% right.
So, does a client processes stack, user address space, process control block etc pass on to the server thread that processes its request.' No! That would be an absolute nightmare!
Similarly, if it is a file server and a file open request is implemeted by a server thread, then is the fd a part of server file descriptor table or the calling processes.
The file/whatever is opened by the client-server thread. All resources/handles allocated by the client-server thread belong to the server.
Rgds, Martin
精彩评论