Commit 2608a56a authored by Samarth Joshi's avatar Samarth Joshi

Merging thread code

parents 4acc3128 37accc82
......@@ -9,3 +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 <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;
bzero(buff, sizeof(buff));
n = 0;
char message[256]="helloworld";
requestMessage = request('g',message,NULL);
send(sockfd , requestMessage , sizeof(struct message), 0 );
}
int main(int argc, char const *argv[])
{
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;
}
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;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
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");
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)
Reasons for error could be GET key not found, DEL key not found etc.
*/
struct message{
char status;
char key[256];
char value[256];
};
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#define _GNU_SOURCE
#include <fcntl.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <string.h> // memset
#define MAX_EVENTS 10
/*
KVServer will consist of a main thread that will perform the following steps:
......@@ -71,6 +59,17 @@ error. (Assume each character to be 1 byte in size)
*/
/*
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#define _GNU_SOURCE
#include <fcntl.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <string.h>
#define MAX_EVENTS 10
void *worker(void *args) {
......@@ -169,4 +168,91 @@ int main (int argc, int argv) {
}
return 0;
}
\ No newline at end of file
}*/
#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"
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
// Function designed for chat between client and server.
void func(int sockfd)
{
printf("receive functions:");
char buff[ sizeof(struct message)];
int n;
struct message* requestMessage=malloc(sizeof(struct message));
// infinite loop for chat
// read the message from client and copy it in buffer
read( sockfd , buff, sizeof(struct message));
// print buffer which contains the client contents
printf("From client: %c\t To client : ", buff[0]);
}
// Driver function
int main(int argc, char const *argv[])
{
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);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
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);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
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;
}
all: KVClient.c KVServer.c KVMessageFormat.h
gcc KVServer.c -o server
gcc KVClient.c -o client
\ No newline at end of file
Team:
Samarth Joshi 203059008
Roshan Sahu 203050048
How to Build:
How to Run:
References:
File added
File added
File added
#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