代码: 全选
//SERVER
#include<fcntl.h>
#include <unistd.h>
#include <stdio.h>
//int clientpipe;
int main(){
int serverpipe = open("/run/server", O_RDONLY);
size_t n;
typedef struct {
enum {CONNECT, DISCONNECT} cmd;
pid_t pid;
} msg_t;
msg_t msg;
while(n=read(serverpipe, &msg, sizeof(msg))){
if(msg.cmd == CONNECT){
printf("client%d connect\n", msg.pid);
//clientpipe = open("/run/client", O_WRONLY);
}
else{
printf("client disconnect\n");
//close(clientpipe);
break;
}
}
close(serverpipe);
return 0;
}
代码: 全选
//CLIENT
#include <fcntl.h>
#include <unistd.h>
int main(){
int serverpipe = open("/run/server", O_WRONLY);
typedef struct {
enum {CONNECT, DISCONNECT} cmd;
pid_t pid;
} msg_t;
msg_t msg;
msg = (msg_t){CONNECT, 0};
msg.pid = getpid();
write(serverpipe, &msg, sizeof(msg));
msg = (msg_t){DISCONNECT, 0};
write(serverpipe, &msg, sizeof(msg));
close(serverpipe);
return 0;
}