-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADD: server with single connection, random fixes
- Loading branch information
Showing
5 changed files
with
141 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
server | ||
server_sc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Build | ||
|
||
Build the server | ||
|
||
~~~bash | ||
$ make server | ||
~~~ | ||
|
||
Build the server that accepts only a single connection at a time | ||
|
||
~~~bash | ||
$ make server_sc | ||
~~~ | ||
|
||
# Connecting to the server | ||
|
||
~~~bash | ||
$ php client.php Hi | ||
~~~ | ||
|
||
# References | ||
- http://www.binarytides.com/php-socket-programming-tutorial/ | ||
- https://github.com/zappala/socket-programming-examples-c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#include <arpa/inet.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <sys/un.h> | ||
#include <unistd.h> | ||
#include <signal.h> | ||
|
||
using namespace std; | ||
|
||
#define SOCKET_FILENAME "/tmp/server.sock" | ||
|
||
int server; | ||
|
||
void bind_listen_socket(int &server, sockaddr_un &server_addr) | ||
{ | ||
// create socket | ||
server = socket(PF_UNIX, SOCK_STREAM, 0); | ||
if (!server) { | ||
perror("socket"); | ||
exit(-1); | ||
} | ||
|
||
// call bind to associate the socket with our local address and | ||
// port | ||
if (bind(server, (const struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { | ||
perror("bind"); | ||
exit(-1); | ||
} | ||
|
||
// convert the socket to listen for incoming connections | ||
if (listen(server, 0) < 0) { | ||
perror("listen"); | ||
exit(-1); | ||
} | ||
|
||
puts("Listening to connection..."); | ||
|
||
} | ||
|
||
void signal_callback_handler(int signum) | ||
{ | ||
// close server | ||
close(server); | ||
// remove the socket file | ||
unlink(SOCKET_FILENAME); | ||
// signal handled | ||
exit(0); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
struct sockaddr_un server_addr, client_addr; | ||
socklen_t clientlen = sizeof(client_addr); | ||
int client, buflen, nread; | ||
char *buf; | ||
|
||
puts("Hell World"); | ||
|
||
// listen to SIGINT, SIGTERM, and SIGKILL | ||
signal(SIGINT, signal_callback_handler); | ||
signal(SIGTERM, signal_callback_handler); | ||
signal(SIGKILL, signal_callback_handler); | ||
|
||
// setup socket address structure | ||
memset(&server_addr, 0, sizeof(server_addr)); | ||
server_addr.sun_family = AF_UNIX; | ||
strcpy(server_addr.sun_path, SOCKET_FILENAME); | ||
|
||
// bind and listen on the socket file | ||
bind_listen_socket(server, server_addr); | ||
|
||
// allocate buffer | ||
buflen = 1024; | ||
buf = new char[buflen+1]; | ||
|
||
// loop to handle all requests | ||
while (1) { | ||
unsigned int client = accept(server, (struct sockaddr *)&client_addr, &clientlen); | ||
|
||
// got a request, close the socket | ||
close(server); | ||
unlink(SOCKET_FILENAME); | ||
|
||
// read a request | ||
memset(buf, 0, buflen); | ||
nread = recv(client, buf, buflen, 0); | ||
|
||
printf("\nClient says: %s\n\n", buf); | ||
|
||
// echo back to the client | ||
send(client, buf, nread, 0); | ||
|
||
close(client); | ||
|
||
sleep(2); | ||
|
||
// re-bind and listen on the socket | ||
bind_listen_socket(server, server_addr); | ||
} | ||
|
||
close(server); | ||
|
||
unlink(SOCKET_FILENAME); | ||
|
||
return 0; | ||
} |