-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef CLIENT_H | ||
#define CLIENT_H | ||
|
||
void client(); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef MSGTYPE_H | ||
#define MSGTYPE_H | ||
|
||
#define MTEXTSIZE 128 | ||
#define KEYID 2817 | ||
|
||
struct msgbuf { | ||
long mtype; | ||
char mtext[MTEXTSIZE]; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef SERVER_H | ||
#define SERVER_H | ||
|
||
void server(); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include <sys/types.h> | ||
#include <sys/ipc.h> | ||
#include <sys/msg.h> | ||
#include <sys/stat.h> | ||
|
||
#include "msg.h" | ||
|
||
void client() { | ||
key_t key_id; | ||
struct msgbuf msg; | ||
char *text; | ||
|
||
key_id = msgget(KEYID, IPC_CREAT|0666); | ||
|
||
if (key_id < 0) { | ||
perror("msgget error "); | ||
exit(0); | ||
} | ||
|
||
printf("Start client\n"); | ||
while (1) { | ||
printf("input > "); | ||
scanf("%s", text); | ||
msg.mtype = 1; | ||
strcpy(msg.mtext, text); | ||
|
||
if (msgsnd(key_id, &msg, sizeof(msg.mtext), 0) < 0) { | ||
perror("msgsnd error "); | ||
exit(0); | ||
} | ||
printf("client: send %s\n", msg.mtext); | ||
sleep(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <signal.h> | ||
|
||
#include <sys/types.h> | ||
#include <sys/ipc.h> | ||
#include <sys/msg.h> | ||
|
||
#include "server.h" | ||
#include "client.h" | ||
|
||
#include "msg.h" | ||
|
||
void int_handler(int signo) { | ||
key_t key_id; | ||
|
||
key_id = msgget(KEYID, IPC_CREAT|0666); | ||
|
||
if (key_id < 0) { | ||
perror("msgget error "); | ||
exit(0); | ||
} | ||
|
||
msgctl(key_id, IPC_RMID, 0); | ||
} | ||
|
||
int main() { | ||
signal(SIGINT, int_handler); | ||
|
||
if (fork() == 0) { | ||
client(); | ||
} else { | ||
server(); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include <sys/types.h> | ||
#include <sys/ipc.h> | ||
#include <sys/msg.h> | ||
#include <sys/stat.h> | ||
|
||
#include "msg.h" | ||
|
||
void server() { | ||
key_t key_id; | ||
struct msgbuf msg; | ||
|
||
key_id = msgget(KEYID, IPC_CREAT|0666); | ||
|
||
if (key_id < 0) { | ||
perror("msgget error "); | ||
exit(0); | ||
} | ||
|
||
printf("Start server\n"); | ||
|
||
while (1) { | ||
if (msgrcv(key_id, &msg, sizeof(msg.mtext), 0, 0) < 0) { | ||
perror("msgrcv error "); | ||
exit(0); | ||
} | ||
printf("server: recevied %s\n", msg.mtext); | ||
} | ||
} |