| #include "Client.h" |
| |
| #ifndef _MSC_VER |
| void handler(int snum) { |
| if (snum == SIGUSR1) { |
| Send_message(); |
| kill(pnum, SIGUSR1); |
| } |
| } |
| #endif |
| |
| int main(int argc, char *argv[]) { |
| |
| #ifdef _MSC_VER |
| |
| if (!(hEvent = OpenEvent(EVENT_ALL_ACCESS | EVENT_MODIFY_STATE, TRUE, SYNC_EVENT))) { |
| cout << "Run server program before." << endl; |
| system("pause"); |
| return 0; |
| } |
| |
| hNamedPipe = CreateFileA( |
| szPipeName, |
| GENERIC_READ | GENERIC_WRITE, |
| 0, |
| NULL, |
| OPEN_EXISTING, |
| 0, |
| NULL); |
| |
| if (hNamedPipe == INVALID_HANDLE_VALUE) { |
| cout << "Run server program before." << endl; |
| system("pause"); |
| return 0; |
| } |
| |
| #else |
| |
| struct sigaction act; |
| memset(&act, 0, sizeof(act)); |
| act.sa_handler = handler; |
| sigset_t set; |
| sigemptyset(&set); |
| sigaddset(&set, SIGUSR1); |
| act.sa_mask = set; |
| sigaction(SIGUSR1, &act, NULL); |
| |
| cout << "Client pid is " << getpid() << endl; |
| cout << " Server pid: "; |
| cin >> pnum; |
| |
| if (mkfifo(NAMEDPIPE_NAME, 0777)) { |
| perror("mkfifo"); |
| } |
| |
| if ((fd = open(NAMEDPIPE_NAME, O_RDWR)) <= 0) { |
| perror("Openfile"); |
| return 1; |
| } |
| |
| #endif |
| |
| cout << "Enter your messages here:" << endl; |
| |
| #ifdef _MSC_VER |
| |
| while (true) { |
| Send_message(); |
| SetEvent(hEvent); |
| } |
| |
| CloseHandle(hNamedPipe); |
| |
| #else |
| |
| while (true) { |
| sleep(1); |
| } |
| |
| #endif |
| |
| return 0; |
| } |
| |
| #ifdef _MSC_VER |
| |
| void Send_message() { |
| getline(cin, buf); |
| if (buf == "exit") { |
| SetEvent(hEvent); |
| exit(0); |
| } |
| strcpy_s(buffer, buf.c_str()); |
| if (!WriteFile(hNamedPipe, buffer, strlen(buffer) + 1, &cbWritten, NULL)) { |
| perror("WriteFile"); |
| } |
| } |
| |
| #else |
| |
| void Send_message() { |
| memset(readbuffer, '\0', BUF_SIZE); |
| memset(buffer, '\0', BUF_SIZE); |
| cin.clear(); |
| while (cin.get() != '\n') |
| ; |
| getline(cin, buf); |
| if (buf == "exit") { |
| kill(pnum, SIGUSR2); |
| exit(0); |
| } |
| strcpy(buffer, buf.c_str()); |
| write(fd, buffer, strlen(buffer)); |
| } |
| |
| #endif |