4. A c.program to create TCP program through which a client send an integer and the server will reverse the number and forward it to the client.

/*A SERVER CODE TO SORT AN ARRAY AND SEND IT TO THE CLIENT*/
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<string.h>


int main(){
int sock,len,k,lis,acc,sent;
char line[100],line1[100],line2[100];
int num1,digit=0,rev=0,n;
char buf[]="welcome to socket Programming";
unsigned int rec;
struct sockaddr_in server,client;


sock=socket(AF_INET,SOCK_STREAM,0);

if(sock==-1){
perror("Socket\n");
exit(1);
}
server.sin_family=AF_INET;
server.sin_port=htons(6000);
server.sin_addr.s_addr=INADDR_ANY;

bzero(&server.sin_zero,8);

len=sizeof(struct sockaddr_in);
k=bind(sock,(struct sockaddr *)&server,sizeof(server));
if(k==-1){
perror("bind\n");
exit(1);
}

lis=listen(sock,5);
if(lis==-1){
perror("listen\n");
exit(1);
}

while(1){

printf("waiting for client connection on port 6000\n");
acc=accept(sock,(struct sockaddr *)&client,&len);
if(acc==-1){
perror("accept\n");
exit(1);
}


printf("Passing Information to the client............................\n");

memset(line,0x0,100);

n=recv(acc,line,100,0);
num1=atoi(line);

while(num1!=0){
rev=rev*10;
rev=rev+(num1%10);
num1=num1/10;
}


sprintf(line1,"%d\n",rev);

send(acc,line1,strlen(line1),0);


close(acc);
return 0;

}
}

4.2 CLIENT CODE FOR THE ABOVE SERVER PROGRAM

/*COMPUTER NETWORK OF THE CLIENT PROCESS*/



#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int main()
{
  int i,sockfd,n;
  char buf[100];
  char line[100],num2[200],sum[200];
  struct sockaddr_in sa_addr;

  sockfd=socket(AF_INET,SOCK_STREAM,0);


  sa_addr.sin_family=AF_INET;
  sa_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
  sa_addr.sin_port=htons(6000);

  bzero(sa_addr.sin_zero,8);


  i=connect(sockfd,(struct sockaddr *)&sa_addr,sizeof(sa_addr));
printf("connection Established successfully continue your operation\n");

/* After connection, the client can send or receive messages.*/
do{
printf("enter the number to be reversed: ");
scanf("%s",line);
send(sockfd,line,strlen(line)+1,0); 

n=recv(sockfd,line,100,0);
        printf("Reverse number received from server=%s\n",line);
}

while(strcpy(line,"quite"));
	
close(sockfd);

 return 0;
}

Please leave your comment

share and subscribe our site if you get some thing good.

Published by temamhashim

This is Temam Hashim certified Software Engineer from top leading University with highest score with full capablity of various top leading programming language as a software Engineer and ability to deal with logic and mathimatical problem.

Leave a comment

Design a site like this with WordPress.com
Get started