Skip to content

Commit

Permalink
Set server & client
Browse files Browse the repository at this point in the history
  • Loading branch information
uoo723 committed Jun 20, 2017
1 parent dfd87d8 commit eda860b
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef CLIENT_H
#define CLIENT_H

void client();

#endif
12 changes: 12 additions & 0 deletions include/msg.h
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
6 changes: 6 additions & 0 deletions include/server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef SERVER_H
#define SERVER_H

void server();

#endif
39 changes: 39 additions & 0 deletions src/client.c
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);
}
}
37 changes: 37 additions & 0 deletions src/main.c
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;
}
31 changes: 31 additions & 0 deletions src/server.c
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);
}
}

0 comments on commit eda860b

Please sign in to comment.