I have this code but I only eccepts one client at a time:

#include <iostream>
#include <conio.h>
#include <winsock2.h>
#include <string>

using namespace std;


int main()
{
    WSADATA wsadat;
    WORD rVersion;
    rVersion = MAKEWORD(2,0);

    if(WSAStartup(rVersion, &wsadat) != NO_ERROR)
    {
        cout<<"WSA initialization failed.\n";
        WSACleanup();
        return 1;
    }

    //Creating the welcome socket
    SOCKET welcome_socket;
    welcome_socket = socket(AF_INET, SOCK_STREAM, 0);

    if(welcome_socket == INVALID_SOCKET)
    {
        cout<<"Socket could not be created.\n";
        WSACleanup();
        return 1;
    }

    string ipaddress;
    int portno;
    cout<<"Enter your IP address: ";
    cin>>ipaddress;
    //ipaddress = "127.0.0.1";
    cout<<"Enter the port no.: ";
    cin>>portno;
    //portno=10000;

    SOCKADDR_IN sockaddress;
    sockaddress.sin_addr.s_addr = inet_addr(ipaddress.c_str());
    sockaddress.sin_port = htons(portno);
    sockaddress.sin_family = AF_INET;

    if(bind(welcome_socket, (SOCKADDR*)(&sockaddress), sizeof(sockaddress)) == SOCKET_ERROR)
    {
        cout<<"Attempt to bind failed.\n";
        WSACleanup();
        return 1;
    }

    //listen for incoming connection requests on the welcome socket


    listen(welcome_socket, 1);

    //create a socket for communication by accepting any requests on welcome socket


    SOCKET comm_sock;
    SOCKADDR_IN clientaddr;
    int addresslen = sizeof(clientaddr);
    char*Buffer = new char[13];

    int turn=0;
    char* buf;
    buf = new char[50];
    char temp;
    while(!kbhit())
    {
            comm_sock = accept(welcome_socket, (SOCKADDR*) &clientaddr, &addresslen);
            if(comm_sock != SOCKET_ERROR)
            {
                cout<<"Connection established with client at: "
                    <<(int)clientaddr.sin_addr.S_un.S_un_b.s_b1<<"."
                    <<(int)clientaddr.sin_addr.S_un.S_un_b.s_b2<<"."
                    <<(int)clientaddr.sin_addr.S_un.S_un_b.s_b3<<"."
                    <<(int)clientaddr.sin_addr.S_un.S_un_b.s_b4<<endl;
            }
            while(buf !="exit")
            {
                if(turn==0)
                {
                        cout<<endl;
                        //send and receive data;
                        recv(comm_sock, buf, 50, 0);

                        if(buf[0]=='e' && buf[1]=='x' && buf[2]=='i' && buf[3]=='t')
                            return 1;

                        cout<<"Msg from client: "<<buf<<endl;
                        cin.get(temp);
                        turn=1;

                }


                if(turn==1)
                {
                    cout<<"Enter Message: ";
                    cin.get(buf,50);

                if(buf[0]=='e' && buf[1]=='x' && buf[2]=='i' && buf[3]=='t')
                    return 1;

                    send(comm_sock, buf,strlen(buf)+1,0);
                    turn=0;
                }
            }
    }

    WSACleanup();
    return 1;
}

I need it to accepts multiple clients and recv messages from all of them and then write it to a file. I have the file writing part covered just not the accepting multiple clients and recieving multiple message.

Thanks....

Recommended Answers

All 4 Replies

Add multithreading. Put line 57 in a tight loop, inside that loop after listen() returns create a new thread. lines 62-110 should be in another function which is the thread code. The newest version of c++ standards includes new standards for creating threads which I have not read or used. You can use either that or the operating system-dependent way of creating threads. Since you didn't say what operating system you are using I can't help you further with that. I'd suggest you use the new c++ standards for creating threads so that your program can be os independent as much as possible.

[edit]Here is a link you might want to read.

Also, for a server, you don't need to bind the socket, but you need to do an accept() before you listen() for connection requests. A client will bind().

Oops. You listen() first (sets up the incoming queue length), and then you loop, calling accept() to get connection requests from clients. Sorry, my answer was backwards in the previous posting... my bad! :-)

Ok. I missed your call to listen() in the code example. You are basically doing it ok, but you may want to increase your queue length (2nd arg to listen()) to some number bigger than one if you may be getting multiple connection requests at the same time.

Also, looking further at your code, this line:

while(buf !="exit")

Won't work. You need to do this instead:

while(strcmp(buf, "exit") != 0)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.