
root@ubuntu:~/c++# ./u_server Listener on port /tmp/echo_socket Waiting for connections ... Welcome message sent successfully Adding to list of sockets as 0 Welcome message sent successfully Adding to list of sockets as 1 recv failed: Connection reset by peer recv failed: Connection reset by peer Welcome message sent successfully Adding to list of sockets as 0 Welcome message sent successfully Adding to list of sockets as 1 recv failed: Connection reset by peer recv failed: Connection reset by peer ^C
server
root@ubuntu:~/c++# cat u_server.c #include <stdio.h> #include <stdint.h> #include <string.h> //strlen #include <stdlib.h> #include <errno.h> #include <unistd.h> //close #include <sys/un.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/stat.h> #include <netinet/in.h> #include <sys/time.h> //FD_SET, FD_ISSET, FD_ZERO macros #include<pthread.h> //for threading , link with lpthread #include <fcntl.h> #define SOCK_PATH "/tmp/echo_socket" bool daemonize() { int res = fork(); if (res < 0) { perror("fork"); return false; } if (res != 0) _exit(0); // Now we're running as the daemon... res = setsid(); if (res < 0) { perror("setsid"); return false; } if (false) { int fd = open("/dev/null", O_RDWR, 0); if (fd >= 0) { dup2(fd, STDIN_FILENO); dup2(fd, STDOUT_FILENO); dup2(fd, STDERR_FILENO); if (fd > 2) close(fd); } } return true; } /* * This will handle connection for each client * */ void *connection_handler(void *socket_desc) { //Get the socket descriptor int& sock = *(int*)socket_desc; int read_size; char client_message[2000]; //Send some messages to the client const char *message = "Greetings! I am your connection handler "; write(sock , message , strlen(message)); message = "Now type something and i shall repeat what you type "; write(sock , message , strlen(message)); //Receive a message from client while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 ) { //end of string marker client_message[read_size] = '