blob: c218edb696da3b67d2d3d0e320faad85c7d07587 [file] [log] [blame]
#include "Server.h"
#ifndef _MSC_VER
void handler(int snum) {
if (snum == SIGUSR1) {
Recieve_message();
kill(pnum, SIGUSR1);
} else if (snum == SIGUSR2) {
close(fd);
remove(NAMEDPIPE_NAME);
exit(0);
}
}
#endif
int main(int argc, char *argv[]) {
cout << "Now run client program." << endl;
#ifdef _MSC_VER
HANDLE hEvent;
hEvent = CreateEvent(NULL, FALSE, FALSE, SYNC_EVENT);
hNamedPipe = CreateNamedPipeA(
lpszPipeName,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE |
PIPE_READMODE_MESSAGE |
PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
BUF_SIZE,
BUF_SIZE,
5000,
NULL);
if (hNamedPipe == INVALID_HANDLE_VALUE) {
perror("CreateNamedPipe");
return 1;
}
if (!ConnectNamedPipe(hNamedPipe, NULL)) {
perror("ConnectNamedPipe");
}
#else
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = handler;
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
sigaddset(&set, SIGUSR2);
act.sa_mask = set;
sigaction(SIGUSR1, &act, NULL);
sigaction(SIGUSR2, &act, NULL);
cout << "Server pid is " << getpid() << endl;
cout << " Client pid: ";
cin >> pnum;
if ((fd = open(NAMEDPIPE_NAME, O_RDWR)) <= 0) {
perror("openfile");
return 1;
}
kill(pnum, SIGUSR1);
#endif
cout << "Recieved messages:" << endl;
#ifdef _MSC_VER
while (true) {
WaitForSingleObject(hEvent, INFINITE);
Recieve_message();
}
#endif
while (true) {
sleep(1);
}
return 0;
}
#ifdef _MSC_VER
void Recieve_message() {
if (ReadFile(hNamedPipe, buffer, BUF_SIZE, &cbRead, NULL)) {
cout << buffer << endl;
}
else {
DisconnectNamedPipe(hNamedPipe);
CloseHandle(hNamedPipe);
exit(0);
}
}
#else
void Recieve_message() {
memset(readbuffer, '\0', BUF_SIZE);
memset(buffer, '\0', BUF_SIZE);
read(fd, readbuffer, BUF_SIZE - 1);
cout << readbuffer << endl;
}
#endif