Network Programming CSC-341: Instructor: Junaid Tariq, Lecturer, Department of Computer Science
Network Programming CSC-341: Instructor: Junaid Tariq, Lecturer, Department of Computer Science
CSC- 341
Instructor:
Junaid Tariq,
Lecturer,
Department of Computer Science
Lecture
18
PART 2
TCP CLIENT-SERVER
EXAMPLE
CHAPTER 5
Contents
Introduction
TCP Echo Server
TCP Echo Client
Normal Startup and Termination
Posix Signal Handling
Handling SIGCHLD Signals
Data Format
and so on...
INTRODUCTION
fgets
stdin writen readline
TCP TCP
stdout client readline writen server
fputs
1. The Client reads a line of text from its standard input and
writes the line to the server.
2. The server reads the line from its network input and echoes
the line back to the client.
3. The client reads the echoed line and prints it on its standard
output.
INTRODUCTION
Besides running our client and server in their normal mode, we
examine lots of boundary conditions for this example:
What happens when the client and server are started;
When the server starts, it calls socket, bind, listen, and accept,
blocking in the call to accept.
NORMAL STARTUP
Before starting the client, we run the netstat program to verify
the state of the server's listening socket.
The client calls socket and connect, the latter causing TCP's three-
way handshake to take place.
When the three-way handshake completes, connect returns in the client and
accept returns in the server. The connection is established. The following
steps then take place:
1. The client calls str_cli, which will block in the call to fgets, because we have not
typed a line of input yet.
2. When accept returns in the server, it calls fork and the child calls str_echo. This
function calls read, which blocks while waiting for a line to be sent from the client.
3. The server parent, on the other hand, calls accept again, and blocks while waiting
for the next client connection.
NORMAL STARTUP
We have three processes, and all three are asleep (blocked):
client, server parent, and server child.
Since we are running the client and server on the same host,
netstat now shows two additional lines of output,
corresponding to the TCP connection:
NORMAL STARTUP
We can also use the ps command to check the status and
relationship of these processes.
NORMAL
TERMINATION
NORMAL TERMINATION
At this point, the connection is established and whatever we type
to the client is echoed back.