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
1c707917
Commit
1c707917
authored
Oct 03, 2019
by
SHAILESH KUMAR
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
toxml.c updated
parent
89f7c742
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
219 additions
and
172 deletions
+219
-172
client
client
+0
-0
client.c
client.c
+45
-30
client.c~
client.c~
+44
-30
server
server
+0
-0
server.c
server.c
+65
-56
server.c~
server.c~
+65
-56
No files found.
client
View file @
1c707917
No preview for this file type
client.c
View file @
1c707917
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include<stdlib.h>
#include <string.h>
#include<string.h>
#include<strings.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <netdb.h>
#include <unistd.h>
int
main
(
int
argc
,
char
**
argv
)
void
error
(
char
*
msg
)
{
{
int
s
;
perror
(
msg
);
int
sock_fd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
exit
(
0
);
}
struct
addrinfo
hints
,
*
result
;
memset
(
&
hints
,
0
,
sizeof
(
struct
addrinfo
));
hints
.
ai_family
=
AF_INET
;
hints
.
ai_socktype
=
SOCK_STREAM
;
s
=
getaddrinfo
(
NULL
,
"1234"
,
&
hints
,
&
result
);
if
(
s
!=
0
)
{
fprintf
(
stderr
,
"getaddrinfo: %s
\n
"
,
gai_strerror
(
s
));
exit
(
1
);
}
connect
(
sock_fd
,
result
->
ai_addr
,
result
->
ai_addrlen
);
int
main
(
int
argc
,
char
*
argv
[])
{
int
sockfd
,
portno
,
n
;
char
buffer
[
100
];
struct
sockaddr_in
serv_addr
;
while
(
fgets
(
buffer
,
100
,
stdin
)
!=
NULL
){
struct
hostent
*
server
;
printf
(
"SENDING: %s"
,
buffer
);
printf
(
"===
\n
"
);
write
(
sock_fd
,
buffer
,
strlen
(
buffer
));
char
buffer
[
256
];
char
resp
[
1000
];
portno
=
atoi
(
"8080"
);
int
len
=
read
(
sock_fd
,
resp
,
999
);
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
resp
[
len
]
=
'\0'
;
if
(
sockfd
<
0
)
printf
(
"%s
\n
"
,
resp
);
error
(
"ERROR opening socket"
);
server
=
gethostbyname
(
"127.0.0.1"
);
if
(
server
==
NULL
)
{
fprintf
(
stderr
,
"ERROR, no such host
\n
"
);
exit
(
0
);
}
}
bzero
((
char
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
serv_addr
.
sin_family
=
AF_INET
;
bcopy
((
char
*
)
server
->
h_addr
,
(
char
*
)
&
serv_addr
.
sin_addr
.
s_addr
,
server
->
h_length
);
serv_addr
.
sin_port
=
htons
(
portno
);
if
(
connect
(
sockfd
,(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
<
0
)
error
(
"ERROR connecting"
);
printf
(
"Please enter the message: "
);
bzero
(
buffer
,
256
);
fgets
(
buffer
,
255
,
stdin
);
n
=
write
(
sockfd
,
buffer
,
strlen
(
buffer
));
if
(
n
<
0
)
error
(
"ERROR writing to socket"
);
bzero
(
buffer
,
256
);
n
=
read
(
sockfd
,
buffer
,
255
);
if
(
n
<
0
)
error
(
"ERROR reading from socket"
);
printf
(
"%s
\n
"
,
buffer
);
return
0
;
return
0
;
}
}
client.c~
View file @
1c707917
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include<stdlib.h>
#include <string.h>
#include<strings.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <netdb.h>
#include <unistd.h>
int main(int argc, char** argv
)
void error(char *msg
)
{
{
int s;
perror(msg);
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
exit(0);
}
struct addrinfo hints, *result;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
s = getaddrinfo(NULL, "1234", &hints, &result);
if (s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
exit(1);
}
connect(sock_fd, result->ai_addr, result->ai_addrlen);
int main(int argc, char *argv[])
{
int sockfd, portno, n;
char buffer[100];
struct sockaddr_in serv_addr;
while(fgets(buffer, 100, stdin) != NULL){
struct hostent *server;
printf("SENDING: %s", buffer);
printf("===\n");
write(sock_fd, buffer, strlen(buffer));
char buffer[256];
char resp[1000];
portno = atoi("8080");
int len = read(sock_fd, resp, 999);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
resp[len] = '\0';
if (sockfd < 0)
printf("%s\n", resp);
error("ERROR opening socket");
server = gethostbyname("127.0.0.1");
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
return 0;
return 0;
}
}
server
View file @
1c707917
No preview for this file type
server.c
View file @
1c707917
/* A simple server in the internet domain using TCP
The port number is passed as an argument
This version runs forever, forking off a separate
process for each connection
gcc server2.c -lsocket
*/
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include<stdlib.h>
#include<unistd.h>
int
main
(
int
argc
,
char
**
argv
)
#include<strings.h>
void
dostuff
(
int
);
/* function prototype */
void
error
(
char
*
msg
)
{
{
int
s
;
perror
(
msg
);
int
sock_fd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
struct
addrinfo
hints
,
*
result
;
memset
(
&
hints
,
0
,
sizeof
(
struct
addrinfo
));
hints
.
ai_family
=
AF_INET
;
hints
.
ai_socktype
=
SOCK_STREAM
;
hints
.
ai_flags
=
AI_PASSIVE
;
s
=
getaddrinfo
(
NULL
,
"1234"
,
&
hints
,
&
result
);
if
(
s
!=
0
)
{
fprintf
(
stderr
,
"getaddrinfo: %s
\n
"
,
gai_strerror
(
s
));
exit
(
1
);
exit
(
1
);
}
}
if
(
bind
(
sock_fd
,
result
->
ai_addr
,
result
->
ai_addrlen
)
!=
0
)
int
main
(
int
argc
,
char
*
argv
[])
{
{
perror
(
"bind()"
);
int
sockfd
,
newsockfd
,
portno
,
clilen
,
pid
;
exit
(
1
);
struct
sockaddr_in
serv_addr
,
cli_addr
;
}
if
(
listen
(
sock_fd
,
10
)
!=
0
)
sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
{
if
(
sockfd
<
0
)
perror
(
"listen()"
);
error
(
"ERROR opening socket"
);
exit
(
1
);
bzero
((
char
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
serv_addr
.
sin_family
=
AF_INET
;
serv_addr
.
sin_addr
.
s_addr
=
INADDR_ANY
;
serv_addr
.
sin_port
=
htons
(
8080
);
if
(
bind
(
sockfd
,
(
struct
sockaddr
*
)
&
serv_addr
,
sizeof
(
serv_addr
))
<
0
)
error
(
"ERROR on binding"
);
listen
(
sockfd
,
5
);
clilen
=
sizeof
(
cli_addr
);
while
(
1
)
{
newsockfd
=
accept
(
sockfd
,
(
struct
sockaddr
*
)
&
cli_addr
,
&
clilen
);
if
(
newsockfd
<
0
)
error
(
"ERROR on accept"
);
pid
=
fork
();
if
(
pid
<
0
)
error
(
"ERROR on fork"
);
if
(
pid
==
0
)
{
close
(
sockfd
);
dostuff
(
newsockfd
);
exit
(
0
);
}
}
else
close
(
newsockfd
);
}
/* end of while */
return
0
;
/* we never get here */
}
printf
(
"Waiting for connection...
\n
"
);
/******** DOSTUFF() *********************
int
counter
=
0
;
There is a separate instance of this function
char
return_buffer
[
50
];
for each connection. It handles all communication
while
(
1
){
once a connnection has been established.
int
client_fd
=
accept
(
sock_fd
,
NULL
,
NULL
);
*****************************************/
printf
(
"Connection made: client_fd=%d
\n
"
,
client_fd
);
void
dostuff
(
int
sock
)
{
char
buffer
[
1000
];
int
n
;
int
len
=
read
(
client_fd
,
buffer
,
999
);
char
buffer
[
256
];
buffer
[
len
]
=
'\0'
;
printf
(
"Read %d chars
\n
"
,
len
);
printf
(
"===
\n
"
);
printf
(
"%s
\n
"
,
buffer
);
sprintf
(
return_buffer
,
"Server response to %dth client connection"
,
counter
);
write
(
client_fd
,
return_buffer
,
strlen
(
return_buffer
));
counter
++
;
}
return
0
;
bzero
(
buffer
,
256
);
n
=
read
(
sock
,
buffer
,
255
);
if
(
n
<
0
)
error
(
"ERROR reading from socket"
);
printf
(
"Here is the message: %s
\n
"
,
buffer
);
n
=
write
(
sock
,
"I got your message"
,
18
);
if
(
n
<
0
)
error
(
"ERROR writing to socket"
);
}
}
server.c~
View file @
1c707917
/* A simple server in the internet domain using TCP
The port number is passed as an argument
This version runs forever, forking off a separate
process for each connection
gcc server2.c -lsocket
*/
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include<stdlib.h>
#include<unistd.h>
int main(int argc, char** argv)
#include<strings.h>
void dostuff(int); /* function prototype */
void error(char *msg)
{
{
int s;
perror(msg);
int sock_fd = socket(AF_INET, SOCK_STREAM, 0);
struct addrinfo hints, *result;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
s = getaddrinfo(NULL, "1234", &hints, &result);
if (s != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
exit(1);
exit(1);
}
}
if ( bind(sock_fd, result->ai_addr, result->ai_addrlen) != 0 )
int main(int argc, char *argv[])
{
{
perror("bind()");
int sockfd, newsockfd, portno, clilen, pid;
exit(1);
struct sockaddr_in serv_addr, cli_addr;
}
if ( listen(sock_fd, 10) != 0 )
sockfd = socket(AF_INET, SOCK_STREAM, 0);
{
if (sockfd < 0)
perror("listen()");
error("ERROR opening socket");
exit(1);
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(8080);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
while (1) {
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
pid = fork();
if (pid < 0)
error("ERROR on fork");
if (pid == 0) {
close(sockfd);
dostuff(newsockfd);
exit(0);
}
}
else close(newsockfd);
} /* end of while */
return 0; /* we never get here */
}
printf("Waiting for connection...\n");
/******** DOSTUFF() *********************
int counter = 0;
There is a separate instance of this function
char return_buffer[50];
for each connection. It handles all communication
while(1){
once a connnection has been established.
int client_fd = accept(sock_fd, NULL, NULL);
*****************************************/
printf("Connection made: client_fd=%d\n", client_fd);
void dostuff (int sock)
{
char buffer[1000];
int n;
int len = read(client_fd, buffer, 999);
char buffer[256];
buffer[len] = '\0';
printf("Read %d chars\n", len);
printf("===\n");
printf("%s\n", buffer);
sprintf(return_buffer, "Server response to %dth client connection", counter);
write(client_fd, return_buffer, strlen(return_buffer));
counter++;
}
return 0;
bzero(buffer,256);
n = read(sock,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(sock,"I got your message",18);
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