winsock server question?

This is a discussion on winsock server question? within the Windows Programming forums, part of the Platform Specific Boards category; hey guys! first time here... hope to stay for long! cheers to everyone! I found this example - this server ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    Exclamation winsock server question?

    hey guys!

    first time here... hope to stay for long! cheers to everyone!

    I found this example - this server serves only one client at a time,
    what do I need to change to it allows multiple clients to connect to?

    Code:
    #ifndef _WINSOCKAPI_
    #define _WINSOCKAPI_
    #endif
    
    #include <windows.h>
    #include <winsock2.h>
    
    #include <stdio.h>
    
    // Declare the sockets we use.
    
    
    SOCKET listeningSocket;
    SOCKET connectedSocket;
    
    int InitSockets(void)
    {
      struct sockaddr *servAddr;
      struct sockaddr_in *inetServAddr;
      int error = 0;
      // Create the socket.
      listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
      if(listeningSocket == INVALID_SOCKET) {
        printf("error: socket() failed");
        return 1;
      }
     
      // Allocate memory for the address structure and set it to zero.
      servAddr = (struct sockaddr *) malloc(sizeof(sockaddr));
      memset((char *) servAddr, 0, sizeof(sockaddr));
      
      // Fill the address structure.
      servAddr->sa_family = (u_short) AF_INET;
      inetServAddr = (struct sockaddr_in *) servAddr;
      inetServAddr->sin_port = htons((u_short) 9009);
    
      // Bind the address information to the socket.
      error = bind(listeningSocket, servAddr, sizeof(sockaddr));
      if(error == SOCKET_ERROR) {
        printf("error: bind() failed");
        free(servAddr);
        return 1;
      }
    
      free(servAddr);
      servAddr = NULL;
     
      // Listen for incoming connections. Queue only one connection.
      error = listen(listeningSocket, 1);
      if(error == SOCKET_ERROR)  {
        printf("error: listen() failed");
        return 1;
      }
      
      // Accept the connection. Accept is a blocking function.
      connectedSocket = accept(listeningSocket, NULL, NULL);
      if(connectedSocket == INVALID_SOCKET)
    
    
      void ServerProcess(void)
      {
         int connectionOpen;
         char buf[2];
         connectionOpen = 1;
      
        // Loop as long as connection is open.
        while(connectionOpen) {
           // Read the incoming data from the connected socket.
          if(recv(connectedSocket, buf, 2, 0)) {
            // Set the received letter to uppercase and
            // make sure the string ends after that by setting the next
            // byte to NULL.
            buf[0] = toupper(buf[0]);
            buf[1] = '\0';
            printf("Got message from client: %s\n", buf);
            // Send the feedback.
            if(send(connectedSocket, buf, 2, 0) == SOCKET_ERROR) {
              connectionOpen = 0;
            }
          }
          else {
              closesocket(connectedSocket);
              connectionOpen = 0;
          }
       }
    }
    int main(void) {
    
      if(NET_WinSockInitialize() != 0) {
         printf("Critical error, quitting\n");
         return 1;
      }
      
      if(InitSockets() != 0) {
         printf("Critical error, quitting\n");
         WSACleanup();
         return 1;
      }
    
      ServerProcess();
      WSACleanup();
      return 0;
    }
    If client code is needed - I can supply! Thanks!

    regards.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    use threds. Look up the CreateThread function in MSDN. Call accept in a loop. Each time you accept a new connection spawn a new thread with CreateThread and pass the new socket as the paramater.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Look up the CreateThread function in MSDN
    He shouldn't use CreateThread() since he is linking to the crt library. Use _beginthread() or _beginthreadex() instead.

    Another option is to use asynchronous sockets. This allows you to use a multiple socket paradigm without creating multiple threads. Read up on WSAAsyncSelect for more information on this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dumb question time - Winsock & accept()
    By jdinger in forum Networking/Device Communication
    Replies: 5
    Last Post: 06-11-2005, 02:08 AM
  2. Question on returning stdout from a server
    By verbena1 in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-29-2005, 01:59 PM
  3. winsock question
    By the Wookie in forum C++ Programming
    Replies: 12
    Last Post: 10-17-2002, 03:18 AM
  4. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 02:54 PM
  5. win32 Winsock problem
    By spoon_ in forum Windows Programming
    Replies: 3
    Last Post: 07-19-2002, 02:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21