socket 在Unix domain的使用
server.c
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <unistd.h> #include <sys/un.h> #include <pthread.h> #include <sys/syscall.h> #define UNIX_DOMAIN "/tmp/test" void *threaddeal(void *arg) { printf("this is a new thread\n"); int com_fd = *(int*)arg; char recv_buf[1024]; memset(recv_buf,0,1024); char snd_buf[1024]; memset(snd_buf,0,1024); strcpy(snd_buf,"server to client"); int num; while(num=read(com_fd,recv_buf,sizeof(recv_buf))) { printf("server recv (%d)) :%s, threadid = %u,lwpid = %u\n",num,recv_buf,pthread_self(),syscall(SYS_gettid)); memset(recv_buf,0,1024); write(com_fd,snd_buf,sizeof(snd_buf));//回复client } close(com_fd); } int main(void) { socklen_t clt_addr_len; int listen_fd; int com_fd; int ret; struct sockaddr_un clt_addr; struct sockaddr_un srv_addr; listen_fd=socket(PF_UNIX,SOCK_STREAM,0); if(listen_fd<0) { perror("cannot create communication socket"); return 1; } //set server addr_param srv_addr.sun_family=AF_UNIX; strncpy(srv_addr.sun_path,UNIX_DOMAIN,sizeof(srv_addr.sun_path)-1); unlink(UNIX_DOMAIN); //bind sockfd & addr ret=bind(listen_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr)); if(ret==-1) { perror("cannot bind server socket"); close(listen_fd); unlink(UNIX_DOMAIN); return 1; } //listen sockfd ret=listen(listen_fd,1); if(ret==-1) { perror("cannot listen the client connect request"); close(listen_fd); unlink(UNIX_DOMAIN); return 1; } //have connect request use accept clt_addr_len=sizeof(clt_addr); while(1) { com_fd=accept(listen_fd,(struct sockaddr*)&clt_addr,&clt_addr_len); if(com_fd<0) { perror("cannot accept client connect request"); close(listen_fd); unlink(UNIX_DOMAIN); return 1; } //开一个新线程处理新连接com_fd pthread_t threadid; //线程的标识符 if(pthread_create(&threadid,NULL,threaddeal,(void*)(&com_fd)) != 0) { perror("cannot creat new thread"); } } close(listen_fd); unlink(UNIX_DOMAIN); return 0; }
client.c
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> #include <unistd.h> #include <pthread.h> #define UNIX_DOMAIN "/tmp/test" void *threaddeal(void *arg) { printf("this is a new thread\n"); int com_fd = *(int*)arg; char recv_buf[1024]; memset(recv_buf,0,1024); char snd_buf[1024]; memset(snd_buf,0,1024); int num; while(num=read(com_fd,recv_buf,sizeof(recv_buf))) { printf("client recv : (%d)) :%s\n",num,recv_buf); memset(recv_buf,0,1024); } close(com_fd); } int main(void) { int connect_fd; int ret; char snd_buf[1024]; struct sockaddr_un srv_addr; //creat unix socket connect_fd=socket(PF_UNIX,SOCK_STREAM,0); if(connect_fd<0) { perror("cannot create communication socket"); return 1; } srv_addr.sun_family=AF_UNIX; strcpy(srv_addr.sun_path,UNIX_DOMAIN); //connect server ret=connect(connect_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr)); if(ret==-1) { perror("cannot connect to the server"); close(connect_fd); return 1; } memset(snd_buf,0,1024); strcpy(snd_buf,"client send to server11111111111");
//开新线程处理server返回 pthread_t threadid; //线程的标识符 if(pthread_create(&threadid,NULL,threaddeal,(void*)(&connect_fd)) != 0) { perror("cannot creat new thread"); } //send msg to server while(1){ write(connect_fd,snd_buf,sizeof(snd_buf)); sleep(2); } close(connect_fd); return 0; }