The problem is to implement an application in c using TCP socket server and client that communicate with each other via sending string .The program must have following feature .
a) The client program sends a request message to the server, identifying itself (Its IP address, and port)
b) The server then sends a line of text to the client containing a random number of characters.
c) The client sends back a number equal to the number of characters in the line of text and closes its end of the connection. So if the server sent 132 bytes of text, the client would send ``132''.
The server sends back a response indicating whether the client's input was correct
b) The server then sends a line of text to the client containing a random number of characters.
c) The client sends back a number equal to the number of characters in the line of text and closes its end of the connection. So if the server sent 132 bytes of text, the client would send ``132''.
The server sends back a response indicating whether the client's input was correct
server.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 5000
#define PORT 8888
char *randstring(size_t length) {
static char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!";
char *randomString = NULL;
if (length) {
randomString = malloc(sizeof(char) * (length +1));
if (randomString) {
for (int n = 0;n < length;n++) {
int key = rand() % (int)(sizeof(charset) -1);
randomString[n] = charset[key];
}
randomString[length] = '\0';
}
}
return randomString;
}
int main(int argc,char *argv[])
{
int sockfd;
socklen_t len;
unsigned short servport;
char sbuffer[5000];
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);
while(1){
len=sizeof(clientaddr);
int newsockfd=accept(sockfd,(struct sockaddr*)&clientaddr,&len);
printf("Connected to....");
printf("client ip = %s\nand client port = %d\n",inet_ntoa(clientaddr.sin_addr),ntohs(clientaddr.sin_port));
char *s;
int num;
srand(time(0));
for(int i=0;i<1;i++){
num = rand()%50 + 500;
}
s= randstring(num);
puts(s);
strcpy(sbuffer,s);
send(newsockfd,sbuffer,strlen(sbuffer),0);
recv(newsockfd,rbuffer,MAX_SIZE,0);
printf(" value received from client = %d\n",atoi(rbuffer));
if(strlen(sbuffer) == atoi(rbuffer))
{
strcpy(sbuffer,"correct");
send(newsockfd,sbuffer,strlen(sbuffer),0);
}
else
{
strcpy(sbuffer,"NO");
send(newsockfd,sbuffer,strlen(sbuffer),0);
}
}
return 0;
}
client.c
#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 6000
int main(int argc,char **argv)
{
int sockfd,length;
char sbuffer[5000];
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);
connect(sockfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr));
recv(sockfd,sbuffer,MAXSIZE,0);
printf("\n\nMessage from server side....%s\n",sbuffer);
length = strlen(sbuffer);
sprintf(rbuffer,"%d",length);
send(sockfd,rbuffer,strlen(rbuffer),0);
int num = atoi(rbuffer);
printf("send value is %d\n",num);
memset(sbuffer,'\0',sizeof(sbuffer));
recv(sockfd,sbuffer,MAXSIZE,0);
printf("\n\nMessage from server side....%s\n",sbuffer);
return 0;
}
note : open your terminal and run server program first by providing a port no. greater than 1024 and then run client by providing the server ip and port no. as given bellow in the screen-shot .
0 comments:
Post a Comment