千家信息网

基于TCP协议的进程间通信

发表于:2024-09-25 作者:千家信息网编辑
千家信息网最后更新 2024年09月25日,TCP协议是应用在传输层的协议。是一种面向连接的、可靠的协议。TCP协议的特点:1)面向字节流。2)TCP是面向连接的运输层协议3) 每一条TCP链接只能有两个端点4)TCP提供可靠交付的服务5)TC
千家信息网最后更新 2024年09月25日基于TCP协议的进程间通信

TCP协议是应用在传输层的协议。是一种面向连接的、可靠的协议。

TCP协议的特点:

1)面向字节流。

2)TCP是面向连接的运输层协议

3) 每一条TCP链接只能有两个端点

4)TCP提供可靠交付的服务

5)TCP提供全双工通信

根据TCP协议三次握手,server一直处于监听状态,等接受到client的请求连接(connect)信号,accept该连接。


  server:   1 #include  2 #include  3 #include  4 #include  5 #include  6 #include  7 #include  8 #include  9 #define _BACKLOG 5 10 void usage(char *_proc) 11 { 12     printf("usage: %s,[ip],[proc]",_proc); 13 } 14 int startup(const char *ip,const int port) 15 { 16     int sock = socket(AF_INET,SOCK_STREAM,0); //创建套接字 17     if(sock < 0) 18     { 19         perror("socket"); 20         exit(1); 21     } 22     struct sockaddr_in local; 23     local.sin_family = AF_INET; 24     local.sin_port = htons(port); 25     local.sin_addr.s_addr = inet_addr(ip); 26     if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0) //将IP和端口号绑定 27     { 28         perror("bind"); 29         exit(2); 30     } 31     if(listen(sock,_BACKLOG)<0) //监听是否有进程与之创建连接 32     { 33         perror("listen"); 34         exit(3); 35     } 36     return sock; 37 } 38 void* pthread(void * arg) 39 { 40     int sock = (int)arg; 41     char buf[1024]; 42     while(1) 43     { 44         ssize_t size = read(sock,buf,sizeof(buf)-1); 45         if(size>0) 46         { 47             buf[size] = '\0'; 48         } 49         else if(size==0) 50         { 51             printf("client close...\n"); 52             break; 53         } 54         else 55         { 56             perror("read"); 57             exit(3); 58         } 59         printf("client say: %s\n",buf); 60     } 61     close(sock); 62     return NULL; 63 } 64  65 int main(int argc,char *argv[]) 66 { 67     if(argc != 3) 68     {  69         usage(argv[0]); 70         exit(1); 71     } 72     char *ip = argv[1]; 73     int port = atoi(argv[2]); 74     int listen_sock = startup(ip,port); 75     struct sockaddr_in client; 76     socklen_t len = sizeof(client); 77     while(1) 78     { 79         int new_sock = accept(listen_sock,(struct sockaddr*)&client,&len); 80         if(new_sock<0) 81         { 82             perror("accept"); 83             continue; 84         } 85         printf("get a client... sock %d,ip: %s,port: %d\n",new_sock,inet_nto    a(client.sin_addr),ntohs(client.sin_port)); 86 #ifdef _V1_ //单进程通信 87         char buf[1024]; 88         while(1) 89         { 90             ssize_t size = read(new_sock,buf,sizeof(buf)-1); 91             if(size>0) 92             { 93                 buf[size] = '\0'; 94             } 95             else if(size==0) 96             { 97                 printf("client close...\n"); 98                 break; 99             }100             else101             {102                 perror("read");103                 exit(3);104             }105             printf("client say :%s\n",buf);106         }107 #elif _V2_ //多进程108         pid_t pid = fork();109         if(pid<0)110         {                              111             perror("fork");112             exit(4);113         }114         else if(pid == 0)115         {//child116             close(listen_sock);117             char *_client = inet_ntoa(client.sin_addr);118             char buf[1024];119             while(1)120             {121                 memset(buf,'\0',sizeof(buf));122                 ssize_t size = read(new_sock,buf,sizeof(buf)-1);123                 if(size>0)124                 {125                     buf[size] = '\0';126                 }127                 else if(size==0)128                 {129                     printf("[ip]:%s close...\n",_client);130                     break;131                 }132                 else133                 {134                     perror("read");135                     exit(3);136                 }137                 printf("[ip]:%s say :%s\n",_client,buf);138             }139             close(new_sock);140             exit(0);141         }142         else143         {//father144             close(new_sock);145         }146 #elif _V3_ //多线程147         pthread_t pid;148         pthread_create(&pid,NULL,pthread,(void*)new_sock);149         pthread_detach(pid);150 #else151         printf("default");152 #endif153     }154     return 0;155 }client:   1 #include  2 #include  3 #include  4 #include  5 #include  6 #include  7 #include  8   9 void usage(char *proc) 10 { 11     printf("usage:%s,[remote ip],[remote proc]",proc); 12 } 13 int main(int argc,char *argv[]) 14 { 15     if(argc != 3) 16     { 17         usage(argv[0]); 18         exit(1); 19     } 20     int sock = socket(AF_INET,SOCK_STREAM,0); 21     if(sock<0) 22     { 23         perror("sock"); 24         exit(1); 25     } 26     int port = atoi(argv[2]); 27     char *ip = argv[1]; 28     struct sockaddr_in remote; 29     remote.sin_family = AF_INET; 30     remote.sin_port = htons(port); 31     remote.sin_addr.s_addr = inet_addr(ip); 32  33     int ret = connect(sock,(struct sockaddr*)&remote,sizeof(remote)); 34     if(ret<0) 35     { 36         perror("connect"); 37         exit(2); 38     } 39     char buf[1024]; 40     while(1) 41     { 42         printf("please say: "); 43         scanf("%s",buf); 44         ssize_t size = write(sock,buf,sizeof(buf)-1); 45  46     } 47     return 0; 48 }  结果:[fbl@localhost tcp]$ ./tcp_server 192.168.1.106 8080get a client... sock 4,ip: 192.168.1.106,port: 51708client say: helloclient say: hiclient say: nihaoclient close...^C[fbl@localhost tcp]$ [fbl@localhost tcp]$ ./tcp_client 192.168.1.106 8080please say: helloplease say: hiplease say: nihao please say: ^C[fbl@localhost tcp]$





进程 通信 两个 信号 口号 套接字 特点 状态 端点 线程 结果 链接 双工 传输 应用 服务 监听 运输 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 与服务器进行同步时间出错 太原元工互联网科技有限公司 山西调度服务器生产 加强对网络安全工作的认识 怎么打开金蝶的数据库 数据库中关系代数的连接 受人称赞的应用软件开发 九合网络技术有限公司官网 违反网络安全法规定给他人 网络安全周 教育局负责 电脑服务器开机之后要按什么键 服务器打印机能用多久 服务器监视软件 已创建数据库从哪里找 网络安全专业最好的大学中国 服务器关联安全组 铜梁区网络软件开发服务标志 GP数据库变长数据类型 网络安全专业需要考什么证 微信公众号查询数据库怎么做 成都新网智能网络技术 华为服务器 开机黑屏 系统和网络安全保障服务能开票嘛 南京白鸥网络技术 软件开发转成本分录怎么做 口碑好的网络技术五星服务 在合肥学软件开发最好学校 网络技术软件技术的发展趋势 数据库的变量类型 sas服务器版安装包
0