I'm working on a very basic client server communication service just to learn the concept. On the server side I have a function that accepts connections and lets the client communicate with the server. Whenever a connected client sends a message to the server, it sends a responding message to the client. On the client side I create one thread that listens to messages/responses from the server. This thread simply runs a loop that checks if read() returns more than 0 bytes if so it prints the message. The other threads handles user input and sending messages to the server.
Now to the question: I have a few lines of code that listens to messages from the server
void readFromServer(int fileDescriptor)
{
int nOfBytes;
char buffer[512];
while(1){
nOfBytes = read(fileDescriptor, buffer, 512);
if(nOfBytes > 0)
printf("Server says: %s \n\n>", buffer);
}
}
This means that when a message comes from the server I would like to represent it like this
>Serever says: Something
>|
but for some reason what I get is this
>>Server says: Something
|
(| pipeline represents the cursor in the console window)
Does anyone know why this is? And is there any solution to this problem?
Thanks in advance.
EDIT------------> The fork() is done in the main function like this
int pid = fork();
if(pid != 0)
readFromServer(sock);