Skip to content

Commit

Permalink
ADD: server with single connection, random fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyku committed Jan 16, 2018
1 parent 9738938 commit 52224c5
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
server
server_sc
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ default: server
server: server.cpp
$(CC) $(CFLAGS) -o server server.cpp

server_sc: server_single_connect.cpp
$(CC) $(CFLAGS) -o server_sc server_single_connect.cpp

clean:
rm -f server
rm -f server server_sc
23 changes: 23 additions & 0 deletions README.md
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
15 changes: 4 additions & 11 deletions server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ int main(int argc, char **argv)
{
struct sockaddr_un server_addr, client_addr;
socklen_t clientlen = sizeof(client_addr);
int client, buflen, nread, initCommResult;
int client, buflen, nread;
char *buf;
char c;
int reuse = 1;

puts("Hell World");

Expand All @@ -52,13 +50,6 @@ int main(int argc, char **argv)
exit(-1);
}

// set socket to immediately reuse port when the application closes
reuse = 1;
if (setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
perror("setsockopt");
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) {
Expand Down Expand Up @@ -96,5 +87,7 @@ int main(int argc, char **argv)

close(server);

return 0;
unlink(SOCKET_FILENAME);

return 0;
}
109 changes: 109 additions & 0 deletions server_single_connect.cpp
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;
}

0 comments on commit 52224c5

Please sign in to comment.