Commit 37accc82 authored by Roshan Rabinarayan's avatar Roshan Rabinarayan

messages added

parent dbb1720f
...@@ -9,66 +9,96 @@ send GET , PUT , and DELETE requests to the server process. It will have a main ...@@ -9,66 +9,96 @@ send GET , PUT , and DELETE requests to the server process. It will have a main
client library interfaces to do the actual operations. client library interfaces to do the actual operations.
*/ */
#include <netdb.h>
#include <stdio.h> #include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h>
#include "KVMessageFormat.h"
#include <arpa/inet.h>
#define MAX 80 #define MAX 80
#define PORT 8080 #define PORT 8080
#define SA struct sockaddr #define SA struct sockaddr
struct message *request(char status,char* key,char* value)
{
if(!status ||(key==NULL && value==NULL) )
{
printf("Invalid parameters in request()");
return NULL;
}
struct message *requestMessage= malloc(sizeof(struct message));
requestMessage->status=status;
memcpy(requestMessage->key,key,256); //copied the complete key
if(strlen(requestMessage->key)<256)
{
requestMessage->key[strlen(requestMessage->key)]='\0';
}
if(value!=NULL)
{
memcpy(requestMessage->value,value,256); //copied the complete value
}
if(strlen(requestMessage->value)<256) //to pad with \0
{
requestMessage->value[strlen(requestMessage->value)]='\0';
}
printf("[Message Generated at Client]\n[[Status:%c]\n[Key:%s]\n[Value:%s]]",requestMessage->status,requestMessage->key,requestMessage->value);
return requestMessage;
}
struct message* requestMessage;
void func(int sockfd) void func(int sockfd)
{ {
char buff[MAX]; char buff[MAX];
int n; int n;
for (;;) {
bzero(buff, sizeof(buff)); bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0; n = 0;
while ((buff[n++] = getchar()) != '\n') char message[256]="helloworld";
; requestMessage = request('g',message,NULL);
write(sockfd, buff, sizeof(buff)); send(sockfd , requestMessage , sizeof(struct message), 0 );
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
} }
int main() int main(int argc, char const *argv[])
{ {
int sockfd, connfd; int sock = 0, valread;
struct sockaddr_in servaddr, cli; struct sockaddr_in serv_addr;
char *hello = "Hello from client";
// socket create and varification char buffer[1024] = {0};
sockfd = socket(AF_INET, SOCK_STREAM, 0); if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
if (sockfd == -1) { {
printf("socket creation failed...\n"); printf("\n Socket creation error \n");
exit(0); return -1;
} }
else
printf("Socket successfully created..\n"); serv_addr.sin_family = AF_INET;
bzero(&servaddr, sizeof(servaddr)); serv_addr.sin_port = htons(PORT);
// assign IP, PORT // Convert IPv4 and IPv6 addresses from text to binary form
servaddr.sin_family = AF_INET; if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); {
servaddr.sin_port = htons(PORT); printf("\nInvalid address/ Address not supported \n");
return -1;
// connect the client socket to server socket
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
printf("connection with the server failed...\n");
exit(0);
} }
else
printf("connected to the server..\n"); if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
// function for chat printf("\nConnection Failed \n");
func(sockfd); return -1;
}
// close the socket char key[256]="abc";
close(sockfd); char value[256]="cde";
struct message *requestMessage= request('g',key,value);
struct message *replyMessage=malloc(sizeof(struct message));
send(sock , requestMessage ,sizeof(struct message) , 0 );
printf("Hello message sent\n");
valread = read( sock , replyMessage, sizeof(struct message));
printf("[Message Received from client]\n[[Status:%c]\n[Key:%s]\n[Value:%s]]",replyMessage->status,replyMessage->key,replyMessage->value);
return 0;
} }
\ No newline at end of file
...@@ -18,3 +18,8 @@ Error: 400 (with the appropriate error message) ...@@ -18,3 +18,8 @@ Error: 400 (with the appropriate error message)
Reasons for error could be GET key not found, DEL key not found etc. Reasons for error could be GET key not found, DEL key not found etc.
*/ */
struct message{
char status;
char key[256];
char value[256];
};
...@@ -63,8 +63,10 @@ error. (Assume each character to be 1 byte in size) ...@@ -63,8 +63,10 @@ error. (Assume each character to be 1 byte in size)
#include <netinet/in.h> #include <netinet/in.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include "KVMessageFormat.h"
#define MAX 80 #define MAX 80
#define PORT 8080 #define PORT 8080
#define SA struct sockaddr #define SA struct sockaddr
...@@ -72,95 +74,74 @@ error. (Assume each character to be 1 byte in size) ...@@ -72,95 +74,74 @@ error. (Assume each character to be 1 byte in size)
// Function designed for chat between client and server. // Function designed for chat between client and server.
void func(int sockfd) void func(int sockfd)
{ {
char buff[MAX]; printf("receive functions:");
char buff[ sizeof(struct message)];
int n; int n;
struct message* requestMessage=malloc(sizeof(struct message));
// infinite loop for chat // infinite loop for chat
for (;;) { // read the message from client and copy it in buffer
bzero(buff, MAX); read( sockfd , buff, sizeof(struct message));
// read the message from client and copy it in buffer
read(sockfd, buff, sizeof(buff));
// print buffer which contains the client contents // print buffer which contains the client contents
printf("From client: %s\t To client : ", buff); printf("From client: %c\t To client : ", buff[0]);
bzero(buff, MAX);
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != '\n')
;
// and send that buffer to client
write(sockfd, buff, sizeof(buff));
// if msg contains "Exit" then server exit and chat ended.
if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...\n");
break;
}
}
} }
// Driver function // Driver function
int main() int main(int argc, char const *argv[])
{ {
int sockfd, connfd, len; int server_fd, new_socket, valread;
struct sockaddr_in servaddr, cli; struct sockaddr_in address;
int opt = 1;
// socket create and verification int addrlen = sizeof(address);
sockfd = socket(AF_INET, SOCK_STREAM, 0); char buffer[1024] = {0};
if (sockfd == -1) { char *hello = "Hello from server";
printf("socket creation failed...\n");
exit(0); // Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
} }
else
printf("Socket successfully created..\n"); // Forcefully attaching socket to the port 8080
bzero(&servaddr, sizeof(servaddr)); if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
// assign IP, PORT {
servaddr.sin_family = AF_INET; perror("setsockopt");
servaddr.sin_addr.s_addr = htonl(INADDR_ANY); exit(EXIT_FAILURE);
servaddr.sin_port = htons(PORT);
// Binding newly created socket to given IP and verification
if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n");
exit(0);
} }
else address.sin_family = AF_INET;
printf("Socket successfully binded..\n"); address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
// Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0) { // Forcefully attaching socket to the port 8080
printf("Listen failed...\n"); if (bind(server_fd, (struct sockaddr *)&address,
exit(0); sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
} }
else if (listen(server_fd, 3) < 0)
printf("Server listening..\n"); {
len = sizeof(cli); perror("listen");
exit(EXIT_FAILURE);
// Accept the data packet from client and verification
while(1)
{ //
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd < 0)
{
printf("server acccept failed...\n");
exit(0);
}
else
printf("server acccept the client...\n");
}
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd < 0) {
printf("server acccept failed...\n");
exit(0);
} }
else if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
printf("server acccept the client...\n"); (socklen_t*)&addrlen))<0)
{
// Function for chatting between client and server perror("accept");
func(connfd); exit(EXIT_FAILURE);
}
// After chatting close the socket struct message *requestMessage= malloc(sizeof(struct message));
close(sockfd); valread = read( new_socket , requestMessage, sizeof(struct message));
printf("[Message Received from client]\n[[Status:%c]\n[Key:%s]\n[Value:%s]]",requestMessage->status,requestMessage->key,requestMessage->value);
struct message *replyMessage=malloc(sizeof(struct message));
replyMessage->status='0';
char buff[256]="success";
memcpy(replyMessage->key,buff,256);
replyMessage->value[0]='\0';
send(new_socket , replyMessage , sizeof(struct message) , 0 );
printf("\n[Message sent to client]\n[[Status:%c]\n[Key:%s]\n[Value:%s]]",replyMessage->status,replyMessage->key,replyMessage->value);
return 0;
} }
\ No newline at end of file
all: KVClient.c KVServer.c all: KVClient.c KVServer.c KVMessageFormat.h
gcc KVServer.c -o server gcc KVServer.c -o server
gcc KVClient.c -o client gcc KVClient.c -o client
\ No newline at end of file
File added
No preview for this file type
No preview for this file type
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include "KVMessageFormat.h"
struct message* get(char m,char *key)
{
if(!m || key==NULL )
{
printf("Invalid parameters in get()");
return NULL;
}
struct message *request= malloc(sizeof(struct message));
request->status=m;
memcpy(request->key,key,256); //copied the complete key
if(strlen(request->key)<256)
{
request->key[strlen(request->key)]='\0';
}
request->value[0]='\0';
return request;
}
struct message* put(char m,char *key,char *value)
{
if(!m || (key==NULL && value==NULL))
{
printf("Invalid parameters in put()");
return NULL;
}
struct message *request= malloc(sizeof(struct message));
request->status=m;
memcpy(request->key,key,256); //copied the complete key
if(strlen(request->key)<256)
{
request->key[strlen(request->key)]='\0';
}
memcpy(request->value,value,256); //copied the complete value
if(strlen(request->key)<256) //to pad with \0
{
request->key[strlen(request->value)]='\0';
}
return request;
}
struct message *del(char m,char *key){
if(!m || key==NULL )
{
printf("Invalid parameters in del()");
return NULL;
}
struct message *request= malloc(sizeof(struct message));
request->status=m;
memcpy(request->key,key,256); //copied the complete key
if(strlen(request->key)<256)
{
request->key[strlen(request->key)]='\0';
}
request->value[0]='\0';
return request;
}
struct message *request(char status,char* key,char* value)
{
if(!status ||(key==NULL && value==NULL) )
{
printf("Invalid parameters in request()");
return NULL;
}
struct message *requestMessage= malloc(sizeof(struct message));
requestMessage->status=status;
memcpy(requestMessage->key,key,256); //copied the complete key
if(strlen(requestMessage->key)<256)
{
requestMessage->key[strlen(requestMessage->key)]='\0';
}
if(value!=NULL)
{
memcpy(requestMessage->value,value,256); //copied the complete value
}
if(strlen(requestMessage->value)<256) //to pad with \0
{
requestMessage->value[strlen(requestMessage->value)]='\0';
}
printf("[Message Generated at Client]\n[[Status:%c]\n[Key:%s]\n[Value:%s]]",requestMessage->status,requestMessage->key,requestMessage->value);
return requestMessage;
}
void main()
{
char message[256];
for(int i=0;i<30;i++)
{
message[i]='a';
}
message[sizeof(message)]='\0';
request('g',message,message);
//creating a message
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment