Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?
********


C++
<pre>#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;
}


What I have tried:

Im confused how to slove this , i search in the sfml forum for same problem i didnt find anything connected with my problem.
Posted
Updated 14-Sep-22 14:51pm
Comments
Greg Utas 14-Sep-22 17:28pm    
I haven't used this library, but you should try stepping through this code with a debugger to see if any surprises show up. And I'm fairly certain that all of the socket functions return something which indicates success, failure, or how many bytes were sent or received. You should be checking all of those results and writing warnings to the console if anything unexpected occurs. And since this is TCP, it's possible (although unlikely) that a space causes the previous characters to be sent. Don't forget that there is no such thing as a "complete message" in TCP: when you invoke send, the contents can get split into multiple packets or buffered and combined into a larger packet.
jeron1 14-Sep-22 18:06pm    
Possibly try using getline() instead of cin >>, I believe cin will return when the first whitespace character is encountered, whereas getline will retrieve the entire string up until the newline '\n' character.

1 solution

jeron1 gave you the answer. Here are a couple of references you should consult to verify what was stated : operator>> (string) - C++ Reference[^], this one describes the redirection operator and this one : istream::getline - C++ Reference[^] describes getline. They both include sample code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900