Getting started with TCP
Before hitting head too many times to a wall, you might want to look some example code. You can find quite good example from http://www.cs.rpi.edu/courses/sysprog/sockets/sock.html
Example codes server.c and client.c should compile also in Linux if you add stdlib.h and strings.h in your #includes.
The example server can handle only a single connection. Later in the page the server is modified to handle multiple connections by forking new process for each client. When fork() is used the main program should wait signal from each client so child processes are not left as zombie processes after they stop executing. Indepedent processes for handling connection from each client can be problematic. Example of this is e.g. chat server, where messages from each connection must be relayed to other connections. If clients are connected to separate processes, then processes must communicate with each other somehow, e.g. using file or socket. Other issue is the number of processes. Two hundred users would mean two hundred processes competing from system resources.
Processing of multiple clients does not require fork. We can continue using select(). Few ideas why to use select() with a TCP server:
When we receive something to our original server socket (e.g. fd1), it is a new connection. Use accept() to process this new connection. accept() returns a new file descriptor (e.g. fd2). From now you have to be watching if you receive something in either of those file descriptors.
If you receive something from our main socket (fd1), it is a new connection request and should be handled with accept, which creates a new file descriptor again(e.g. fd3, etc.). If something arrives from other file descriptors (besides of main socket (fd1)) it is data arriving from the clients (or connection close).
Example codes server.c and client.c should compile also in Linux if you add stdlib.h and strings.h in your #includes.
The example server can handle only a single connection. Later in the page the server is modified to handle multiple connections by forking new process for each client. When fork() is used the main program should wait signal from each client so child processes are not left as zombie processes after they stop executing. Indepedent processes for handling connection from each client can be problematic. Example of this is e.g. chat server, where messages from each connection must be relayed to other connections. If clients are connected to separate processes, then processes must communicate with each other somehow, e.g. using file or socket. Other issue is the number of processes. Two hundred users would mean two hundred processes competing from system resources.
Processing of multiple clients does not require fork. We can continue using select(). Few ideas why to use select() with a TCP server:
When we receive something to our original server socket (e.g. fd1), it is a new connection. Use accept() to process this new connection. accept() returns a new file descriptor (e.g. fd2). From now you have to be watching if you receive something in either of those file descriptors.
If you receive something from our main socket (fd1), it is a new connection request and should be handled with accept, which creates a new file descriptor again(e.g. fd3, etc.). If something arrives from other file descriptors (besides of main socket (fd1)) it is data arriving from the clients (or connection close).
0 Comments:
Post a Comment
<< Home