0

I can't figure out how to accept multiple connections for a simple tcp server using winsock.

I've tried a couple different ways and I can't figure out how to get another connection to work. The first call to accept() seems to be the only one that works. Can someone provide a simple example of how to do this or explain what's going on with listen() and accept() here?

#include <string>
#include <Winsock2.h>

WSADATA WsaData; 
unsigned char packet_in[64];
unsigned short port = 29992;
unsigned int max_packet_size =  sizeof(packet_in);
sockaddr_in xaddress;
SOCKET sock;
SOCKET sock1 = INVALID_SOCKET;
SOCKET sock2 = INVALID_SOCKET;

int main()
{   

    int r = WSAStartup( MAKEWORD(2,2),  &WsaData );
    xaddress.sin_family = AF_INET; 
    xaddress.sin_port =  htons(port);
    DWORD nonBlocking = 1;
    sockaddr_in from; 
    int fromLength = sizeof( from );  
    int count = 0;

    sock = socket( AF_INET,  SOCK_STREAM,  IPPROTO_TCP );   

    r = bind(sock, (const sockaddr*) &xaddress,  sizeof(sockaddr_in));
        if (r != 0) printf("%d\n", WSAGetLastError());

    r = listen(sock, SOMAXCONN);
    if (r == SOCKET_ERROR)
            printf("%d\n",WSAGetLastError());

    ioctlsocket(sock, FIONBIO, &nonBlocking);

    while(true)
    {

        if(count == 0)
        {
            sock1 = accept(sock, NULL, NULL);

        }
        else if (count == 1)
        {
            sock2 = accept(sock, NULL, NULL);

        }

        if(sock1 != INVALID_SOCKET)
            count = 1;
        if(sock2 != INVALID_SOCKET)
            count = 2;
        printf("%i\n",count);


    }
    return 0;
}
dan b
  • 33
  • 2
  • 5
  • Does it get inside your second `if statement -- if (count == 1)`? You should post the output and errors you get after a call to `WSAGetLastError`. – Brandon Jan 01 '16 at 19:50
  • Try: `struct sockaddr_storage ss; socklen_t slen = sizeof(ss); SOCKET fd = accept(listener, (struct sockaddr*)&ss, &slen); ioctlsocket(sock, FIONBIO, &nonBlocking);` – Brandon Jan 01 '16 at 19:59
  • Possible duplicate of [TCP Winsock: accept multiple connections/clients](http://stackoverflow.com/questions/15185380/tcp-winsock-accept-multiple-connections-clients) – Ilya Jan 01 '16 at 20:11
  • Get rid of the non-blocking mode. You don't have anything else to do in the accept thread, or you shouldn't. – user207421 Jan 01 '16 at 23:01

2 Answers2

0

I don't know how to network programming on Windows, but it basically looks like on Linux, so this is small example for Linux.

struct sockaddr_in server_addr
socklen_t len = sizeof(server_addr);
...
while(1)
{
   struct sockaddr_in client_addr;
   int client_fd;
   if((client_fd = accept(server_sock, (struct sockaddr*)& client_addr, &len)) < 0)
   {
      cerr<<"accept() error"<<endl;
      continue;
   }
   ...
}

There is full example written in Polish, but you should understand: full example (find //server.c)

Main differences between network programming on Windows and Linux are structures and classes, so it should give you some knowledge: Beej's guide to network programming

ktoś tam
  • 739
  • 1
  • 6
  • 11
0
  • I think from this simple program, You will get better idea :)

    // Multi-Client - Server chat application
    
        #include"header.h"
    
        void isr(int n)
        {
        printf("Client is Disconnected ..\n");
        kill(getpid(),9);
        return;
        }
    
        main()
        {
            int sockfd,nsfd,len,pid,size;
            char buffer[500],buffer1[500];
            struct sockaddr_in server;
        ///////////////////////////////////////////////////////////////////////
            sockfd=socket(AF_INET,SOCK_STREAM,0);
            if(sockfd==0)
            {
            printf("Socket file is not created socket is fail...\n");
            return;
            }
            ////////////////////////////////////////////////////////////////////////
            server.sin_family=AF_INET;
            server.sin_port=htons(9898);
            len = sizeof(server);
          /////////////////////////////////////////////////////////////////////
            bind(sockfd,(struct sockaddr*)&server,len);
            perror("bind");
          /////////////////////////////////////////////////////////////
            listen(sockfd,5);
            perror("listen");
           /////////////////////////////////////////////////////////////////
            bzero(buffer,sizeof(buffer));
            zero(buffer1,sizeof(buffer1));
            //////////////////////////////////////////////////////////////////////
           while(1)
           {
            nsfd=accept(sockfd,(struct sockaddr *)&server,&len);
                if(nsfd<0)
                {   
                    perror("accept");
                    return;
                }
    
               if(fork()==0)
                {
                    while(1)
                    {
                        size=recv(nsfd,buffer,sizeof(buffer),0);
    
                        if(size==0)
                        {
                        printf("Server is Disconnected...\n");
                        return;
                        }
    
                    printf("Data:%s\n",buffer);
                    bzero(buffer,sizeof(buffer));
    
                    }
               }
            else
            {
                printf("Enter the massage:\n");
                signal(17,isr);
    
                  while(1)
                  {
                      gets(buffer);
                      send(nsfd,buffer,strlen(buffer)+1,0);
                       bzero(buffer,sizeof(buffer));
                 }
            }            
      }
    }