This error is most likely a more general C error than it is related to the use of sockets, but here's what I have
I have code for a server and code for a client that I have been working on. The client essentially sends messages to the server and the server terminal prints whatever was being sent. For this situation, I'm not going to include the client.c file unless requested, because it is not where the issue is located.
From server.c:
while(1)
{
if ((newsockfd=accept(sockfd, (struct sockaddr*)&cli_addr, &clilen)) < 0)
{
perror("accept");
exit(0);
}
usleep(2000);
printf("Server received: ");
while (read(newsockfd, &buffer, 256) > 0)
printf("%s", buffer);
printf("\nServer: Message end. Waiting for next connection.\n");
}
The output I'm getting when receiving input looks like the following:
Server received: *first message from client*
*weird gibberish* *second message from client*
*weird gibberish* *third message from client*
...
Server: Message end. Waiting for next connection.
The weird gibberish varies depending on the specific server instance that is being run, but until that process ends, the gibberish is identical in each appearance.