Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <ctype.h>
- #include <sys/time.h>
- #include <fcntl.h>
- #include <iostream>
- #include <fstream>
- #include <unistd.h>
- int sock;
- int connectlist[5];
- fd_set socks;
- int highsock;
- void setnonblocking(int sock)
- {
- int opts;
- opts = fcntl(sock, F_GETFL);
- if(opts < 0)
- {
- perror("fcntl(F_GETFL)");
- exit(EXIT_FAILURE);
- }
- opts = (opts | O_NONBLOCK);
- if(fcntl(sock, F_SETFL, opts) < 0)
- {
- perror("fcntl(F_SETFL)");
- exit(EXIT_FAILURE);
- }
- return;
- }
- void build_select_list()
- {
- int listnum = 0;
- FD_ZERO(&socks);
- FD_SET(sock, &socks);
- while(connectlist[listnum] != 0)
- {
- FD_SET(connectlist[listnum], &socks);
- if(connectlist[listnum] > highsock)
- highsock = connectlist[listnum++];
- }
- }
- void handle_new_connection()
- {
- int listnum;
- int connection;
- connection = accept(sock, NULL, NULL);
- if(connection < 0)
- {
- perror("accept");
- exit(EXIT_FAILURE);
- }
- setnonblocking(connection);
- while(connection != -1)
- if(connectlist[listnum] == 0)
- {
- printf("Connection accepted:\nFD=%d;\nSlot=%d\n", connection, listnum);
- connectlist[listnum++] = connection;
- connection = -1;
- }
- }
- void handle_data(int listnum)
- {
- char buffer[256];
- if(read(connectlist[listnum], buffer, 256) < 0)
- {
- printf("\nConnection lost:\nFD=%d;\nSlot=%d\n", connectlist[listnum], listnum);
- close(connectlist[listnum]);
- connectlist[listnum] = 0;
- }
- else
- {
- printf("\nRecieved: %s; ", buffer);
- write(connectlist[listnum], buffer, sizeof(buffer));
- write(connectlist[listnum], "\n", sizeof("\n"));
- printf("Responded.\n");
- }
- }
- void read_socks()
- {
- int listnum;
- if(FD_ISSET(sock, &socks))
- handle_new_connection();
- while(connectlist[listnum] != 0)
- if(FD_ISSET(connectlist[listnum++], &socks))
- handle_data(listnum-1);
- }
- int main(int argc, char* argv[])
- {
- printf("maybs\n");
- char *ascport;
- int port;
- struct sockaddr_in server_address;
- int reuse_addr = 1;
- struct timeval timeout;
- int readsocks;
- if(argc < 2)
- {
- printf("Usage: %s PORT\r\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- sock = socket(AF_INET, SOCK_STREAM, 0);
- if(sock < 0)
- {
- perror("SOCKET");
- exit(EXIT_FAILURE);
- }
- printf("com on yo\n");
- setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr));
- setnonblocking(sock);
- ascport = argv[1];
- port = atoi(ascport);
- memset((char *) &server_address, 0, sizeof(server_address));
- server_address.sin_family = AF_INET;
- server_address.sin_addr.s_addr = htonl(INADDR_ANY);
- server_address.sin_port = port;
- if(bind(sock, (struct sockaddr *) &server_address, sizeof(server_address)) < 0)
- {
- perror("BIND");
- close(sock);
- exit(EXIT_FAILURE);
- }
- printf("past bind?\n");
- listen(sock, 1024);
- highsock = sock;
- memset((char *) &connectlist, 0, sizeof(connectlist));
- while(1) {
- build_select_list();
- timeout.tv_sec = 1;
- timeout.tv_usec = 0;
- readsocks = select(highsock+1, &socks, (fd_set *) 0, (fd_set *) 0, &timeout);
- if(readsocks < 0)
- {
- perror("select");
- exit(EXIT_FAILURE);
- }
- if(readsocks != 0)
- read_socks();
- printf("nothin 2 do\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment