Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <arpa/inet.h>
- #include <sys/wait.h>
- #include <signal.h>
- #include <curl/curl.h>
- #include <fcntl.h>
- #define PORT 31337
- void sigchild(int s);
- int main() {
- // Interacted Variables
- int listensock;
- int newfd;
- struct sockaddr_in myaddr;
- struct sockaddr_in theiraddy;
- fd_set readfds;
- fd_set masterfds;
- int maxfd;
- int childpipe[2];
- // Uninteracted values
- unsigned int sin_size;
- struct sigaction sa;
- int ret;
- int yes = 1;
- // Temporary variables
- char *tmpbuf = malloc(20);
- int x;
- listensock = socket(AF_INET, SOCK_STREAM, 0);
- if (listensock == -1) {
- perror("socket");
- exit(1);
- }
- ret = setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
- if (ret == -1) {
- perror("setsockopt");
- exit(1);
- }
- memset(&myaddr, '\0', sizeof(struct sockaddr_in));
- myaddr.sin_family = AF_INET;
- myaddr.sin_port = htons(PORT);
- myaddr.sin_addr.s_addr = INADDR_ANY;
- ret = bind(listensock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr));
- if (ret == -1) {
- perror("bind");
- exit(1);
- }
- ret = listen(listensock, 50);
- if (ret == -1) {
- perror("listen");
- exit(1);
- }
- sa.sa_handler = sigchild;
- sigemptyset(&sa.sa_mask);
- sa.sa_flags = SA_RESTART;
- ret = sigaction(SIGCHLD, &sa, NULL);
- if (ret == -1) {
- perror("sigaction");
- exit(1);
- }
- FD_ZERO(&readfds);
- FD_ZERO(&masterfds);
- FD_SET(listensock, &masterfds);
- maxfd = listensock;
- while(1) {
- readfds = masterfds;
- sin_size = sizeof(struct sockaddr_in);
- ret = select(maxfd+1, &readfds, NULL, NULL, NULL);
- if (ret == -1) {
- perror("select");
- printf("Error: %s\n", strerror(errno));
- exit(1);
- }
- if (FD_ISSET(listensock, &readfds)) {
- newfd = accept(listensock, (struct sockaddr *)&theiraddy, &sin_size);
- if (newfd == -1) { // Weird error, just continue
- perror("accept");
- close(newfd);
- fprintf(stderr, "Server capped out, bad accept\n");
- continue;
- }
- else {
- printf("SERVER: New connection from %s\n", inet_ntoa(theiraddy.sin_addr));
- }
- ret = pipe(childpipe);
- if (ret == -1) {
- perror("The pipe did not work, not sure what to do\n");
- exit(4);
- }
- //FD_SET(childpipe[0], &masterfds);
- //if (childpipe[0] > maxfd)
- // maxfd = childpipe[0];
- if(!fork()) {
- // This is the child process
- close(childpipe[1]); // The child will write to childpipe[0]
- close(listensock);
- close(newfd);
- exit(0);
- }
- else {
- // This is the parent process
- close(childpipe[0]); // The parent will write to childpipe[1]
- close(newfd);
- }
- } // The IF of whether its the listensock
- else {
- for(x=0;x<maxfd;x++) {
- if (FD_ISSET(x, &readfds)) {
- read(x, tmpbuf, 10);
- printf("%s\n", tmpbuf);
- }
- }
- } // This means a child sent a message back to the client
- }
- }
- void sigchild(int s) {
- while(wait(NULL) > 0);
- }
Add Comment
Please, Sign In to add comment