Thread: How can be stored an array of sockets?

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    1

    Question How can be stored an array of sockets?

    Hello all,

    I have in mind the creation of a chat server to test how do the sockets work.

    I created a simple server which attends 1 client at a time and then closes the connection.

    Now I would need to know how to store that client sockets into an array so I can later make them to communicate between them using the file descriptor creating a child process for each one.

    I got this code so far:

    Code:
    // Standard libs
    #include <stdio.h>
    
    // Socket related libs
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    #define PORT 4000
    #define CLIENTS 2
    
    int main(void)
    {
        // File descriptors for client and server
        int socketServer;
        int socketClient;
        
        struct sockaddr_in server;
        struct sockaddr_in client;
        
        int sin_size;
    
        print_info();
    
        printf("Initializing server...\n");
        
        printf("Creating sockets... ");
        if((socketServer=socket(AF_INET, SOCK_STREAM, 0)) == -1){
            printf("[FAILED]\n");
            perror("Initializing socket()");
            return 1;
        }
        else printf("[OK]\n");
        
        server.sin_family = AF_INET;
        
        server.sin_port = htons(PORT);
        server.sin_addr.s_addr = INADDR_ANY;
        bzero(&(server.sin_zero),8);
        
        printf("Binding sockets... ");
        if(bind(socketServer,(struct sockaddr*)&server,
               sizeof(struct sockaddr))==-1) {
                       printf("[FAILED]\n");
                       perror("bind()");
                      return 1;
          }
          else printf("[OK]\n");
          
          printf("Listening on port %d... ",PORT);
          if(listen(socketServer,CLIENTS) == -1) {
              printf("[FAILED]\n");
              perror("listen()");
              return 1;
          }
          else printf("[OK]\n");
          
          // Continuously accepting remote connections
          while(1){
              sin_size=sizeof(struct sockaddr_in);
              
              if ((socketClient = accept(socketServer,(struct sockaddr *)&client,
                            &sin_size))==-1) {
                                perror("accept()");
                                return 1;
            }
            
            printf("Connection accepted from %s\n",
                        inet_ntoa(client.sin_addr)
            );
            
            printf("Sending welcome message...\n");
            send(socketClient,"Welcome to my server\n",22,0);
            
            printf("Closing client connection\n");
            close(socketClient);
          }
        
        close(socketServer);
        return 0;
    }
    I would appreciate any help about how to do it.

    Regards,

    Ernie-

  2. #2
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Code:
    SOCKET sockets[CLIENTS] = (SOCKET*)malloc(sizeof(SOCKET) * CLIENTS)
    ?

    Sorry. I don't know C very well. Although so far, whenever I put SOCKETs into an array or try to use them in a function, they seem to always produce errors. I don't know how that works considering its just a u_int but...
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You took the two different solutions and combined them into one (that doesn't work.)

    Use EITHER:
    Code:
    SOCKET sockets[CLIENTS]; /* no malloc, stack memory */
    OR:
    Code:
    SOCKET *sockets = malloc(sizeof(SOCKET) * CLIENTS); /* heap memory, so it's a pointer, no array */
    Also note that typecasting malloc is bad > FAQ > Casting malloc - Cprogramming.com

  4. #4
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Quote Originally Posted by memcpy View Post
    You took the two different solutions and combined them into one (that doesn't work.)

    Use EITHER:
    Code:
    SOCKET sockets[CLIENTS]; /* no malloc, stack memory */
    OR:
    Code:
    SOCKET *sockets = malloc(sizeof(SOCKET) * CLIENTS); /* heap memory, so it's a pointer, no array */
    Also note that typecasting malloc is bad > FAQ > Casting malloc - Cprogramming.com
    Thanks for the info. C confuses me at times o.e
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Rodaxoleaux View Post
    Thanks for the info. C confuses me at times o.e
    Yeah, the compiler does crazy tish like warning you of "incompatible types in assignment". WTF is that?

    Quote Originally Posted by Ernie-
    how to store that client sockets into an array
    In C, sockets are identified with file descriptors, which are ints. You know how to create an array of ints, right?
    Last edited by MK27; 03-10-2012 at 04:43 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. swapping strings (which are stored in an array)
    By nair in forum C Programming
    Replies: 2
    Last Post: 10-04-2010, 01:27 PM
  2. Hex digits stored in char array.
    By Kevinmun in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 04:05 PM
  3. stored values in elements of array, help
    By houler in forum C Programming
    Replies: 5
    Last Post: 12-09-2004, 04:19 PM
  4. Easiest way to find the max value stored in an array
    By criticalerror in forum C++ Programming
    Replies: 14
    Last Post: 01-22-2004, 03:35 PM
  5. Printing an integer stored in an array
    By Freez3L in forum C Programming
    Replies: 4
    Last Post: 11-18-2002, 02:11 AM