0
点赞
收藏
分享

微信扫一扫

Linux C/C++ UDP Socket 网络通信

Villagers 2022-02-27 阅读 70

Python微信订餐小程序课程视频

https://edu.csdn.net/course/detail/36074

Python实战量化交易理财系统

https://edu.csdn.net/course/detail/35475
昨晚 Vv 让我给她讲讲网络编程,于是我就傻乎乎的带她入了门…

1. socket() 函数

1.1 头文件

复制代码
  • 1

#include

1.2 函数参数

示例:int socket(int domain, int type, int protocol){...}

  • domain:设置协议域(协议族)

    • AF_INET:IPV4
    • AF_INET6:IPV6
    • ⋯⋯\cdots
  • type:指定 socket 类型

    • SOCKET_STREAM:流式 socket,针对于面向连接的 TCP 服务应用
    • SOCKET_DGRAM:数据报式 socket,针对于无连接的 UDP 服务应用
    • ⋯⋯\cdots
  • protocal:指定协议

    • 000:自动选择第二个参数类型对应的传输协议
    • IPPROTO_TCP:TCP传输协议
    • IPPROTO_UDP:UDP传输协议
    • ⋯⋯\cdots

1.3 返回值

示例:int sock_fd = socket(AF_INET, SOCKET_DGRAM, 0);

  1. sock_fd = -1:套接字创建失败
  2. sock_fd = x(x >= 0):套接字创建成功,返回套接字的文件描述符(索引)

1.4 Socket是什么?

socket在哪里

2. bind()函数

2.1 sockaddr

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5

#include struct sockaddr{ sa\_family\_t sin_family; // 协议族 char sa_data[14]; // 14 个字节,包含套接字中的目标地址和目标端口信息 };

2.2 sockaddr_in

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

`#include // 或 #include
struct in_addr{
In_addr_t s_addr; // 32位 IPv4 地址
};

struct sockaddr_in{
sa_family_t sin_family; // 协议族
uint16_t sin_port; // 16位 TCP/UDP 端口号 (端口号最大是 65535 = 2^16 - 1)
struct in_addr; // 32位 IP 地址
char sin_zero[8]; // 不使用 (为了让sockaddr与sockaddr_in两个数据结构保持大小相同而保留的空字节)
};`

2.3 函数参数

示例:int bind(sock_fd, const struct sockaddr* address, socklen_t address_len);

  • sock_fd:套接字描述符
  • address:sockaddr结构指针,该结构中包含了要绑定的地址和端口号
  • address_len:address缓冲区的长度

2.4 返回值

示例:

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

// 绑定 ip port struct sockaddr\_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(9123); // htons 主机字节序转网络字节序 // 方法1: // INADDR\_ANY 是通配地址,即本机所有 ip 都绑定上。 INADDR\_ANY 转换过来就是0.0.0.0 inet\_pton(AF_INET, INADDR_ANY, &addr.sin_addr.s_addr); // 方法2: // inet\_addr()作用是将一个IP字符串转化为一个网络字节序的整数值,用于sockaddr\_in.sin\_addr.s\_addr。 addr.sin_addr.s_addr = inet\_addr("192.168.0.115"); int res = bind(sock_fd, (struct sockaddr *) &addr, sizeof(addr));

  • res = 0:绑定成功
  • res = -1:绑定失败

2.5 作用

3. sendto() 函数

3.1 函数参数

示例:int sendto(int sock_fd, const void *buf, int len, int flags, const struct sockaddr *address, socklen_t address_len);

  • sock_fd:套接字描述符

  • void *buf:UDP 数据报缓存区(包含待发送数据)

    1. void* 指针可以指向任意类型的数据:
    1. UDP 数据报缓存区:
  • len:UDP数据报的长度

  • flags:调用方式标志位(一般设置为 000,先不掌握)

  • sockaddr *address:sockaddr结构指针,该结构中包含了要发送的地址和端口号

  • address_len:address缓冲区的长度

3.2 返回值

示例:

复制代码
  • 1
  • 2
  • 3

char buf[128] = ""; fgets(buf, sizeof(buf) , stdin); int res = sendto(sock_fd , buf , strlen(buf) , 0, (struct sockaddr *) &server_addr, sizeof(server_addr));

  • res = x:发送成功,xxx 为发送出去的字符数
  • res = -1:发送失败

3.3 作用

把 UDP 数据报发给指定地址。

4. revcfrom() 函数

4.1 函数参数

示例:recvfrom(int socke_fd, const void *buf, int len, int flags, struct sockaddr *address, socklen_t *address_len)

  • sock_fd:套接字描述符

  • void *buf:UDP 数据报缓存区(包含所接收的数据)

    1. UDP 数据报缓存区:
  • len:UDP数据报的长度

  • flags:调用方式标志位(一般设置为 000,先不掌握)

  • sockaddr *address:sockaddr结构指针,该结构中包含了发送方的地址和端口号(可以为 NULL)

  • address_len:socklen_t 指针,指向了 address 结构体的长度(可以为 NULL)

4.2 返回值

示例:

复制代码
  • 1
  • 2

char buf[128] = ""; int recv_len = recvfrom(sock_fd, buf, sizeof(buf), 0, (struct sockaddr*)&client_addr, &client_len);

  • recv_len = x:接收成功,xxx 为接收到的字符数
  • res = -1:接收失败

4.3 作用

接收发送方的网络数据。

5. 服务器代码与客户端代码

Server.cpp

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

`#include
#include
#include
#include
#include
using namespace std;

int main(int argc , char *argv[]){
cout << “Server:\n”;
int sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if(sock_fd < 0) {
perror(“socket 创建失败”);
return 0;
}
cout << “socket 创建成功!\n”;
// 绑定 ip port
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(9123);
// inet_pton(AF_INET, “192.168.0.111”, &addr.sin_addr.s_addr);
addr.sin_addr.s_addr = inet_addr(“192.168.0.115”); //INADDR_ANY 通配地址,即本机所有 ip 都绑定上。 INADDR_ANY 转换过来就是0.0.0.0
int res = bind(sock_fd, (struct sockaddr *) &addr, sizeof(addr));
if(res < 0) {
perror(“绑定失败”);
close(sock_fd);
return 0;
}
cout << “socket 绑定(命名)成功!\n”;
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
while(1){

char buf[128] = “”;
int recv_len = recvfrom(sock_fd, buf, sizeof(buf), 0, (struct sockaddr*)&client_addr, &client_len);
printf(“来自 ip 地址为 %s 端口号为 %d 的信息:%s 信息的总长度为 %d\n” , inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), buf, recv_len);
sendto(sock_fd, buf, recv_len, 0, (struct sockaddr*)&client_addr, sizeof(client_addr));
}
close(sock_fd);
return 0;
}`

Client.cpp

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

`#include
#include
#include
#include
#include
using namespace std;
int main(int argc, char *argv[]){
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(9123); // 服务器端口
inet_pton(AF_INET, “192.168.0.115”, &server_addr.sin_addr.s_addr);

int sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if(sock_fd < 0)
perror("");

while(1){
char buf[128] = “”;
cin.getline(buf , sizeof(buf));
int res = sendto(sock_fd , buf , strlen(buf) , 0, (struct sockaddr *) &server_addr, sizeof(server_addr));
char read_buf[128] = “”;
recvfrom(sock_fd, read_buf, sizeof(read_buf), 0, NULL, NULL);
printf(“共发送 %d 个字符数\n” , res);
}
close(sock_fd);
return 0;
}`

举报

相关推荐

0 条评论