0% found this document useful (0 votes)
53 views4 pages

Pace Institute of Technology&Sciences CSE Dept

The document describes a TCP client-server application to reverse a string. The client program connects to the server, writes a string, and reads the reversed string back. The server program binds to a port, accepts the client connection, reads the input string, reverses it by iterating through the string in reverse order, and writes the reversed string back to the client. When run, the client program sends "hello" to the server, which responds with "olleh", demonstrating that the string was successfully reversed.

Uploaded by

veerusite
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views4 pages

Pace Institute of Technology&Sciences CSE Dept

The document describes a TCP client-server application to reverse a string. The client program connects to the server, writes a string, and reads the reversed string back. The server program binds to a port, accepts the client connection, reads the input string, reverses it by iterating through the string in reverse order, and writes the reversed string back to the client. When run, the client program sends "hello" to the server, which responds with "olleh", demonstrating that the string was successfully reversed.

Uploaded by

veerusite
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Pace Institute of Technology&Sciences

WEEK 5 Design TCP client-server application to reverse a string. Client program: #include<netinet/in.h> #include<sys/stat.h> #include<stdio.h> #include<string.h> #include<arpa/inet.h> main() { int sid,i=0; char buf[20]; char s[16]="192.168.1.213"; struct sockaddr_in servaddr,cliaddr; sid=socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(1400); servaddr.sin_addr.s_addr=inet_addr(s); connect(sid,(struct sockaddr*)&servaddr,sizeof(servaddr)); bzero(buf,20); write(sid,"hello",5);
R.Anitha

CSE Dept

Page 1

Pace Institute of Technology&Sciences


read(sid,buf,5); puts("reverse:"); puts(buf); return 0; } OUTPUT: $ cc client.c $ ./a.out reverse: olleh

CSE Dept

Server program: #include<netinet/in.h> #include<sys/stat.h> #include<stdio.h> #include<string.h> main() { int sid,c_sid,c_len,l,i=0,k; char buf[20],c,buf1[20]; char s[16]="192.168.1.213"; struct sockaddr_in servaddr,cliaddr;
R.Anitha Page 2

Pace Institute of Technology&Sciences


sid=socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(1400); servaddr.sin_addr.s_addr=htonl(s); bind(sid,(struct sockaddr*)&servaddr,sizeof(servaddr)); listen(sid,5); c_len=sizeof(cliaddr); c_sid=accept(sid,(struct sockaddr*)&cliaddr,&c_len); read(c_sid,buf,5); buf[5]='\0'; for(k=4;k>=0;k--) { buf1[i]=buf[k]; i++; } buf1[i]='\0'; write(c_sid,buf1,i); return 0; } OUTPUT $ cc server.c
R.Anitha

CSE Dept

Page 3

Pace Institute of Technology&Sciences


$ ./a.out

CSE Dept

R.Anitha

Page 4

You might also like