Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / utilities / manual_control / manualControlServer / sockserver_test.c @ 13

History | View | Annotate | Download (2.56 KB)

1

    
2
/* SERVER
3
I am washing my socks.
4
And hanging them out to dry.
5

6
-Greg
7
*/
8

    
9
#include <sys/types.h>
10
#include <sys/socket.h>
11
#include <stdio.h>
12
#include <netinet/in.h>
13
#include <arpa/inet.h>
14
#include <string.h>
15
#include <netdb.h>
16

    
17
int mysocket;   //the socket that gets new connections
18
int io_socket;  //the socket we use to communicate when a connection is initiated.
19

    
20
int main (int argc, char **argv) {
21

    
22
        unsigned int clientlen;
23
        struct sockaddr_in clientaddr;
24
        struct hostent *hp;
25
        char *haddrp;
26
        char newdata[100];
27
        int newdatasize;
28

    
29
        if (argc != 2) {
30
                fprintf(stderr, "Usage: %s <port>\n", argv[0]);
31
                return 0;
32
        }
33
        
34
        //start server
35
        printf("Starting server on port %i\n", atoi(argv[1]));
36
        mysocket = open_listenfd(atoi(argv[1]));
37
        if (mysocket < 0) {
38
                fprintf(stderr, "Error listening on port.\n");
39
                return -1;
40
        } else {
41
                printf("Server running, waiting for connection\n");
42
        }
43
        
44
        //accept connections
45
        while (1) {
46
                clientlen = sizeof(clientaddr);
47
                io_socket = accept(mysocket, (struct sockaddr *) &clientaddr, &clientlen);
48
                
49
                //get client info
50
                hp = gethostbyaddr((const char *) &clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr), AF_INET);
51
                haddrp = inet_ntoa(clientaddr.sin_addr);
52
                printf("Client connected: %s %s\n", hp->h_name, haddrp);
53
                
54
                if (!fork()) { // this is the child process
55
            close(mysocket); // child doesn't need the listener
56
                        newdata[0] = ' ';
57
                        while (newdata[0] != 'q') {
58
                                memset(newdata, '\0', strlen(newdata));
59
                                newdatasize = recv(io_socket, newdata, 99, 0);
60
                                printf("Received: %s\n", newdata);
61
                                //send_command(cmd0, GLOBAL_DEST);
62
                        }
63
                        printf("\nClient has disconnected.\n");
64
            close(io_socket);
65
            return 0;
66
        }
67
                close(io_socket);
68
        }
69
        
70
        close(mysocket);
71
        return 0;
72
}
73

    
74
//listen on port, as specified by 15-213 book.
75
int open_listenfd(int port) {
76
        int listenfd, optval=1;
77
        struct sockaddr_in serveraddr;
78
        
79
        //create socket descriptor
80
        if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
81
                return -1;
82
        
83
        //prevent "address already in use" error
84
        if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *) &optval, sizeof(int)) < 0)
85
                return -1;
86
                
87
        bzero((char *) &serveraddr, sizeof(serveraddr));
88
        serveraddr.sin_family = AF_INET;
89
        serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
90
        serveraddr.sin_port = htons((unsigned short)port);
91
        if (bind(listenfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
92
                return -1;
93
        
94
        //start listening
95
        if (listen(listenfd, 128) < 0)
96
                return -1;
97
        return listenfd;
98
}
99