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
client library interfaces to do the actual operations.
*/
#include <netdb.h>
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "KVMessageFormat.h"
#include <arpa/inet.h>
#define MAX 80
#define PORT 8080
#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)
{
char buff[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
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;
}
}
char message[256]="helloworld";
requestMessage = request('g',message,NULL);
send(sockfd , requestMessage , sizeof(struct message), 0 );
}
int main()
int main(int argc, char const *argv[])
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
// socket create and varification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
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);
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
else
printf("connected to the server..\n");
char key[256]="abc";
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");
// function for chat
func(sockfd);
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);
// close the socket
close(sockfd);
return 0;
}
\ No newline at end of file
......@@ -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.
*/
struct message{
char status;
char key[256];
char value[256];
};
......@@ -63,8 +63,10 @@ error. (Assume each character to be 1 byte in size)
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "KVMessageFormat.h"
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
......@@ -72,95 +74,74 @@ error. (Assume each character to be 1 byte in size)
// Function designed for chat between client and server.
void func(int sockfd)
{
char buff[MAX];
printf("receive functions:");
char buff[ sizeof(struct message)];
int n;
struct message* requestMessage=malloc(sizeof(struct message));
// infinite loop for chat
for (;;) {
bzero(buff, MAX);
// read the message from client and copy it in buffer
read(sockfd, buff, sizeof(buff));
read( sockfd , buff, sizeof(struct message));
// print buffer which contains the client contents
printf("From client: %s\t To client : ", buff);
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;
}
}
printf("From client: %c\t To client : ", buff[0]);
}
// Driver function
int main()
int main(int argc, char const *argv[])
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
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);
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "Hello from server";
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
else
printf("Socket successfully binded..\n");
// Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...\n");
exit(0);
}
else
printf("Server listening..\n");
len = sizeof(cli);
// Accept the data packet from client and verification
while(1)
{ //
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd < 0)
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
printf("server acccept failed...\n");
exit(0);
perror("setsockopt");
exit(EXIT_FAILURE);
}
else
printf("server acccept the client...\n");
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd < 0) {
printf("server acccept failed...\n");
exit(0);
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
else
printf("server acccept the client...\n");
// Function for chatting between client and server
func(connfd);
// After chatting close the socket
close(sockfd);
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
struct message *requestMessage= malloc(sizeof(struct message));
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 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