The goal of this post is to create a dummy server in c using TCP socket .The protocol between the server and client is as follows:
a)The first step is to start The server on a known port.
b) if server is started successfully Then the second step is to start client (server IP and port are provided on the command line).
c) The client connects to the server, and then asks the user for input.(e.g., "Hi", "Bye", "How are you").
d) The server reads the user's input from the client socket. If the server gets "Bye" (without the quotes), the server must reply with "Goodbye". For any other, the server must reply "OK".
e) The client then reads the reply from the server, and checks that it is accurate (either "OK" or "Goodbye").
f) If the user had typed "Bye", and the server replied with a "Goodbye" correctly, the client quits. Otherwise, the client asks the user for the next message to send to the server. If the server sends a wrong reply, say, "NO" instead of "OK", the client exits with error message.
b) if server is started successfully Then the second step is to start client (server IP and port are provided on the command line).
c) The client connects to the server, and then asks the user for input.(e.g., "Hi", "Bye", "How are you").
d) The server reads the user's input from the client socket. If the server gets "Bye" (without the quotes), the server must reply with "Goodbye". For any other, the server must reply "OK".
e) The client then reads the reply from the server, and checks that it is accurate (either "OK" or "Goodbye").
f) If the user had typed "Bye", and the server replied with a "Goodbye" correctly, the client quits. Otherwise, the client asks the user for the next message to send to the server. If the server sends a wrong reply, say, "NO" instead of "OK", the client exits with error message.
dummyServer.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<time.h>
#define MAX_SIZE 1024
#define PORT 8888
int main(int argc,char *argv[])
{
int sockfd;
socklen_t len;
pid_t childpid;
unsigned short servport;
char sbuffer[100];
char rbuffer[100];
int *new;
servport = atoi(argv[1]);
struct sockaddr_in serveraddr,clientaddr;
sockfd=socket(PF_INET,SOCK_STREAM,0);
memset(&serveraddr,'\0',sizeof(serveraddr));
serveraddr.sin_family=PF_INET;
serveraddr.sin_port=htons(servport);
serveraddr.sin_addr.s_addr=INADDR_ANY;
bind(sockfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr));
listen(sockfd,5);
printf("binding done waiting for client\n");
while(1){
len=sizeof(clientaddr);
int newsockfd=accept(sockfd,(struct sockaddr*)&clientaddr,&len);
if(newsockfd<0){
exit(1);
}
printf("Connected to....");
printf("client ip = %s\nand client port = %d\n",inet_ntoa(clientaddr.sin_addr),ntohs(clientaddr.sin_port));
if((childpid = fork()) == 0){
close(sockfd);
while(1)
{
memset(rbuffer,'\0',sizeof(rbuffer));
recv(newsockfd,rbuffer,MAX_SIZE,0);
printf("Client Message.....%s\n",rbuffer);
if(strcmp(rbuffer,":exit") == 0){
printf("Disconnected from...%s:%d\n",inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));
break;
}
else if(strcmp(rbuffer,"Bye") == 0)
{
strcpy(sbuffer,"GoodBye");
send(newsockfd,sbuffer,strlen(sbuffer),0);
//memset(&sbuffer,'\0',sizeof(sbuffer));
close(sockfd);
}
else{
//memset(&rbuffer,'\0',sizeof(rbuffer));
strcpy(sbuffer,"OK");
send(newsockfd,sbuffer,strlen(sbuffer),0);
close(sockfd);
}
}
}
}
return 0;
}
TCPClient.c
note :- Run dummy Server first with a known port number provided to command line and then run TCPClient program with providing the server port number and server ip.As given bellow in the screen shot.#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<string.h>
#define MAXSIZE 1024
int main(int argc,char **argv)
{
int sockfd,length;
char sbuffer[100];
char rbuffer[100];
unsigned short servport;
char* servip;
struct sockaddr_in serveraddr;
servport = atoi(argv[1]);
servip = argv[2];
sockfd=socket(PF_INET,SOCK_STREAM,0);
memset(&serveraddr,'\0',sizeof(serveraddr));
serveraddr.sin_family=PF_INET;
serveraddr.sin_port=htons(servport);
serveraddr.sin_addr.s_addr=inet_addr(servip);
while(1){
connect(sockfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr));
printf("Enter message... \n");
scanf("%s",sbuffer);
send(sockfd,sbuffer,strlen(sbuffer),0);
memset(rbuffer,'\0',sizeof(rbuffer));
recv(sockfd,rbuffer,MAXSIZE,0);
printf("Message from server....%s\n",rbuffer);
if((strcmp(rbuffer,"GoodBye")) == 0)
break;
else{
printf("Enter message... \n");
scanf("%s",sbuffer);
send(sockfd,sbuffer,strlen(sbuffer),0);
recv(sockfd,rbuffer,MAXSIZE,0);
}
memset(sbuffer,'\0',sizeof(sbuffer));
}
return 0;
}
Do not forget to subscribe me for latest update on my blog , follow me and grow your knowledge. thank you.
0 comments:
Post a Comment