Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / ColonetServer / initialization.c @ 20

History | View | Annotate | Download (3.1 KB)

1
/** 
2
 * @file initialization.c
3
 *
4
 *  @author Jason Knichel
5
 *  @author Eugene Marinelli
6
 */
7

    
8
#include <sys/socket.h>
9
#include <sys/types.h>
10
#include <netinet/in.h>
11
#include <string.h>
12
#include <fcntl.h>
13
#include <unistd.h>
14
#include <stdio.h>
15
#include <stdlib.h>
16

    
17
#include "includes/initialization.h"
18
#include "includes/client.h"
19
#include "includes/options.h"
20
#include "includes/Logging.h"
21

    
22
extern int listenSocket;
23

    
24
/*
25
int parseCommandLine(int argc, char * argv[], commandParams_t * params)
26
{
27
  printf("Parsing Command Line...\n");
28
  if (!params)
29
    return -1;
30

31
  //if no command line parameters were specified, set to defaults
32
  if (argc == 1) {
33
    printf("No port was specified for listening for client connections."
34
           "  Defaulting to %d\n", DEFAULTPORT);
35
    params->listenPort = DEFAULTPORT;
36
  }
37

38
  if (!argv)
39
    return -1;
40

41
  int i;
42
  for (i = 1; i < argc; i++) {
43
    char * temp = argv[i];
44
    if (temp[0] != '-')
45
      {
46
        printUsage(argv[0]);
47
        return -1;
48
      }
49

50
    switch(temp[1])
51
      {
52
      case 'h': printUsage(argv[0]);
53
        break;
54
      case 'p': 
55
        {
56
          if (i >= argc-1)
57
            {
58
              printUsage(argv[0]);
59
              return -1;
60
            }
61
          i++;
62
          char * portString = argv[i];
63
          char * endptr = NULL;
64
          int port = strtol(portString, &endptr, 10);
65
          if (*endptr != '\0')
66
            {
67
              printf("Invalid port specified...%s, %d\n", endptr, port);
68
              return -1;
69
            }
70
          if (port < SMALLEST_POSS_LISTEN_PORT)
71
            {
72
              printf("You cannot listen on a port less than %d.\n",
73
                     SMALLEST_POSS_LISTEN_PORT);
74
              return -1;
75
            }
76
          params->listenPort = port;
77
          printf("Setting port to listen on to %d.\n", params->listenPort);
78
        }
79
        break;
80
      default: printUsage(argv[0]);
81
        return -1;
82
        break;
83
      }
84
  }
85

86
  return 0;
87
}
88
*/
89

    
90
int initConnection(int port)
91
{
92
  printf("Initializing connection that will be used to listen for " 
93
         "clients...\n");
94
  int opts = 1;
95
  struct sockaddr_in my_addr;
96

    
97
  //get a socket fd
98
  if ((listenSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
99
    printf("\tThere was an error creating a socket\n");
100
    return -1;
101
  }
102

    
103
  //set up the address struct
104
  memset(&my_addr,'\0',sizeof(my_addr));
105
  my_addr.sin_family = AF_INET;
106
  my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
107
  my_addr.sin_port = htons(port);
108
  
109
  setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &opts, sizeof(opts));
110

    
111
  //get the current socket options
112
  if ((opts = fcntl(listenSocket, F_GETFL)) < 0) {
113
    printf("\tThere was an error getting the socket options.\n");
114
    return -1;
115
  }
116

    
117
  //set the socket to non blocking
118
  opts = (opts | O_NONBLOCK);
119
  if (fcntl(listenSocket, F_SETFL, opts) < 0) {
120
    printf("\tThere was an error setting the socket to be non blocking.\n");
121
    return -1;
122
  }
123
  
124
  //bind the socket to listen on the specified port
125
  if (bind(listenSocket, (struct sockaddr *) &my_addr, sizeof(my_addr)) < 0) {
126
    printf("\tThere was an error binding the socket\n");
127
    return -1;
128
  }
129
  
130
  return 0;
131
}