Greetings, problem with recv. Takes messages together what is the error, how to fix it? If you determine in advance how here, everything is sent correctly. I want to put \ n after each adoption, but then it accepts each word that was entered, separated by a space, on a new line.

//Send char buff[512] = "Helloy client\n"; int sen = send(AcceptSocket, buff, strlen(buff), 0); while(1) { int rec = recv(AcceptSocket, buff, sizeof(buff), 0); printf("%s", buff); scanf("%s",&buff); send(AcceptSocket, buff,sizeof(buff), 0); }; Привет друг! //Отсылаем данные Приветдруг! // Такие данные придут клиенту или же если отправлены наоборот 

Similarly with the Client

  • rewrite the question. Give examples of input and output. Give a real example (judging by the question, you gave the code in which everything works). - KoVadim
  • Suppose if you write 2 words Hello friend! Then he thinks like: Hello friend! And so on that the client that the server - User

2 answers 2

For a start - even though it is not to your question - pass the null character -

 int sen = send(AcceptSocket, buff, strlen(buff) +1, 0); ^^^ 

or add it later ..

And the problem is that scanf("%s",&buff); reads one word , not the entire line. Use, for example, fgets :

 fgets(buff,sizeof(buff),stdin); 
  • It is possible an example with fgets, I didn’t really understand how to use it - User
  • fgets() will wait for a long time if the connection is not terminated and not in the stream 'n . - 0andriy
  • @ 0andriy Yes, yes, of course, and scanf also works over the network ... Do you even see what is written, for a start ... - Harry
  • Added a description of fgets ... - Harry
  • Sori, I did not do it myself, now I will correct it - User

Try after recv(AcceptSocket, buff, sizeof(buff), 0); declare string ResponseBuffer(buff); and use the next ResponseBuffer with all the features of the string. I had no problems with this practice.

  • all @Harry the answer helped decided, I just simply programmed) Thanks and you) - User