SFML community forums

Help => Network => Topic started by: VenomOFSilence on September 14, 2022, 10:17:32 pm

Title: Why im not Receving the full string when write blankspace between words?
Post by: VenomOFSilence on September 14, 2022, 10:17:32 pm
The Code dont print the Blankspace when I type it between words in the message getline it only print first word or last letter but if i write between words any character like -./\_ it will print the full message , Example :

-Without character:
I Write : Hi how Are you
I Recive : Hi
*******
-With character:
I Write : How-how-Are-you?
I Recive : How-how-Are-you?
********

#include <iostream>
#include <SFML/Network.hpp>
using namespace std;
void server() {
    sf::TcpListener listener;
    listener.listen(5301);
    sf::TcpSocket socket;
    listener.accept(socket);
    cout << "New client connected: " << socket.getRemoteAddress() << endl;
    while(true){
                char buffer[1024];
                size_t received = 0;
                socket.receive(buffer, sizeof(buffer), received);
                cout << "Client : " << buffer << endl;
                string message;
                cout << "Enter Message  :";
                cin >> message;
                socket.send(message.c_str(), message.size() + 1);
                }
}
void client() {
    string consoleInput;
    cout << "Input server address: " << endl;
    cin >> consoleInput;
    sf::TcpSocket socket;
    socket.connect(consoleInput, 5301);
    while(true){
                string message;
                cout << ": ";
                cin >> message;
                socket.send(message.c_str(), message.size() + 1);
                char buffer[1024];
                size_t received = 0;
                socket.receive(buffer, sizeof(buffer), received);
                cout << "Server:" << buffer << endl;
                }
}
int main()
{
    cout << "Client[c] or server[s]? " << endl;
    string consoleInput;
    cin >> consoleInput;
    if (consoleInput == "c")
    {
        client();
    }
    else if (consoleInput == "s")
    {
        server();
    }

    return 0;
}
 
Title: Re: Why im not Receving the full string when write blankspace between words?
Post by: eXpl0it3r on September 20, 2022, 01:27:48 pm
Some istream functions will stop not just at new line, but at any whitespace.
I don't see that in your code directly, but since you mention getline, I wonder whether your code actually is the posted code here?

socket.receive(buffer, sizeof(buffer), received);
You should really be using std::array instead and not rely on sizeof to get the size of the array.