Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <signal.h>
- #include <errno.h>
- void sigchld_handler(int signo)
- {
- while(waitpid(-1, NULL, WHOHANG) > 0);
- }
- int main() {
- int def_sock, new_sock;
- struct sockaddr_in s_addr;
- struct sockaddr_in n_addr;
- socklen_t addr_size;
- char buffer[1024];
- int result;
- int nread;
- pid_t pid;
- /*---- Create the socket. The three arguments are: ----*/
- /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
- def_sock = socket(AF_INET, SOCK_STREAM, 0);
- if (def_sock == -1) {
- printf("Socket can't be created, exit script");
- exit(1);
- }
- /*---- Configure settings of the server address struct ----*/
- s_addr.sin_family = AF_INET;
- s_addr.sin_port = htons(5001);
- s_addr.sin_addr.s_addr = INADDR_ANY;
- memset(s_addr.sin_zero, '0', sizeof s_addr.sin_zero);
- printf("Port = '%d'\n", s_addr.sin_port);
- /*---- Bind the address struct to the socket ----*/
- result = bind(def_sock, (struct sockaddr *) &s_addr, sizeof(s_addr));
- if (result < 0) {
- printf("Can't bind socket, exiting\n");
- exit(1);
- }
- /*---- Listen on the socket, with 5 max connection requests queued ----*/
- if (listen(def_sock, 5) == 0) {
- printf("Listening\n");
- }
- else {
- printf("Listening can't be started\n");
- exit(1);
- }
- signal(SIGCHLD, sigchld_handler);
- /*---- Accept call creates a new socket for the incoming connection ----*/
- printf("Original pid = '%i'\n", getpid());
- while (1) {
- printf("Test1\n");
- printf("Test3\n");
- addr_size = sizeof n_addr;
- if ((new_sock = accept(def_sock, (struct sockaddr *) &n_addr, &addr_size)) < 0) {
- printf("Hello!\n");
- }
- else {
- printf("Hello2!\n");
- }
- printf("Test4\n");
- if ( new_sock == -1 ) {
- printf("Can't create socket for connection\n");
- exit(1);
- }
- printf("Test2\n");
- pid = fork();
- printf("Fork returned %d\n", (int) pid);
- if ((pid = fork()) == 0) {
- printf("Test1\n");
- printf("Child process %i created\n", getpid());
- close(def_sock);
- nread = recv(new_sock, buffer, 25, 0);
- printf("Test1\n");
- buffer[nread] = '\0';
- printf("%s\n", buffer);
- send(new_sock, buffer, nread, 0);
- close(new_sock);
- sleep(5);
- printf("child process %i finished\n", getpid());
- exit(0);
- }
- printf("Test2\n");
- wait(NULL);
- close(new_sock);
- /*---- Send message to the socket of the incoming connection ----*/
- //memset(buffer, 0, sizeof buffer);
- //strcpy(buffer,"Hello World!\n");
- //send(new_sock,buffer,13,0);
- ////*---- Read the message from the client into the buffer ----*/
- ////memset(buffer, 0, sizeof buffer);
- //if (recv(new_sock, buffer, sizeof(buffer), 0) < 0) {
- //printf("Can't get message from client\n");
- //}
- //else {
- //printf("Client said %s\n", buffer);
- //}
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment