Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Why im not Receving the full string when write blankspace between words?  (Read 2436 times)

0 Members and 1 Guest are viewing this topic.

VenomOFSilence

  • Newbie
  • *
  • Posts: 2
    • View Profile
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;
}
 
« Last Edit: September 20, 2022, 01:21:19 pm by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: Why im not Receving the full string when write blankspace between words?
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything