Good afternoon, I encountered the following problem. When sending a message from the client to the server, the following happens: if there is a space in the entered message (for example, Hello world), then it comes to the server not with one message, but with two. That is, hello comes separately, world separately. However, if I zahardkozhivayu message (for example char message [50] = "Hello world"), then it is sent correctly in one line. I can not understand what the problem is. I attach the code. Used visual studio 2012.

client.h

#ifndef CLIENT_H #define CLIENT_H #include <windows.h> #include <stdio.h> #include <conio.h> #include <iostream> #include <string> #pragma comment (lib, "ws2_32.lib") #pragma comment (lib, "mswsock.lib") class client{ public: void init(); void Connect(); void close(); private: void sendMessage(); char PCName[30], ServerName[30]; std::string Message, IP; WSAData WSADat; // Бвойства WinSock (Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ WSAStartup) sockaddr_in sin; // Бвойства(адрСс) создаваСмого сокСта SOCKET Sock; // ΠšΠ»ΠΈΠ΅Π½Ρ‚ΡΠΊΠΈΠΉ сокСт }; #endif //CLIENT_H 

client.cpp

 #include "client.h" void client::close(){ closesocket(Sock); WSACleanup(); } void client::sendMessage(){ for(;;){ std::cout << "Enter message: "; std::cin >> Message; if(Message.size() < 201){ const char *message = Message.c_str(); if (send(Sock, message, strlen(message) + 1, 0) != SOCKET_ERROR){ std::cout << "Sent!\n"; } else{ std::cout << "Error of sending!\n"; } } else{ std::cout << "Message is too big \n"; } } } void client::Connect(){ do{ std::cout << "Enter server's IP: "; std::cin >> IP; } while(IP.size() > 16); const char *Ip = client::IP.c_str(); sin.sin_addr.s_addr = inet_addr(Ip); // IP-адрСс сСрвСра (ΠΏΠΎΡ€ΠΈ создании сСрвСра ΠΌΠΎΠΆΠ½ΠΎ 0) std::cout << "Connecting to server...\n"; if (connect(Sock, (sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR) { std::cout << "Connection error!\n"; Connect(); } if ((send(Sock, (char *) &PCName, strlen(PCName) + 1, 0)) == SOCKET_ERROR){ // ΠžΡ‚ΠΏΡ€Π°Π²ΠΊΠ° ΠΈΠΌΠ΅Π½ΠΈ этого ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π° (ΠΊΠ»ΠΈΠ΅Π½Ρ‚Π°) perror("SOCKET_ERROR"); exit(1); } if ((recv (Sock, (char *) &ServerName, 30, 0)) == SOCKET_ERROR){ // ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΠΈΠΌΠ΅Π½ΠΈ ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π° сСрвСра perror("SOCKET_ERROR"); exit(1); } std::cout << "Connected to " << ServerName << std::endl; sendMessage(); } void client::init(){ WSAStartup(0x0202, &WSADat); // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ WinSock // 0x0202 - вСрсия WinSock. 2.0, 2.2 // WSADat - структура, ΠΊΡƒΠ΄Π° Π±ΡƒΠ΄ΡƒΡ‚ занСсСны Ρ€Π΅Π·. ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ gethostname(PCName, 30); // ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΠΈΠΌΠ΅Π½ΠΈ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ ПК sin.sin_family = AF_INET; // Π’ΠΈΠΏ адрСса sin.sin_port = htons(2803); // НомСр ΠΏΠΎΡ€Ρ‚Π° сСрвСра Sock = socket(AF_INET, SOCK_STREAM, 0); // Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ сокСта if(Sock < 0){ perror("socket"); exit(1); } } 

main.cpp

  #include "client.h" int main() { client client; client.init(); client.Connect(); client.close(); return 0; } 

server.cpp

  #include "server.h" int server::numClients = 0; void server::join(std::thread &th){ th.join(); } void server::creatingWorkingThreads(){ for(;;){ if(numClients < 3){ threads.push_back(std::thread ([this](){connect();})); numClients++; } } std::for_each(threads.begin(), threads.end(), std::bind(&server::join, this, std::placeholders::_1)); } void server::getMessage(){ for(;;){ if (recv(Client, Message, 200, 0) != SOCKET_ERROR) { std::cout << "Message from " << ClientName << " : " << Message << std::endl; } else{ std::cout << ClientName << ": " << "has disconnected" << std::endl; numClients--; break; } } } void server::init(){ WSAStartup(0x0202,&WSADat); // Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ WinSock // 0x0202 - вСрсия WinSock. // WSADat - структура, ΠΊΡƒΠ΄Π° Π±ΡƒΠ΄ΡƒΡ‚ занСсСны Ρ€Π΅Π·. ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ gethostname(PCName, 30); // ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΠΈΠΌΠ΅Π½ΠΈ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ ПК sin.sin_family = AF_INET; // Π’ΠΈΠΏ адрСса sin.sin_addr.s_addr = 0; // IP-адрСс сСрвСра (ΠΏΠΎΡ€ΠΈ создании сСрвСра ΠΌΠΎΠΆΠ½ΠΎ 0) sin.sin_port = htons(2803); // НомСр ΠΏΠΎΡ€Ρ‚Π° сСрвСра Sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ сокСта if(Sock < 0){ perror("socket"); exit(1); } if(bind(Sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) { // БвязываниС созданного сокСта с адрСсом sin perror("bind"); exit(2); } // ***** ОТиданиС ΠΊΠ»ΠΈΠ΅Π½Ρ‚Π° std::cout << "Wait of client...\n"; listen(Sock, SOMAXCONN); // ΠŸΡ€ΠΎΡΠ»ΡƒΡˆΠΈΠ²Π°Π½ΠΈΠ΅ сокСта сСрвСром (для ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ ΠΊΠ»ΠΈΠ΅Π½Ρ‚Π°) } void server::connect(){ Client = accept(Sock, (sockaddr*)&sin, 0); if ((recv (Client, (char *) &ClientName, 30, 0)) == SOCKET_ERROR){ // ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΠΈΠΌΠ΅Π½ΠΈ ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π° ΠΊΠ»ΠΈΠ΅Π½Ρ‚Π° perror("SOCKET_ERROR"); exit(1); } if ((send(Client, (char *) &PCName, strlen(PCName) + 1, 0)) == SOCKET_ERROR){ // ΠžΡ‚ΠΏΡ€Π°Π²ΠΊΠ° ΠΈΠΌΠ΅Π½ΠΈ этого ΠΊΠΎΠΌΠΏΡŒΡŽΡ‚Π΅Ρ€Π° (сСрвСра) perror("SOCKET_ERROR"); exit(1); } std::cout << "Client " << ClientName << " has connected" << std::endl; getMessage(); } void server::close(){ closesocket(Sock); closesocket(Client); WSACleanup(); } 

server.h

 #ifndef SERVER_H #define SERVER_H #include <windows.h> #include <stdio.h> #include <conio.h> #include <tchar.h> #include <algorithm> #include <vector> #include <thread> #include <iostream> #pragma comment (lib, "ws2_32.lib") #pragma comment (lib, "mswsock.lib") class server{ public: void init(); void close(); void creatingWorkingThreads(); private: char PCName [30], ClientName[30], Message[200]; WSAData WSADat; // Бвойства WinSock (Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ WSAStartup) sockaddr_in sin; // Бвойства(адрСс) создаваСмого сокСта SOCKET Sock, Client; // Π‘Π΅Ρ€Π²Π΅Ρ€Π½Ρ‹ΠΉ ΠΈ клиСнтский сокСты static int numClients; void getMessage(); void connect(); void join(std::thread &th); std::vector<std::thread> threads; }; #endif //SERVER_H 

main.cpp

 #include "server.h" int main() { server server; server.init(); server.creatingWorkingThreads(); server.close(); return 0; } 

what does the attached codeoption with hard message

    1 answer 1

    std::cin reads the string up to the first whitespace. To read a line with spaces, use getline () .

    • Thank you very much, now everything works correctly. But there was still a problem, From the same opera, apparently I sorely lack understanding in this matter. I enter the ip, the connection passes, but before I can send the first message, immediately after the connection, the client sends an empty string. The code is the same, but instead of std :: cin >> message, I used, as you said, std :: getline (std :: cin, Message); - zAvr