Вопрос

I am programming a server / client communication system where a client requests to log in to the server and can request to view other client's online status. I can make the client log in fine, but when I try to log in (successfully) and then send another packet requesting another client's information, the server does not receive that packet.

the main part of the server, not the technical connection stuff starting from bind Server:

Users client[2]; //I intialized them already

//Bind
bind(WelcomeSocket, (sockaddr*)&SvrAddr, sizeof(SvrAddr));

//listening
listen(WelcomeSocket, 5);

//temp users
Users temp;

//while loop for the connection 
while (1) {

    ConnectionSocket = accept(WelcomeSocket, NULL, NULL);



    if (recv(ConnectionSocket, RxBuffer, sizeof(RxBuffer), 0))
        cout << "WORKEDDDDDDDD" << endl;

    memcpy(&temp, RxBuffer, sizeof(struct Users));

    cout << temp.message << temp.userName << endl << endl;

    //check which message type is being sent
    switch(temp.message) {

    //if message type 1
    case 1 :
        for (int i = 0; i < 2; i++) {

            //if receieved username matches with any username in the database
            if (strcmp(temp.userName, client[i].userName) == 0) {

                //assign the recieved users information to the matched one in database
                strcpy(client[i].userName, temp.userName);
                client[i].online = true;
                client[i].message = 2;

                cout << client[i].userName << endl << client[i].online << endl;

                //send the acknowledgement packet
                send(ConnectionSocket, (char *)&client[i], sizeof(struct Users), 0);
            }

        }
        closesocket(ConnectionSocket);
        break;

    //if message type 3
    case 3 :
        cout << "3";
        break;

    default :
        break;

    }

}

closesocket(ConnectionSocket);
WSACleanup();
}

Client:

connect(ClientSocket, (sockaddr*)&SvrAddr, sizeof(SvrAddr));



//cout << "Name: ";
//cin >> login;

//Send request to login
int log;
char * name = new char[128];
char * request = new char[128];
Users client;
Users talkto;



    cout << "To login press (1). ";
    cin >> log;
    flushall();

    if (log == 1) {

        cout << "Username : ";
        cin.getline(name, 128, '\n');
        flushall();

        //Set client login info
        strcpy(client.userName, name);
        client.message = 1;



        send(ClientSocket, (char *)&client, sizeof(struct Users), 0);


        //Recieve acknowledgement
        recv(ClientSocket, RxBuffer, sizeof(RxBuffer), 0);
        //create temp users
        Users temp;

        memcpy(&temp, RxBuffer, sizeof(struct Users));



        if (temp.message == 2) {

            cout << "Enter user for user information: ";
            cin.getline(talkto.userName, 128, '\n');
            flushall();
            talkto.message = 3;

            //send request for user information packet
            if (send(ClientSocket, (char *)&talkto, sizeof(struct Users), 0))
            cout << "SENDT" << endl;
        }

        //cout << temp.userName << endl << temp.online << endl << temp.message;

        closesocket(ClientSocket);


WSACleanup();

}

The structure for users

struct Users {

int message;
char userName[50];
char ipAddress[50];
int PortNumber;
bool online;

};

Not sure why it's not receiving information more than one time

Это было полезно?

Решение

ConnectionSocket = accept(WelcomeSocket, NULL, NULL);

You should put that before the loop, not inside, because the accept function is waiting for another client, blocking you from receiving another packet.

Also, when your recv call returns 0, it means that the connection has closed, and it's the end of the stream, so you should break from your loop when recv returns 0, or some unexpected SOCKET_ERROR.

Другие советы

For each client connection, your server performs accept-recv-send-closesocket. Period.

A connection to client is closed after the server processes the first packet. You have either to establish a new connection before sending each packet at client side (but that would probably make your login procedure useless) or to make server be able to maintain several concurrent client connections polling them in an infinite loop, maybe in a separate thread(s).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top