Senin, 22 September 2014

IMPLEMENTATION OF TCP (Transmission Control Protocol) Source Code in C Language

PROGRAM:



CLIENT ( Type This cilent Program in The Editor ):



#include

#include

#include

#include

#include

#include

#include

int main(int argc,char **argv)

{

struct sockaddr_in saddr;

struct hostent *server;

int n,i,ssid,csid,pid;

char buffer[1024];

if(argc<2)

fprintf(stderr,"port # not specified\n");

csid=socket(AF_INET,SOCK_STREAM,0);

if(csid<0)

perror("socket failed error");

bzero((char*)&saddr,sizeof(saddr));

server=gethostbyname(argv[1]);

saddr.sin_family=AF_INET;

saddr.sin_port=htons(atoi(argv[2]));

bcopy((char*)server->h_addr,(char*)&saddr.sin_addr.s_addr,server->h_length);

ssid=connect(csid,(struct sockaddr*)&saddr,sizeof(saddr));

if(ssid<0)

perror("socket connect error");

bzero(buffer,1024);

printf("Type msg to server");

fgets(buffer,1024,stdin);

n=write(csid,buffer,sizeof(buffer));

if(n==0)

perror("Socket write error");

n=read(csid,buffer,sizeof(buffer));

perror("Socket read buffer");

printf("msg from server :\n");

for(i=0;i
printf("%c",buffer[i]);

return 0;

}





SERVER:



#include

#include

#include

#include

#include

int main(int argc,char **argv)

{

struct sockaddr_in saddr,caddr;

int n,len,ssid,csid,pid;

char buffer[1024];

if(argc<2)

fprintf(stderr,"port # not specified\n");

ssid=socket(AF_INET,SOCK_STREAM,0);

if(ssid<0)

perror("Socket failed error");

bzero((char*)&saddr,sizeof(saddr));

saddr.sin_family=AF_INET;

saddr.sin_port=htons(atoi(argv[1]));

saddr.sin_addr.s_addr=INADDR_ANY;

if(bind(ssid,(struct sockaddr*)&saddr,sizeof(saddr))<0)

perror("Socket bind error");

listen(ssid,5);

len=sizeof(caddr);

csid=accept(ssid,(struct sockaddr*)&caddr,&len);

if(csid<0)

perror("socket accept error");

bzero(buffer,1024);

n=read(csid,buffer,1024);

if(n==0)

perror("Socket read error");

printf("msg from client:%s\n",buffer);

n=write(csid,buffer,1024);

if(n<0)

perror("write error");

return 0;

}



















OUTPUT:



IN SERVER:

[testciet @linuxcentre ~]$ cc tcpserver.c

[testciet @linuxcentre ~]$ ./a.out 1678

Msg from client: hi



IN CLIENT:

[testciet @linuxcentre ~]$ cc tcpclient.c

[testciet @linuxcentre ~]$ ./a.out localhost 1678

Type msg to server:hi

Socket read buffer: Success

msg from server :

hi





Keywords :

CIET college Programs,LAB Programs for Engineering Students,Computer Networks LAB Programs,Remoboys,karthik,Remokn,Student3k,TCP programs source code

0 komentar:

Posting Komentar

Blog Archive