Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
key-value-server
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
SHAILESH KUMAR
key-value-server
Commits
817896ef
Commit
817896ef
authored
Oct 16, 2019
by
SHAILESH KUMAR
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
job queue
parent
efc835b7
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
140 additions
and
28 deletions
+140
-28
client
client
+0
-0
client.c
client.c
+4
-1
client.c~
client.c~
+4
-1
server
server
+0
-0
server.c
server.c
+66
-13
server.c~
server.c~
+66
-13
No files found.
client
View file @
817896ef
No preview for this file type
client.c
View file @
817896ef
...
@@ -59,6 +59,7 @@ void sendXML(int sockfd,char *msg){
...
@@ -59,6 +59,7 @@ void sendXML(int sockfd,char *msg){
}
}
toXML
(
encodedXML
,
key
,
value
,
msgtype
,
""
);
toXML
(
encodedXML
,
key
,
value
,
msgtype
,
""
);
write
(
sockfd
,
strcat
(
encodedXML
,
delim
),
strlen
(
encodedXML
));
write
(
sockfd
,
strcat
(
encodedXML
,
delim
),
strlen
(
encodedXML
));
char
buf
[
256
];
char
buf
[
256
];
read
(
sockfd
,
buf
,
256
);
read
(
sockfd
,
buf
,
256
);
...
@@ -114,8 +115,10 @@ int main(int argc, char *argv[])
...
@@ -114,8 +115,10 @@ int main(int argc, char *argv[])
sendXML
(
sockfd
,
line
);
sendXML
(
sockfd
,
line
);
}
}
while
(
1
);
fclose
(
fp
);
fclose
(
fp
);
while
(
1
);
//exit(0);
//exit(0);
return
0
;
return
0
;
...
...
client.c~
View file @
817896ef
...
@@ -59,6 +59,7 @@ void sendXML(int sockfd,char *msg){
...
@@ -59,6 +59,7 @@ void sendXML(int sockfd,char *msg){
}
}
toXML(encodedXML,key,value,msgtype,"");
toXML(encodedXML,key,value,msgtype,"");
write(sockfd,strcat(encodedXML,delim),strlen(encodedXML));
write(sockfd,strcat(encodedXML,delim),strlen(encodedXML));
char buf[256];
char buf[256];
read(sockfd,buf,256);
read(sockfd,buf,256);
...
@@ -114,8 +115,10 @@ int main(int argc, char *argv[])
...
@@ -114,8 +115,10 @@ int main(int argc, char *argv[])
sendXML(sockfd,line);
sendXML(sockfd,line);
}
}
while(1);
fclose(fp);
fclose(fp);
while(1);
//exit(0);
//exit(0);
return 0;
return 0;
...
...
server
View file @
817896ef
No preview for this file type
server.c
View file @
817896ef
...
@@ -12,7 +12,16 @@
...
@@ -12,7 +12,16 @@
#include<unistd.h>
#include<unistd.h>
#include<strings.h>
#include<strings.h>
#include<string.h>
#include<string.h>
void
dostuff
(
int
);
/* function prototype */
#include<pthread.h>
#include "toxml.h"
#include "jobs.h"
jobs_queue
*
job_head
=
NULL
;
jobs_queue
*
job_tail
=
NULL
;
pthread_mutex_t
job_q_lock
;
void
*
serve
(
void
*
);
/* function prototype */
void
error
(
char
*
msg
)
void
error
(
char
*
msg
)
{
{
perror
(
msg
);
perror
(
msg
);
...
@@ -23,7 +32,7 @@ int main(int argc, char *argv[])
...
@@ -23,7 +32,7 @@ int main(int argc, char *argv[])
{
{
int
sockfd
,
newsockfd
,
portno
,
clilen
,
pid
;
int
sockfd
,
newsockfd
,
portno
,
clilen
,
pid
;
struct
sockaddr_in
serv_addr
,
cli_addr
;
struct
sockaddr_in
serv_addr
,
cli_addr
;
pthread_mutex_init
(
&
job_q_lock
,
NULL
);
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
if
(
sockfd
<
0
)
if
(
sockfd
<
0
)
error
(
"ERROR opening socket"
);
error
(
"ERROR opening socket"
);
...
@@ -39,37 +48,81 @@ int main(int argc, char *argv[])
...
@@ -39,37 +48,81 @@ int main(int argc, char *argv[])
while
(
1
)
{
while
(
1
)
{
newsockfd
=
accept
(
sockfd
,
newsockfd
=
accept
(
sockfd
,
(
struct
sockaddr
*
)
&
cli_addr
,
&
clilen
);
(
struct
sockaddr
*
)
&
cli_addr
,
&
clilen
);
if
(
newsockfd
<
0
)
/*
if (newsockfd < 0)
error("ERROR on accept");
error("ERROR on accept");
pid = fork();
pid = fork();
if (pid < 0)
if (pid < 0)
error("ERROR on fork");
error("ERROR on fork");
if (pid == 0) {
if (pid == 0) {
close(sockfd);
close(sockfd);
dostuff
(
newsockfd
);
serve
(newsockfd);
exit(0);
exit(0);
}
}
else
close
(
newsockfd
);
else close(newsockfd);*/
pthread_t
thr
;
int
*
arg
;
arg
=
malloc
(
sizeof
(
*
arg
));
*
arg
=
newsockfd
;
pthread_create
(
&
thr
,
NULL
,
serve
,
arg
);
}
/* end of while */
}
/* end of while */
return
0
;
/* we never get here */
return
0
;
/* we never get here */
}
}
/********
DOSTUFF
() *********************
/********
serve
() *********************
There is a separate instance of this function
There is a separate instance of this function
for each connection. It handles all communication
for each connection. It handles all communication
once a connnection has been established.
once a connnection has been established.
*****************************************/
*****************************************/
void
dostuff
(
int
sock
)
void
*
serve
(
void
*
socket
)
{
{
int
sock
;
int
n
;
int
n
;
char
buffer
[
256
];
long
tid
=
pthread_self
();
sock
=*
((
int
*
)
socket
);
char
buffer
[
300000
];
while
(
1
){
while
(
1
){
bzero
(
buffer
,
256
);
//zeroing all the value in buffer
n
=
read
(
sock
,
buffer
,
255
);
bzero
(
buffer
,
300000
);
//reading the socket
n
=
read
(
sock
,
buffer
,
300000
);
if
(
n
<
0
)
error
(
"ERROR reading from socket"
);
if
(
n
<
0
)
error
(
"ERROR reading from socket"
);
printf
(
"Here is the message:
\n
%s
\n
"
,
buffer
);
else
if
(
n
==
0
)
{
n
=
write
(
sock
,
"I got your message
\n
"
,
19
);
printf
(
"Client has been closed,Killing the thread
\n
"
);
pthread_cancel
(
tid
);
}
else
{
//Creating a structure to add in linked list of job queue
jobs_queue
*
job
=
(
jobs_queue
*
)
malloc
((
sizeof
(
jobs_queue
)));
job
->
fd
=
sock
;
job
->
next
=
NULL
;
strcpy
(
job
->
xml
,
buffer
);
//Adding to the chain in Job Queue
pthread_mutex_lock
(
&
job_q_lock
);
if
(
job_head
==
NULL
){
job_head
=
job
;
job_tail
=
job
;
}
else
{
job_tail
->
next
=
job
;
job_tail
=
job
;
}
pthread_mutex_unlock
(
&
job_q_lock
);
//Printing the linked list
jobs_queue
*
p
=
job_head
;
while
(
p
!=
NULL
){
printf
(
"%d "
,
p
->
fd
);
p
=
p
->
next
;
}
printf
(
"
\n
"
);
//Acknowledging the client to send next data
n
=
write
(
sock
,
"ACK
\n
"
,
4
);
if
(
n
<
0
)
error
(
"ERROR writing to socket"
);
if
(
n
<
0
)
error
(
"ERROR writing to socket"
);
}
}
}
}
}
server.c~
View file @
817896ef
...
@@ -12,7 +12,16 @@
...
@@ -12,7 +12,16 @@
#include<unistd.h>
#include<unistd.h>
#include<strings.h>
#include<strings.h>
#include<string.h>
#include<string.h>
void dostuff(int); /* function prototype */
#include<pthread.h>
#include "toxml.h"
#include "jobs.h"
jobs_queue *job_head=NULL;
jobs_queue *job_tail=NULL;
pthread_mutex_t job_q_lock;
void *serve(void*); /* function prototype */
void error(char *msg)
void error(char *msg)
{
{
perror(msg);
perror(msg);
...
@@ -23,7 +32,7 @@ int main(int argc, char *argv[])
...
@@ -23,7 +32,7 @@ int main(int argc, char *argv[])
{
{
int sockfd, newsockfd, portno, clilen, pid;
int sockfd, newsockfd, portno, clilen, pid;
struct sockaddr_in serv_addr, cli_addr;
struct sockaddr_in serv_addr, cli_addr;
pthread_mutex_init(&job_q_lock,NULL);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
if (sockfd < 0)
error("ERROR opening socket");
error("ERROR opening socket");
...
@@ -39,37 +48,81 @@ int main(int argc, char *argv[])
...
@@ -39,37 +48,81 @@ int main(int argc, char *argv[])
while (1) {
while (1) {
newsockfd = accept(sockfd,
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr, &clilen);
(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
/*
if (newsockfd < 0)
error("ERROR on accept");
error("ERROR on accept");
pid = fork();
pid = fork();
if (pid < 0)
if (pid < 0)
error("ERROR on fork");
error("ERROR on fork");
if (pid == 0) {
if (pid == 0) {
close(sockfd);
close(sockfd);
dostuff
(newsockfd);
serve
(newsockfd);
exit(0);
exit(0);
}
}
else close(newsockfd);
else close(newsockfd);*/
pthread_t thr;
int *arg;
arg=malloc(sizeof(*arg));
*arg=newsockfd;
pthread_create(&thr,NULL,serve,arg);
} /* end of while */
} /* end of while */
return 0; /* we never get here */
return 0; /* we never get here */
}
}
/********
DOSTUFF
() *********************
/********
serve
() *********************
There is a separate instance of this function
There is a separate instance of this function
for each connection. It handles all communication
for each connection. It handles all communication
once a connnection has been established.
once a connnection has been established.
*****************************************/
*****************************************/
void
dostuff (int sock
)
void
* serve (void* socket
)
{
{
int sock;
int n;
int n;
char buffer[256];
long tid=pthread_self();
sock=*((int*) socket);
char buffer[300000];
while(1){
while(1){
bzero(buffer,256);
//zeroing all the value in buffer
n = read(sock,buffer,255);
bzero(buffer,300000);
//reading the socket
n = read(sock,buffer,300000);
if (n < 0) error("ERROR reading from socket");
if (n < 0) error("ERROR reading from socket");
printf("Here is the message:\n %s\n",buffer);
else if (n==0) {
n = write(sock,"I got your message\n",19);
printf("Client has been closed,Killing the thread\n");
pthread_cancel(tid);
}
else{
//Creating a structure to add in linked list of job queue
jobs_queue *job=(jobs_queue *)malloc((sizeof(jobs_queue)));
job->fd=sock;
job->next=NULL;
strcpy(job->xml,buffer);
//Adding to the chain in Job Queue
pthread_mutex_lock(&job_q_lock);
if(job_head==NULL){
job_head=job;
job_tail=job;
}
else{
job_tail->next=job;
job_tail=job;
}
pthread_mutex_unlock(&job_q_lock);
//Printing the linked list
/*jobs_queue *p=job_head;
while(p!=NULL){
printf("%d ",p->fd);
p=p->next;
}
printf("\n");
*/
//Acknowledging the client to send next data
n = write(sock,"ACK\n",4);
if (n < 0) error("ERROR writing to socket");
if (n < 0) error("ERROR writing to socket");
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment