TCP Concurrent Echo Program Using Fork and Thread: Ms - Rupilaa V.M., Ms - Sangeetha M., MR - Sathya Seelan K., MR - Vadivel R
TCP Concurrent Echo Program Using Fork and Thread: Ms - Rupilaa V.M., Ms - Sangeetha M., MR - Sathya Seelan K., MR - Vadivel R
TCP Concurrent Echo Program Using Fork and Thread: Ms - Rupilaa V.M., Ms - Sangeetha M., MR - Sathya Seelan K., MR - Vadivel R
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.
__________________________________________________*****_________________________________________________
TCP/IP
2225
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);
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].
2226
2227
} #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...");
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
} REFERENCES
2229