TCP Concurrent Echo Program Using Fork and Thread
TCP Concurrent Echo Program Using Fork and Thread
TCP Concurrent Echo Program Using Fork and Thread
Volume: 3 Issue: 4
ISSN: 2321-8169
2225 2229
_______________________________________________________________________________________________
__________________________________________________*****_________________________________________________
I.
INTRODUCTION
Client B
Client A
Concurrent
Child server
process
c. Threads
Create one server thread to handle each client
connection
Kernel automatically interleaves multiple server
threads
All threads share the same address space
2225
IJRITCC | April 2015, Available @ http://www.ijritcc.org
_______________________________________________________________________________________
ISSN: 2321-8169
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);
A. SOCKET FUNCTIONS
All the functions used in TCP client/server
communication is defined in the header file
#include<sys/socket.h>.
socket function this function specifies the type of
communication protocol. This function is used by both
client and server.
Syntax: int socket(int family, int type, int protocol);
connect function used by a TCP client to establish a
connection with a TCP server.
Syntax: int connect(int sockfd, const struct sockaddr
*servaddr, socklen_t addrlen);
B. THREAD FUNCTIONS
All the functions are defined in the header file
#include<pthread.h>.
pthread_create create a new thread.
Syntax: int pthread_create(pthread_t *restrict thread, const
pthread_attr_t *restrict attr, void *(*start_routine)(void*),
void *restrict arg);
pthread_self obtain ID of the calling thread.
Syntax: pthread_t pthread_self(void);
pthread_detach detach a thread.
Syntax: int pthread_detach(pthread_t thread);
pthread_exit thread termination
Syntax: void pthread_exit(void *value_ptr);
III.
2226
IJRITCC | April 2015, Available @ http://www.ijritcc.org
_______________________________________________________________________________________
ISSN: 2321-8169
2225 2229
_______________________________________________________________________________________________
IV.
close(ls);
return 0;
fgets
stdin
TCP
stdout
client
fputs
send
recv
recv
send
}
TCP
VI.
server
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<stdlib.h>
void str_echo(int s)
{
char buf[50];
//receiving data from client
recv(s,buf,50,0);
puts("Message from Client...");
fputs(buf,stdout);
send(s,buf,50,0);
}
int main()
{
int ls,cs,len;
struct sockaddr_in serv,cli;
pid_t pid;
puts("I am Server...");
//creating socket
ls=socket(AF_INET,SOCK_STREAM,0);
puts("Socket Created Successfully...");
}
int main()
{
int ls;
struct sockaddr_in cli;
puts("I am Client...");
/*creating socket*/
ls=socket(AF_INET,SOCK_STREAM,0);
puts("Socket Created Successfully...");
/*socket address structure*/
cli.sin_family=AF_INET;
cli.sin_addr.s_addr=inet_addr("127.0.0.1");
cli.sin_port=htons(5000);
/*connecting to server*/
connect(ls,(struct sockaddr*)&cli,sizeof(cli));
puts("Connected with Server...");
str_echo(ls);
_______________________________________________________________________________________
ISSN: 2321-8169
2225 2229
_______________________________________________________________________________________________
puts("Child process created...");
close(ls);
str_echo(cs);
close(cs);
exit(0);
}
close(cs);
}
return 0;
}
VII.
#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
listen(ls,3);
puts("Listening for Client...");
2228
_______________________________________________________________________________________
ISSN: 2321-8169
2225 2229
_______________________________________________________________________________________________
for(; ;)
{
len=sizeof(cli);
cs=accept(ls,(struct sockaddr*)&cli,&len);
puts("Connected to Client...");
//creating thread
pthread_create(&th,NULL,&doit,(void *)cs);
}
return 0;
}
VIII.
CONCLUSION
2229
IJRITCC | April 2015, Available @ http://www.ijritcc.org
_______________________________________________________________________________________