I have one server, and multiple clients running as seperate tasks, NOT children. When a client initially writes to the named pipe, the server does a line feed. I have no idea where this comes from. It does a line feed, but does not display the buffer. After that, for every client write to the server, the server shows the previous record. In other words, I never see the first write until a second write occurs, and then the server display is always one record behind. I don't understand why the first write stays in the pipe until a second write occurs. Obviously, I need the first write to display when I perform the first write.
Code:
/*************************************
* Server Server Server Server *
* server_test_1 *
*************************************/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
char *ServerPipe="/tmp/Server.pipe";
int main(int argc, char *argv[])
{
int Server;
int BytesIn;
char Buffer[200] = {0};
int BufferSize = sizeof(Buffer);
if (access(ServerPipe, F_OK) != 0) {
if (mkfifo(ServerPipe, S_IRWXU) != 0) {
printf("\n\nCan't create the FIFO\n\n");
return(10);
}
}
printf("\n\nServer - Waiting for Client to press Q\n\n");
Server = open(ServerPipe, O_RDONLY);
do {
BytesIn=read(Server, Buffer, BufferSize);
printf("\nBytesIn: %d %d - %s", BytesIn,
strlen(Buffer),
Buffer);
Buffer[1] = '\0';
} while (Buffer[0] != 'Q');
sleep(1);
close(Server);
unlink(ServerPipe);
printf("\n\nTat's all folken....\n\n");
return(0);
}
/***************************************
* Client Client Client Client Client *
* *
* client_test_2 *
***************************************/
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <termios.h>
char *ServerPipe="/tmp/Server.pipe";
int main(int argc, char *argv[])
{
int Server, x = 0;
char Buffer[200] = {0};
int BytesOut;
printf("\n\nClient_Test_1\n\n");
if (access(ServerPipe, F_OK) != 0) {
printf("\n\nServer not running!!!!\n\n");
return(10);
}
Server=open(ServerPipe, O_WRONLY);
do {
printf("\nPress 'Q' to quit, 'S' to send...");
Buffer[0] = getchar();
if (Buffer[0] == 'S') {
sprintf(Buffer, "Test record %d from PID: %d", ++x,
getpid());
printf("\nSending...");
BytesOut = write(Server, Buffer, sizeof(Buffer));
printf("BytesOut: %d", BytesOut);
}
}
while (Buffer[0] != 'Q');
Buffer[1] = '\0';
write(Server, Buffer, strlen(Buffer));
close(Server);
printf("\n\nThat's all folks....\n\n");
return(0);
}