本文主要是介绍网络通信1—TCP程序编写步骤(参照 Ubuntu 16.04 版本)(转载),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转载自:https://blog.csdn.net/wenfei11471/article/details/79478105
网络通信1—TCP程序编写步骤(参照 Ubuntu 16.04 版本)
wenfei11471 2018-03-07 22:38:48
965
收藏 4
分类专栏: Linux C 语言 文章标签: TCP Linux 网络编程
版权
Linux C 语言 专栏收录该内容
31 篇文章 0 订阅
订阅专栏
- TCP基础模型
- 服务器流程
- step 1:创建 socke() 套接字接口并判断
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1)
{perror("socket failed");return -1;}
- step 2:设置 IP 和 Port
memset (&seraddr, 0, sizeof(seraddr));
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(8888);
seraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- step 3:绑定 IP 和 Port 并判断
ret = bind(listenfd, (struct sockaddr *)&seraddr, sizeof(seraddr));
if (ret == -1)
{perror("bind failed");return -1;}
- step 4:监听连接并判断
ret = listen(listenfd, 5);
if (ret == -1)
{perror("listen failed");return -1;}
- step 5:接受连接请求并判断
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &addrlen);
if (connfd == -1)
{perror("accept failed");return -1;}
printf("connect sucessfully: client IP: %s, Port: %d\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
- step 6:接收数据并判断
ret = recv(connfd, buf, sizeof(buf), 0);
if (ret == -1)
{perror("recv failed");return -1;}
printf("recv from client: %s\nrecv %d bytes\n", buf, ret);
- step 7:发送数据并判断
ret = send(connfd, buf, sizeof(buf), 0);
if (ret == -1)
{perror("send failed");return -1;}
printf("send successfully!\n");
- step 8:关闭连接
close(connfd);
- step 9:检查头文件和变量是否定义
客户端流程
- step 1:创建 socke() 套接字接口并判断
connfd = socket(AF_INET, SOCK_STREAM, 0);
if (connfd == -1)
{perror("socket failed");return -1;}
- step 2:设置服务器 IP 和 Port
memset (&seraddr, 0, sizeof(seraddr));
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(8888);
seraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- step 3:发送连接请求并判断
ret = connect(connfd, (struct sockaddr *)&seraddr, sizeof(seraddr));
if (ret == -1)
{perror("connect faile");return -1;}
printf("connect sucessfully!\n");
- step 4:发送数据并判断
ret = send(connfd, buf, sizeof(buf), 0);
if (ret == -1)
{perror("send faile");return -1;}
printf("send successfully!");
- step 5:接收数据
ret = recv(connfd, buf, sizeof(buf), 0);
if (ret == -1)
{perror("recv faile");return -1;}
printf("%s\n", buf);
- step 6:关闭连接
close(connfd);
- step 7:检查头文件和变量是否定义
- TCP 循环服务器模型
- 服务器流程
- step 1:创建 socke() 套接字接口并判断
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1)
{perror("socket failed");return -1;}
- step 2:设置 IP 和 Port
memset (&seraddr, 0, sizeof(seraddr));
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(8888);
seraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- step 3:绑定 IP 和 Port 并判断
ret = bind(listenfd, (struct sockaddr *)&seraddr, sizeof(seraddr));
if (ret == -1)
{perror("bind failed");return -1;}
- step 4:监听连接并判断
ret = listen(listenfd, 5);
if (ret == -1)
{perror("listen failed");return -1;}
- step 5:设置循环连接
while(1)
- step 6:接受连接请求并判断
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &addrlen);
if (connfd == -1)
{perror("accept failed");return -1;}
printf("connect sucessfully: client IP: %s, Port: %d\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
- step 7:循环接收数据,直达条件满足退出
while(1)
- step 8:接收数据并判断
ret = recv(connfd, buf, sizeof(buf), 0);
if (ret == -1)
{perror("recv failed");return -1;}
- step 9:设置跳出循环的条件
if (!strncmp("quit", buf, 4))
break;
printf("recv from client: %s\nrecv %d bytes\n", buf, ret);
- step 10:发送数据并判断
ret = send(connfd, buf, sizeof(buf), 0);
if (ret == -1)
{perror("send failed");return -1;}
printf("send successfully!\n");
- step 11:关闭连接
close(connfd);
- step 12:检查头文件和变量是否定义
客户端流程
- step 1:创建 socke() 套接字接口并判断
connfd = socket(AF_INET, SOCK_STREAM, 0);
if (connfd == -1)
{perror("socket failed");return -1;}
- step 2:设置服务器 IP 和 Port
memset (&seraddr, 0, sizeof(seraddr));
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(8888);
seraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- step 3:发送连接请求并判断
ret = connect(connfd, (struct sockaddr *)&seraddr, sizeof(seraddr));
if (ret == -1)
{perror("connect faile");return -1;}
printf("connect sucessfully!\n");
- step 4:设置数据循环,并获取标准输入的数据
while (1)
{fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0';
...}
- step 5:发送数据并判断
ret = send(connfd, buf, sizeof(buf), 0);
if (ret == -1)
{perror("send faile");return -1;}
printf("send successfully!");
- step 6:设置退出条件
if (!strncmp("quit", buf, 4))
break;
- step 7:接收数据
ret = recv(connfd, buf, sizeof(buf), 0);
if (ret == -1)
{perror("recv faile");return -1;}
printf("%s\n", buf);
- step 8:关闭连接
close(connfd);
- step 9:检查头文件和变量是否定义
- TCP 并发服务器模型
- 服务器流程
- step 1:创建 socke() 套接字接口并判断
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1)
{perror("socket failed");return -1;}
- step 2:设置 IP 和 Port
memset (&seraddr, 0, sizeof(seraddr));
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(8888);
seraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- step 3:绑定 IP 和 Port 并判断
ret = bind(listenfd, (struct sockaddr *)&seraddr, sizeof(seraddr));
if (ret == -1)
{perror("bind failed");return -1;}
- step 4:监听连接并判断
ret = listen(listenfd, 5);
if (ret == -1)
{perror("listen failed");return -1;}
- step 5:设置信号处理防止僵尸进程
signal(SIGCHLD, SIG_IGN);
- step 6:设置循环建立连接
while(1)
- step 7:接受连接请求并判断
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &addrlen);
if (connfd == -1)
{perror("accept failed");return -1;}
printf("connect sucessfully: client IP: %s, Port: %d\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
- step 8:创建子进程并判断
pid = fork();
if (pid == -1)
{perror("fork create");return -1}
- step 9:设置父进程关闭连接并重新建立链接
if (pid > 0)
{close(connfd);continue;}
- step 10:子进程关闭监听,并循环接收数据
close(listenfd);
while(1)
- step 11:接收数据并判断
ret = recv(connfd, buf, sizeof(buf), 0);
if (ret == -1)
{perror("recv failed");return -1;}
- step 12:设置跳出循环的条件
if (!strncmp("quit", buf, 4))
break;
printf("recv from client: %s\nrecv %d bytes\n", buf, ret);
- step 10:发送数据并判断
ret = send(connfd, buf, sizeof(buf), 0);
if (ret == -1)
{perror("send failed");return -1;}
printf("send successfully!\n");
- step 11:关闭连接同时退出进程
close(connfd);
exit(0);
- step 12:检查头文件和变量是否定义
客户端流程
- step 1:创建 socke() 套接字接口并判断
connfd = socket(AF_INET, SOCK_STREAM, 0);
if (connfd == -1)
{perror("socket failed");return -1;}
- step 2:设置服务器 IP 和 Port
memset (&seraddr, 0, sizeof(seraddr));
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(8888);
seraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- step 3:发送连接请求并判断
ret = connect(connfd, (struct sockaddr *)&seraddr, sizeof(seraddr));
if (ret == -1)
{perror("connect faile");return -1;}
printf("connect sucessfully!\n");
- step 4:设置数据循环,并获取标准输入的数据
while (1)
{fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0';
...}
- step 5:发送数据并判断
ret = send(connfd, buf, sizeof(buf), 0);
if (ret == -1)
{perror("send faile");return -1;}
printf("send successfully!");
- step 6:设置退出条件
if (!strncmp("quit", buf, 4))
break;
- step 7:接收数据
ret = recv(connfd, buf, sizeof(buf), 0);
if (ret == -1)
{perror("recv faile");return -1;}
printf("%s\n", buf);
- step 8:关闭连接
close(connfd);
- step 9:检查头文件和变量是否定义
这篇关于网络通信1—TCP程序编写步骤(参照 Ubuntu 16.04 版本)(转载)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!