当前位置:网站首页>作业8.9 构建TCP协议的服务器
作业8.9 构建TCP协议的服务器
2022-08-10 12:40:00 【不知名大学生M】
构建TCP协议的服务器
实现代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
//打印错误新的宏函数
#define ERR_MSG(msg) do{
\ fprintf(stderr, " __%d__ ", __LINE__);\ perror(msg);\ }while(0)
#define PORT 8888 //1024~49151
#define IP "192.168.31.96" //本机IP,用ifconfig查看
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("create socket success\n");
//填充地址信息结构体,真实的地址信息结构体与协议族相关
//AF_INET,所以详情请看man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT); //网络字节序的端口号
sin.sin_addr.s_addr = inet_addr(IP); //网络字节序的IP地址
//将地址信息结构体绑定到套接字上
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//将套接字设置为被动监听状态,让内核去监听是否有客户端连接;
if(listen(sfd, 10) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
//从已完成连接的队列头中,取出一个客户端的信息,创建生成一个新的套接字文件描述符,
//该文件描述符才是与客户端通信的文件描述符!!!
int newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
if(newfd < 0)
{
perror("accept");
return -1;
}
//网络字节序的IP-->点分十进制 网络字节序的port--->本机字节序
printf("[%s : %d] newfd = %d\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port),newfd);
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf, sizeof(buf));
//循环接收
res = recv(newfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("[%s : %d] newfd = %d客户端退出\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port),newfd);
break;
}
printf("[%s : %d] newfd = %d : %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port),newfd, buf);
}
close(sfd);
close(newfd);
return 0;
}
运行结果
边栏推荐
- 2022 Recruitment Notice for Academician Zhao Guoping Group of Shenzhen Institute of Advanced Technology, Chinese Academy of Sciences
- 用低代码驱动IT现代化
- 娄底干细胞制备实验室建设须知要求
- The recursive recursive Fighting_ silver study ah but level 4
- 鸿蒙开发从hello world开始
- 【百度统计】用户行为分析
- 11+ chrome高级调试技巧,学会效率直接提升666%
- Keithley DMM7510精准测量超低功耗设备各种运作模式功耗
- C#报错 The ‘xmins‘ attribute is not supported in this context
- Jenkins修改端口号, jenkins容器修改默认端口号
猜你喜欢
【黑马早报】雷军称低谷期曾想转行开酒吧;拜登正式签署芯片法案;软银二季度巨亏230亿美元;北京市消协约谈每日优鲜...
MySQL面试题——MySQL常见查询
把相亲角搬到海外,不愧是咱爸妈
【ECCV 2022|百万奖金】PSG大赛:追求“最全面”的场景理解
【百度统计】用户行为分析
3DS MAX 批量导出文件脚本 MAXScript 带界面
Nanodlp v2.2/v3.0 light curing circuit board, connection method of mechanical switch/photoelectric switch/proximity switch and system state level setting
Proprietary cloud ABC Stack, the real strength!
Merge similar items in LeetCode simple questions
Detailed explanation of es6-promise object
随机推荐
Efficient and Robust 2D-to-BEV Representation Learning via Geometry-guided Kernel Transformer 论文笔记
Fragment-hide and show
鸿蒙开发从hello world开始
Merge similar items in LeetCode simple questions
11+ chrome高级调试技巧,学会效率直接提升666%
娄底石油化工实验设计、建设规划概述
递归递推之Fighting_小银考呀考不过四级
浙大、阿里提出DictBERT,字典描述知识增强的预训练语言模型
How to describe multiple paragraphs with different font settings in Open Office XML format
CodeForces - 834C
跨域的五种解决方案
M²BEV: Multi-Camera Joint 3D Detection and Segmentation with Unified Bird’s-Eye View Representation
shell:常用小工具(sort、uniq、tr、cut)
LeetCode简单题之合并相似的物品
[Advanced Digital IC Verification] Difference and focus analysis between SoC system verification and IP module verification
Fragment的show和hide
Fragment's show and hide
Open Office XML 格式里如何描述多段具有不同字体设置的段落
LeetCode·每日一题·640.求解方程·模拟构造
Network Saboteur