TCP Concurrent Echo Program Using Fork and Thread: Ms - Rupilaa V.M., Ms - Sangeetha M., MR - Sathya Seelan K., MR - Vadivel R

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

International Journal on Recent and Innovation Trends in Computing and Communication ISSN: 2321-8169

Volume: 3 Issue: 4 2225 2229


_______________________________________________________________________________________________
TCP Concurrent Echo Program using Fork and Thread

Ms.Rupilaa V.M., Ms.Sangeetha M., Mr.Sathya Seelan K., Mr.Vadivel R.


Assistant Professor
Adithya Institute of Technology

Abstract In networking, client-server model plays a vital role in exchanging information between processes. Client-server model
predominantly relies on socket programming. Sockets allow communication between processes on same or different machines. Servers in the
client-server model are of two types- Iterative and Concurrent. This paper describes about the elementary socket function for TCP client/server.
An implementation of TCP Echo program for concurrent server using fork and thread is also given.

Keywords Socket, TCP, Concurrent server, fork, thread

__________________________________________________*****_________________________________________________

a. Process-based using fork


I. INTRODUCTION Spawn one server process to handle each client
connection
Rapid growth of Internet leads to the ultimate Kernel automatically interleaves multiple server
development of many net applications which use the client- processes
server model. Fig. 1 shows the client-server model. Client- Each server process has its own private address
server model allows communication between processes or space
applications to exchange some information. The client
process always initiates a connection to the server, while the Fig. 2 shows the concurrent server implemented using
server process always waits for requests from any client. fork(). In Fig. 2, client A has already established a
connection with the server, which has created a child server
process to handle the transaction. This allows the server to
process client B request, without waiting for client A to
complete

TCP/IP

Fig. 1 Client-Server Model


Client B Concurrent
Socket is used in client-server application
framework. Sockets are of four types Stream sockets,
datagram sockets, raw sockets and sequenced-packet sockets
[1]. A stream socket uses TCP as end-to-end protocol and of Client A Child server
type SOCK_STREAM. Datagram socket uses UDP as end- process
to-end protocol and of type SOCK_DGRAM. A raw socket
is of type SOCK_RAW and provides raw network protocol
access. A sequenced-packet socket is similar to stream Fig. 2 Fork Execution Model
socket, with the exception that record boundaries are
preserved and of type SOCK_SEQPACKET. b. Event-based using I/O Multiplexing
One process, one thread, but programmer manually
Servers are of two types in client-server model interleaves multiple connections
Iterative server and concurrent server. Iterative server Relies on lower-level system abstractions
handles single request at a time and are easy to implement.
Concurrent server handles multiple requests at a time but
difficult to design and build. c. Threads
Create one server thread to handle each client
Concurrent Server connection
Concurrent servers are designed using three basic Kernel automatically interleaves multiple server
mechanisms. threads
a. Process-based using fork All threads share the same address space
b. Event-based using I/O Multiplexing
c. Threads

2225

IJRITCC | April 2015, Available @ http://www.ijritcc.org


_______________________________________________________________________________________
International Journal on Recent and Innovation Trends in Computing and Communication ISSN: 2321-8169
Volume: 3 Issue: 4 2225 2229
_______________________________________________________________________________________________
close function used to close a socket and terminate a TCP
connection and included in the header file
#include<unistd.h>
Syntax: int close(int sockfd);

B. THREAD FUNCTIONS
Fig. 3 Thread Execution Model All the functions are defined in the header file
#include<pthread.h>.
Fig. 3 shows the thread execution model. The main
thread creates peer thread and handles multiple client pthread_create create a new thread.
requests. Syntax: int pthread_create(pthread_t *restrict thread, const
pthread_attr_t *restrict attr, void *(*start_routine)(void*),
This paper describes about the socket functions and void *restrict arg);
thread functions used in TCP client-server communication
in section 2. In section 3, the client-server model using pthread_self obtain ID of the calling thread.
socket function is given. Implementation of concurrent Syntax: pthread_t pthread_self(void);
server using fork and thread is given in subsequent sections.
pthread_detach detach a thread.
II. SOCKET AND THREAD FUNCTIONS Syntax: int pthread_detach(pthread_t thread);

A. SOCKET FUNCTIONS pthread_exit thread termination


All the functions used in TCP client/server Syntax: void pthread_exit(void *value_ptr);
communication is defined in the header file
#include<sys/socket.h>. III. ELEMENTARY TCP SOCKETS

socket function this function specifies the type of Fig. 4 shows a timeline of the typical scenario that
communication protocol. This function is used by both takes place between a TCP client and server. First, the
client and server. server is started, and sometime later, the client is started and
Syntax: int socket(int family, int type, int protocol); connects to the server. The client sends a request to the
server, the server processes the request, and the server sends
connect function used by a TCP client to establish a a reply back to the client. This continues until the client
connection with a TCP server. closes its end of the connection. The server then closes its
Syntax: int connect(int sockfd, const struct sockaddr end of the connection and either terminates or waits for a
*servaddr, socklen_t addrlen); new client connection [3].

bind function assigns a local protocol address to a socket.


Syntax: int bind(int sockfd, const struct sockaddr *myaddr,
socklen_t addrlen);

listen function converts an unconnected socket into a


passive socket and specifies the maximum number of
connections the kernel should queue for this socket. This
function is called only by TCP server. Syntax: int listen(int
sockfd, int backlog);

accept function return the next completed connection


from the front of the completed connection queue.
Syntax: int accept(int sockfd, struct sockaddr *cliaddr,
socklen_t *addrlen);

send function send a message on a socket.


Syntax: ssize_t send(int socket, const void *buffer, size_t
length, int flags);

recv function receive data from a connected socket.


Syntax: ssize_t recv(int socket, void *buffer, size_t length,
int flags); Fig. 4 Socket functions for elementary TCP client/server

2226

IJRITCC | April 2015, Available @ http://www.ijritcc.org


_______________________________________________________________________________________
International Journal on Recent and Innovation Trends in Computing and Communication ISSN: 2321-8169
Volume: 3 Issue: 4 2225 2229
_______________________________________________________________________________________________
IV. TCP ECHO CLIENT/SERVER close(ls);
return 0;
fgets send recv }
stdin TCP TCP
server VI. IMPLEMENTATION OF ECHO PROGRAM
stdout client send FOR CONCURRENT SERVER USING FORK
fputs recv

Fig. 5 Echo client/server #include<stdio.h>


#include<sys/socket.h>
Fig. 5 depicts the echo client/server along with the #include<sys/types.h>
functions used for input and output. An echo client/server #include<netinet/in.h>
program performs the following steps: #include<stdlib.h>
The client reads a line of text from the standard input
and writes the line to the server. void str_echo(int s)
The server reads the line from the network input and {
echoes the line back to the client. char buf[50];
The client reads the echoed line and prints it on its
standard output. //receiving data from client
V. IMPLEMENTATION OF ECHO PROGRAM recv(s,buf,50,0);
FOR CLIENT
puts("Message from Client...");
#include<stdio.h> fputs(buf,stdout);
#include<sys/socket.h> send(s,buf,50,0);
#include<sys/types.h> }
#include<netinet/in.h>
void str_echo(int s) int main()
{ {
char buf[50],buf1[50]; int ls,cs,len;
puts("Enter the Message..."); struct sockaddr_in serv,cli;
fgets(buf,50,stdin); pid_t pid;

send(s,buf,50,0); //sending data to server puts("I am Server...");

//receiving data from server //creating socket


recv(s,buf1,50,0); ls=socket(AF_INET,SOCK_STREAM,0);
puts("Message from Server..."); puts("Socket Created Successfully...");
fputs(buf1,stdout);
} //socket address structure
serv.sin_family=AF_INET;
int main() serv.sin_addr.s_addr=INADDR_ANY;
{ serv.sin_port=htons(5000);
int ls;
struct sockaddr_in cli; bind(ls,(struct sockaddr*)&serv,sizeof(serv));
puts("I am Client..."); puts("Binding Done...");

/*creating socket*/ listen(ls,3);


ls=socket(AF_INET,SOCK_STREAM,0); puts("Listening for Client...");
puts("Socket Created Successfully...");
for(; ;)
/*socket address structure*/ {
cli.sin_family=AF_INET; len=sizeof(cli);
cli.sin_addr.s_addr=inet_addr("127.0.0.1");
cli.sin_port=htons(5000); //accepting client connection
cs=accept(ls,(struct sockaddr*)&cli,&len);
/*connecting to server*/ puts("\nConnected to Client...");
connect(ls,(struct sockaddr*)&cli,sizeof(cli));
puts("Connected with Server..."); //creating child process
if((pid=fork()) == 0)
str_echo(ls); {

2227

IJRITCC | April 2015, Available @ http://www.ijritcc.org


_______________________________________________________________________________________
International Journal on Recent and Innovation Trends in Computing and Communication ISSN: 2321-8169
Volume: 3 Issue: 4 2225 2229
_______________________________________________________________________________________________
puts("Child process created..."); Fig. 6 shows the data read from client 1. Fig. 7
close(ls); shows the data read from client 2. Fig.8 shows the data
str_echo(cs); processed by concurrent server from client 1 and client 2.
close(cs);
exit(0);
}
close(cs); VII. IMPLEMENTATION OF ECHO PROGRAM
} FOR CONCURRENT SERVER USING
return 0; THREAD

} #include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<pthread.h>

void str_echo(int s)
{
char buf[20];
recv(s,buf,20,0);
puts("Message from Client...");
fputs(buf,stdout);
send(s,buf,20,0);
}
Fig. 6 Execution of Echo Client 1
static void *doit(void *arg)
{
pthread_detach(pthread_self());
str_echo((int)arg);
close((int)arg);
pthread_exit(0);
return NULL;
}

int main()
{
int ls,cs,len;
Fig. 7 Execution of Echo Client 2 struct sockaddr_in serv,cli;
pid_t pid;
pthread_t th;

puts("I am Server...");

//creating socket
ls=socket(AF_INET,SOCK_STREAM,0);
puts("Socket Created Successfully...");

//socket address structure


serv.sin_family=AF_INET;
serv.sin_addr.s_addr=INADDR_ANY;
serv.sin_port=htons(5000);

bind(ls,(struct sockaddr*)&serv,sizeof(serv));
puts("Binding Done...");

listen(ls,3);
Fig. 8 Execution of Concurrent Echo Server puts("Listening for Client...");

2228

IJRITCC | April 2015, Available @ http://www.ijritcc.org


_______________________________________________________________________________________
International Journal on Recent and Innovation Trends in Computing and Communication ISSN: 2321-8169
Volume: 3 Issue: 4 2225 2229
_______________________________________________________________________________________________
for(; ;) VIII. CONCLUSION
{
len=sizeof(cli); This paper describes about the elementary socket
cs=accept(ls,(struct sockaddr*)&cli,&len); and thread functions needed for the implementation of
puts("Connected to Client..."); concurrent server. Echo client/server program for concurrent
server using fork and thread is implemented and shown
//creating thread along with the execution. Synchronization problem exist
pthread_create(&th,NULL,&doit,(void *)cs); with threads which can be overcome by using mutex and
condition variables. To expand the echo client/server into
} any application, change what the server does with the input
return 0; it receives from its clients.

} REFERENCES

[1] W. Richard Stevens, Unix Network Programming


Vol-I, Second Edition, Pearson Education, 1998.
[2] D.E. Comer, Internetworking with TCP/IP Vol -
III, (BSD Sockets Version), Second Edition,
Pearson Education, 200 UNIT III
[3] Michael J. Donahoo and Kenneth L. Calvert,
"TCP/IP Sockets in C: Practical Guide for
Programmers", Second Edition, Morgan
Kaufmann, 2001.
[4] Stefan Bocking,Socket++: A Uniform Application
Programming Interface for Basic-Level
Communication Services, IEEE Communication
Fig. 9 Execution of Echo Client 1 Magazine December 1996.
[5] Mattew Cook and Syed(shawon)M. Rahman, Java
and C/C++ language feature in terms of Network
Programming, 2005.
[6] https://computing.llnl.gov/tutorials/pthreads/
[7] http://pubs.opengroup.org/onlinepubs/7908799/xsh
/pthread.h.html

Fig. 10 Execution of Echo Client 2

Fig. 11 Execution of Concurrent Echo Server

2229

IJRITCC | April 2015, Available @ http://www.ijritcc.org


_______________________________________________________________________________________

You might also like