套接字常用的是网络间不同主机间的通信外,但是呢,它也可以实现同一主机的不同进程间的通信,且建立的通信是双向的通信,即本地socket通信。
1.执行flow如下:
2.client端code client.c如下:
1 #include <stdio.h> 2 #include <sys/types.h> 3 #include <sys/socket.h> 4 #include <sys/un.h> 5 6 socklen_t c_len; 7 struct sockaddr_in s_addr; 8 struct socaddr_in c_addr; 9 int c_sockfd; 10 short PORT = 1025; 11 12 typedef struct _DATA_BUF{ 13 int data1; 14 int data2; 15 }DATA_BUF; 16 17 DATA_BUF t_data_buf; 18 19 int socket_init() 20 { 21 /*init clientr*/ 22 c_len = sizeof(struct sockaddr_in); 23 c_addr.sin_family = AF_INET; 24 c_addr. sin_port = htons(PORT); 25 c_addr. sin_addr. s_addr = inet_addr(127.0.0.1); 26 //creat socket 27 c_sockfd=socket(AF_INET,SOCK_DGRAM,0); 28 if(socket_fd<0) 29 { 30 perror("socket fail:%s",strerror(errno)); 31 return -1; 32 } 33 34 /*bind server*/ 35 int ret = bind(c_sockfd,(struct sock addr *)&s_addr,s_len); 36 if(ret) 37 { 38 printf("bind fail :%s ",strerror(errno)); 39 return ret; 40 } 41 return 0; 42 } 43 44 int socket_recv(int sockfd,char*buf,size_t len) 45 { 46 int ret = recvform(sockfd,buf,len,0,(struct sockaddr *)&(c_addr),&c_len); 47 if(ret<0) 48 printf("recv fail :%s ",strerror(errno)); 49 return ret; 50 } 51 52 int socket_send(int sockfd,char*buf,size_t len) 53 { 54 int ret sendto(sockfd,buf,len,0,(struct sockaddr *)&c_addr,c_len); 55 if(ret<0) 56 printf("send fail :%s ",strerror(errno)); 57 return ret; 58 } 59 60 int socket_close(int sockfd) 61 { 62 close(sockfd); 63 } 64 65 int main() 66 { 67 c_sockfd = 0; 68 int len = sizeof(DATA_BUF); 69 char cmd[len]; 70 memset(&t_data_buf,0,len); 71 char send_buf[len]; 72 memset(&cmd,0,len); 73 memset(&send_buf,0,len); 74 system("./server &"); 75 unleep(1000000); 76 do{ 77 sock_init(); 78 }while(c_sockfd<0); 79 80 while(1) { 81 memset(&t_data_buf,0,len); 82 sock_recv(c_sockfd,cmd,len); 83 usleep(1000); 84 memcpy(&t_data_buf,(DATA_BUF *)cmd,len); 85 switch(t_data_buf.data1) 86 case 0: 87 t_data_buf. data1 = 9; 88 memcpy(sendbuffer,&t_data_buf,len); 89 sock_send(c_sockfd,sendbuffer,len); 90 break; 91 default: 92 break; 93 } 94 return 0; 95 }
3.server端 server.c
1 #include <stdio.h> 2 #include <sys/types.h> 3 #include <sys/socket.h> 4 #include <sys/un.h> 5 6 socklen_t s_len; 7 struct sockaddr_in s_addr; 8 struct socaddr_in c_addr; 9 int s_sockfd; 10 short PORT = 1025; 11 12 typedef struct _DATA_BUF{ 13 int data1; 14 int data2; 15 }DATA_BUF; 16 17 DATA_BUF t_data_buf; 18 19 int socket_init() 20 { 21 /*init server*/ 22 s_len = sizeof(struct sockaddr_in); 23 s_addr.sin_family = AF_INET; 24 s_addr. sin_port = htons(PORT); 25 s_addr. sin_addr. s_addr = inet_addr(127.0.0.1); 26 //creat socket 27 s_sockfd=socket(AF_INET,SOCK_DGRAM,0); 28 if(socket_fd<0) { 29 perror("socket fail:%s",strerror(errno)); 30 return -1; 31 } 32 /*bind server*/ 33 int ret = bind(s_sockfd,(struct sock addr *)&s_addr,s_len); 34 35 if(ret) { 36 printf("bind fail :%s ",strerror(errno)); 37 return ret; 38 } 39 return 0; 40 } 41 42 int socket_recv(int sockfd,char*buf,size_t len) 43 { 44 int ret = recvform(sockfd,buf,len,0,(struct sockaddr *)&(c_addr),&s_len); 45 return ret; 46 } 47 48 int socket_send(int sockfd,char*buf,size_t len) 49 { 50 int ret sendto(sockfd,buf,len,0,(struct sockaddr *)&c_addr,s_len); 51 unsleep(1000); 52 if(ret<0>) 53 printf(); 54 return ret; 55 } 56 57 int socket_close(int sockfd) 58 { 59 close(sockfd); 60 } 61 62 int main() 63 { 64 int len = sizeof(DATA_BUF); 65 char cmd[len]; 66 memset(&t_data_buf,0,len); 67 char send_buf[len]; 68 memset(&cmd,0,len); 69 memset(&send_buf,0,len); 70 sock_init(); 71 while(1) { 72 memset(&t_data_buf,0,len); 73 sock_recv(s_sockfd,cmd,len); 74 usleep(1000); 75 memcpy(&t_data_buf,(DATA_BUF *)cmd,len); 76 switch(t_data_buf.data1) 77 case 0: 78 t_data_buf. data1 = 9; 79 memcpy(sendbuffer,&t_data_buf,len); 80 sock_send(s_sockfd,sendbuffer,len); 81 break; 82 default: 83 break; 84 } 85 return 0;