Changeset 9887
- Timestamp:
- 08/20/10 13:02:36 (14 years ago)
- Location:
- fact/tools/FAD
- Files:
-
- 5 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
fact/tools/FAD/SocketFunctions/SocketFunctions.cpp
r256 r9887 225 225 } // end of CloseSockets 226 226 227 228 // returns Array of Sockethandles 229 int * 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 -
fact/tools/FAD/SocketFunctions/SocketFunctions.h
r256 r9887 58 58 59 59 60 int * OpenServerSockets (unsigned int NumberOfSockets, unsigned int StartPort) ; 61
Note:
See TracChangeset
for help on using the changeset viewer.