Hello friends this post contains a server program and a client program using TCP socket in c .In this client send a string to the server and server reverse that string and resend to the client .
Question :
The server and client processes should be made to run on different machines. Use TCP Sockets.
revserver.c
#include<stdio.h>
#include<ctype.h>
#include <netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<unistd.h>
#define BUF_SIZE 1000
char* stringReverse(char *str){
char ch;
int length=strlen(str);
for(int i=0;i<length/2;i++)
{
ch=str[i];
str[i]=str[--length];
str[length]=ch;
}
return str;
}
int main(int argc , char *argv[])
{
char sbuffer[BUF_SIZE],rbuffer[BUF_SIZE];
unsigned int len;
int sockfd , newsockfd;
unsigned short servport;
servport = atoi(argv[1]);
struct sockaddr_in serveraddress , clientaddress;
sockfd = socket(PF_INET , SOCK_STREAM,0);
serveraddress.sin_family = PF_INET;
serveraddress.sin_port = htons(servport);
serveraddress.sin_addr.s_addr = INADDR_ANY;
bind(sockfd,(struct sockaddr *)&serveraddress,sizeof(serveraddress));
listen(sockfd,5);
printf("binding done Waiting for client:......\n");
while(1)
{
len = sizeof(clientaddress);
newsockfd = accept(sockfd , (struct sockaddr *) &clientaddress , &len);
printf("connected to the client IP = %s \n and port number = %d\n",inet_ntoa(clientaddress.sin_addr),ntohs(clientaddress.sin_port));
while(newsockfd)
{
recv(newsockfd,rbuffer,BUF_SIZE,0);
printf("client message = %s\n",rbuffer);
char *ptr = stringReverse(rbuffer);
strcpy(sbuffer , ptr);
send(newsockfd,sbuffer,strlen(sbuffer),0);
printf("send message = %s\n",sbuffer);
memset(rbuffer,'\0',strlen(sbuffer));
memset(sbuffer,'\0',strlen(rbuffer));
}
}
return 0;
}
revclient.c
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<ctype.h>
#include<stdlib.h>
#include<netinet/in.h>
#define BUF_SIZE 1000
int main(int argc , char *argv[])
{
int sockfd;
char sbuffer[BUF_SIZE], rbuffer[BUF_SIZE];
unsigned int servport;
char* servip;
servport = atoi(argv[1]);
servip = argv[2];
struct sockaddr_in serveraddress;
sockfd = socket(PF_INET,SOCK_STREAM,0);
serveraddress.sin_port = htons(servport);
serveraddress.sin_family = PF_INET;
serveraddress.sin_addr.s_addr = inet_addr(servip);
while(1)
{
int con = connect(sockfd,(struct sockaddr *) &serveraddress,sizeof(serveraddress));
printf("Enter string....\n");
scanf("%s",sbuffer);
if(sbuffer)
{
send(sockfd,sbuffer,strlen(sbuffer),0);
printf("send message = %s\n",sbuffer);
recv(sockfd,rbuffer,BUF_SIZE,0);
printf("server message = %s\n",rbuffer);
memset(rbuffer,'\0',strlen(rbuffer));
memset(sbuffer,'\0',strlen(sbuffer));
}
}
return 0;
}output:
Other questions of socket programming incSimple socket programming echo server and client
Calculating Server using TCP Sockets in c
Looking for the Best Minecraft Server to play on? Want to advertise your minecraft server? Find them right here on our Minecraft Multiplayer Servers List. Cheap Minecraft Advertising
ReplyDelete