Hello.
Please help me figure out the next problem. In the server with c ++ I call the accept function to create a connection.

int sock = accept(sd, NULL, NULL); 

And then I try to display the value of sock, but the screen is empty. It feels like the server just doesn't go out of acccept.
What can it be connected with?

 struct sockaddr_in local; local.sin_family = AF_INET; local.sin_port = htons(1024); local.sin_addr.s_addr = htonl(INADDR_ANY); int sd; if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); exit(1); } if(bind(sd,(struct sockaddr*)&local,sizeof(local))<0) { perror("bind"); exit(2); } listen(sd, 5); pthread_t tid; int newS; while(1) { newS = accept(sd, NULL, NULL); printf("%d", newS); } close(sd); 
  • accept blocking, it will wait until someone connects (either a signal comes, or someone simply presses ctrl + c, but this is also a signal). - KoVadim
  • Does anyone (client) connect? otherwise this line usually works (I just don't write on the pros) after connecting. - rasmisha
  • The fact of the matter is, what's next, when the server starts up, the client connects successfully, but nothing is displayed on the server anyway - carapuz
  • And why in the buffer? I would still like to see the screen - carapuz
  • Thank you, really, it’s all so. Explain, please, that is, "\ n" does not just print the carriage to the next line, but also pull everything from the buffer to the screen? - carapuz

1 answer 1

What should? The connection is established, printed (into the buffer, not on the screen) the socket number, and the program hangs on the next accept'e. Everything is good. Add the line feed "%d\n" to the format. Then the screen will chat.