Unix的通信方式包括: socket(套接字),pipe(管道), 消息队列, 共享内存, 信号。
- 信号:
#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <signal.h> void fc() { printf("ctrl+c is blocked\n"); (void)signal(SIGINT, SIG_DFL); } int main(){ int i; sigset_t set, pendset; struct sigaction action; (void) signal(SIGINT, fc); if(sigemptyset(&set) < 0) perror("init error\n"); if( sigprocmask(SIG_BLOCK, &set, NULL) < 0) perror("block a new signal error\n"); else{ for(i=0;i<5;++i) { printf("showing this text is because this process is blocked\n"); sleep(1); } } if(sigprocmask(SIG_UNBLOCK, &set, NULL) < 0) perror("del a blocked signal error\n"); }
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> void fc() { printf("you pressed ctrl + c \n"); printf("default detach.\n"); (void) signal(SIGINT, SIG_DFL); } int main(){ (void) signal(SIGINT, fc); printf("main: process enter a loop.\n"); while(1){ sleep(1); printf("looping\n"); } exit(0); }
- socket:server.c代码
/* File Name: server.c */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define DEFAULT_PORT 8000
#define MAXLINE 4096
int main(int argc, char** argv)
{
int socket_fd, connect_fd;
struct sockaddr_in servaddr;
char buff[4096];
int n;
//初始化Socket
if( (socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
//初始化
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);//IP地址设置成INADDR_ANY,让系统自动获取本机的IP地址。
servaddr.sin_port = htons(DEFAULT_PORT);//设置的端口为DEFAULT_PORT
//将本地地址绑定到所创建的套接字上
if( bind(socket_fd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){
printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
//开始监听是否有客户端连接
if( listen(socket_fd, 10) == -1){
printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
printf("======waiting for client's request======\n");
while(1){
//阻塞直到有客户端连接,不然多浪费CPU资源。
if( (connect_fd = accept(socket_fd, (struct sockaddr*)NULL, NULL)) == -1){
printf("accept socket error: %s(errno: %d)",strerror(errno),errno);
continue;
}
//接受客户端传过来的数据
n = recv(connect_fd, buff, MAXLINE, 0);
//向客户端发送回应数据
if(!fork()){ /*紫禁城*/
if(send(connect_fd, "Hello,you are connected!\n", 26,0) == -1)
perror("send error");
close(connect_fd);
exit(0);
}
buff[n] = '\0';
printf("recv msg from client: %s\n", buff);
close(connect_fd);
}
close(socket_fd);
}
client.c
/* File Name: client.c */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define MAXLINE 4096
int main(int argc, char** argv)
{
int sockfd, n,rec_len;
char recvline[4096], sendline[4096];
char buf[MAXLINE];
struct sockaddr_in servaddr;
if( argc != 2){
printf("usage: ./client <ipaddress>\n");
exit(0);
}
if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(8000);
if( inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){
printf("inet_pton error for %s\n",argv[1]);
exit(0);
}
if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){
printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
printf("send msg to server: \n");
fgets(sendline, 4096, stdin);
if( send(sockfd, sendline, strlen(sendline), 0) < 0)
{
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
if((rec_len = recv(sockfd, buf, MAXLINE,0)) == -1) {
perror("recv error");
exit(1);
}
buf[rec_len] = '\0';
printf("Received : %s ",buf);
close(sockfd);
exit(0);
}
- pipe:无名pipe
#include <string.h> #include<unistd.h> #include<wait.h> #include<stdlib.h> #include<stdio.h> int main() { char buff[100]; memset(buff, 0, sizeof(buff)); int pipeFd[2]; if(pipe(pipeFd) < 0) { printf("create pipe error\n"); return -1; } pid_t forkResult; if((forkResult =fork()) < 0) { printf("create child process error\n"); exit(0); } else if(forkResult ==0) { close(pipeFd[1]); int readc; scanf("%s",buff); if((readc=read(pipeFd[0], buff, 100)) > 0) printf("child proc read %s from piipe\n", buff); close(pipeFd[0]); exit(0); } else{ close(pipeFd[0]); printf("input something:\n"); char buffW[100]; if(write(pipeFd[1], buffW, strlen(buffW)) != -1) printf("child proc write %s to pipe\n", buffW); close(pipeFd[1]); waitpid(forkResult, NULL, 0); exit(0); } }
有名pipe
#include <stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<sys/select.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
int main()
{
int i,rfd,wfd,len=0,fd_in;
struct timeval net_timer;
mkfifo("fifol", S_IWUSR|S_IIRUSR|S_IRGRP|S_IROTH);
mkfifo("fifor", S_IWUSR|S_IIRUSR|S_IRGRP|S_IROTH);
fd_set wfd, rfd;
wfd=open("fifol", O_WRONLY);
rfd=open("fifor",O_RDONLY);
if(rfd <= 0 || wfd <= 0) return 0;
printf("this is a's side\n");
char str[32];
fd_set write_fd, read_fd;
while(1)
{
FD_ZERO(&read_fd);
FD_SET(rfd, &read_fd);
FD_SET(fileno(stdin), &read_fd);
net_timer.tv_sec = 5;
net_timer.tv.usec = 0;
memset(str, 0, sizeof(str));
if(i=select(rfd+1, &read_fd, NULL, &net_timer) <= 0)
continue;
if(FD_ISSET(rfd, &read_fd)){
read(read_fd, str, sizeof(str));
printf("--------------------------------\n");
printf("b:%s\n",str);
}
if(FD_ISSET(fileno(stdin), &read_fd)){
printf("------------------------00\n");
fgets(str, sizeof(str), sstdin);
len = write(wfd, str, strlen(str));
}
}
close(rfd);
close(wfd);
}
popen
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
int main(){
char buf[5000];
memset(buf, 0, sizeof(buf));
FILE *fp;
fp = popen("ls -l", "r");
int num;
if(fp != NULL){
num =fread(buf, sizeof(char), 5000, fp);
if(num > 0){
printf("tocken: \"ls -l\", result: \n");
printf("%s \n", buf);
}
pclose(fp);
}
else{
printf("popen error\n");
return 1;
}
fp = popen("grep 1-9.c", "w");
printf("second tocken is \"grep 7-8.c\" result is: \n");
fprintf(fp, "%s\n", buf);
pclose(fp);
return 0;
}