recieving on client

Started by
2 comments, last by hplus0603 19 years, 3 months ago
hi im using one of the tutorials and have the client recieving the message it sends straight back to the server. but i when multiple clients are connected i cant get it to recieve messages from the other client. i assume i need some sort of loop that will constantly check from the server or does the client need threading? ##part of client code, after it connects int nBytes; // I will define a message size so we will know how much data we will send and receive #define MAX_MESSAGE_SIZE 4096 char buffer[MAX_MESSAGE_SIZE]; printf("Input text to send and press Enter\nType 'quit' to exit\n"); // the main loop for (;;) { //user input gets(buffer); // check if quit was typed, then exit if (strcmp(buffer, "quit") == 0) { break; } // get message size unsigned long messageSize = strlen(buffer); // fix our byte ordering messageSize = htonl(messageSize); // send the message size if ((nBytes = send(mySocket, (char*)&messageSize, sizeof(messageSize), 0)) == SOCKET_ERROR) { printf("Send Failed!\n"); } // re-fix our byte ordering messageSize = ntohl(messageSize); // send the actual message if ((nBytes = send(mySocket, buffer, messageSize, 0)) == SOCKET_ERROR) { printf("Send Failed!\n"); } //recieve msg nBytes = recv(mySocket,(char*)&messageSize, sizeof(messageSize), 0); // check for errors if (nBytes == SOCKET_ERROR) { printf("Recv Failed!\n"); } messageSize = ntohl(messageSize); nBytes = recv(mySocket, buffer, messageSize, 0); // check for errors if (nBytes == SOCKET_ERROR) { printf("Recv Failed!\n"); } buffer[messageSize] = '\0'; printf("Message Received from server on %d : %s\n","", buffer); } // shutdown our client shutdownClient(mySocket); }
Advertisement
I haven't exactly read your code, use source tags ;). But from what I've seen, you only seem to have 1 client socket in there. That won't work since each socket needs to be checked.

I assume you're using C++, so I advice writing a socket class to wrap things up nice, and look into non-blocking servers(There is an article here on gdnet).

Basicly, you have either an std::list, custom linked list or just an array for sockets. In this case, I use an std::list as example:
std::list<Socket>::iterator it;for (it = SocketList.begin(); it != SocketList.end(); ++it){    Socket &SockRef = (*it);    if (SockRef.HasData())    {        char szBuff[1024];        if (SockRef.Receive(szBuff, 1024) == SOCKET_ERROR)            printf("ERROR!");        MessageReceived(szBuff); // Custom function    }}


That's basicly a simple main loop which doesn't check for disconnects or do specific error checking.

Make sure to look into non-blocking sockets and how to find out if data is pending. The function HasData() uses the socket's select() function to find out if there is any data on the socket.

Toolmaker

cheerz for the reply.

i dont really wanna use any classes while im learning.

it basically

->connects fine
->sends message to server fine

the code in the main for loop
sends the message then straight after recieves the message

theres no code implementation for constantly listening for a new msg
that the server will send to all clients, and im not to good at
programming so dont know what to do.


//recieve msg nBytes = recv(mySocket,(char*)&messageSize, sizeof(messageSize), 0);messageSize = ntohl(messageSize);nBytes = recv(mySocket, buffer, messageSize, 0);buffer[messageSize] = '\0';printf("Message Received from server on %d : %s\n","", buffer);


this is the recieve message part

error checking removed to cut the code down
You need to learn the use of select(). And, no, you don't need threads. The Forum FAQ might contain links to articles and tutorials that will help you along the way.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement