TCP CLIENT - SERVER COMMUNICATION
1. Client – Server communication involves
reading of text ( character or text) from the client and writing the same into
the server and server reading text and client writing the same. Following
picture depicts the same.
Functions fgets()
and fputs() are from standard I/O library. And writen() and readline() are function created by the W Richard Stevans
(WRS) (code given in the section 3.9)
The communication between client and server is
understood by Echo client and Server. The Following code corresponds to Server
side program.
Line 1: It is the header created by the WRS
which encapsulates a large number of header that are required for the functions
that are referred.
Line 2 – 3: This the definition of the main() with command line arguments.
Line 5-8 : These are variable declarations of types that are used.
Line 9 : It is the system call to the socket function that returns a
descriptor of type int.- in this case it is named as listenfd. The arguments
are family type, stream type and protocol argument – normally 0)
Line 10:
the function bzero() sets the
address space to zero.
Line 11-12: Sets the internet socket address to
wild card address and the server port to the number defined in SERV_PORT which is 9877 (specified by
WRS). It is an intimation that the server is ready to accept a connection
destined for any local interface in case the system is multi homed.
Line 14
: bind () function binds the address
specified by the address structure to the socket. Line 15: The socket is
converted into listening socket by the call to the listen()function
Line 17-18: The server blocks in the call to
accept, waiting for a client connection to complete.
Line 19 – 24: For each client, fork() spawns a child and the child handles the new client. The child closes the listening socket and the parent closes the connected socket The child then calls str_echo () to handle the client
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.