Ignore:
Timestamp:
08/20/10 13:02:36 (14 years ago)
Author:
neise
Message:
added 'fadfake' a programm acting as a FAD board. still with very limited funcionality, but 
nice for testing...
added a function OpenServerSocket() to SocketFunctions.cpp
File:
1 edited

Legend:

Unmodified
Added
Removed
  • fact/tools/FAD/SocketFunctions/SocketFunctions.cpp

    r256 r9887  
    225225} // end of CloseSockets
    226226       
     227
     228// returns Array of Sockethandles
     229int * OpenServerSockets (unsigned int NumberOfSockets, unsigned int StartPort) {
     230
     231        // allocate memory for ServerSocket Descriptors
     232//      int ServerSocket[NumberOfSockets];
     233        int *ServerSocket;
     234        ServerSocket = (int *) malloc( NumberOfSockets * sizeof(int) );
     235
     236        // Create Sockets
     237  for(unsigned int i=0 ; i < NumberOfSockets; i++ ){
     238    if ((ServerSocket[i] = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
     239      printf("Could not open server socket Nr. %d, no remote connection possible .\n", i);
     240      return NULL;
     241    }
     242  }
     243
     244  // Allows immediate reuse of socket after closing (circumvents TIME_WAIT)
     245  int Value=1;
     246  for(unsigned int i=0 ; i < NumberOfSockets; i++ ){
     247    if (setsockopt(ServerSocket[i], SOL_SOCKET, SO_REUSEADDR, (char *) &Value, sizeof (Value)) == -1) {
     248      printf("Warning (ServerSocket Nr. %d): Could not set server socket option SO_REUSEADDR \n", i);
     249    }
     250  }
     251 
     252        struct sockaddr_in SocketAddress[NumberOfSockets];
     253  for(unsigned int i=0 ; i < NumberOfSockets; i++ ){
     254    SocketAddress[i].sin_family = PF_INET;
     255    SocketAddress[i].sin_port = htons((unsigned short) StartPort + i);
     256    SocketAddress[i].sin_addr.s_addr = INADDR_ANY;
     257  }
     258
     259
     260  for(unsigned int i=0 ; i < NumberOfSockets; i++ ){
     261    if (bind(ServerSocket[i], (struct sockaddr *) &SocketAddress[i], sizeof(SocketAddress[i])) == -1) {
     262      printf("Could not bind port %d to socket Nr.: %d\n", StartPort+i, i );
     263      close(ServerSocket[i]);
     264      return NULL;
     265    }
     266  }
     267
     268  for(unsigned int i=0 ; i < NumberOfSockets; i++ ){
     269    if (listen(ServerSocket[i], 0) == -1) {
     270      printf("Could not set socket Nr. %d to listening \n", i);
     271      close(ServerSocket[i]);
     272      return NULL;
     273    }
     274  }
     275
     276        int *ConnectionSocket;
     277        ConnectionSocket = (int *) malloc( NumberOfSockets * sizeof(int) );
     278        struct sockaddr_in ClientAddress;
     279  socklen_t SizeClientAddress=sizeof(ClientAddress);
     280 
     281
     282//  while (1) { // Looping to wait for incoming connection
     283        printf ("Waiting for connection...\n");
     284          for(unsigned int i=0 ; i < NumberOfSockets; i++ ){
     285                        if ((ConnectionSocket[i] = accept(ServerSocket[i], (struct sockaddr *) &ClientAddress, &SizeClientAddress)) == -1){
     286                                close(ServerSocket[i]);
     287                                return NULL;
     288                        }
     289        }
     290//      }
     291  for(unsigned int i=0 ; i < NumberOfSockets; i++ ){
     292        //      close(ServerSocket[i]);
     293        }
     294
     295        printf("Connected to client at %s \n", inet_ntoa(ClientAddress.sin_addr));
     296        return ConnectionSocket;
     297} // end of OpenServerSockets
Note: See TracChangeset for help on using the changeset viewer.