用lwip库与c语言,举一个udp通信的例子,用代码说明
时间: 2024-03-09 18:51:24 浏览: 65
好的,下面是一个基于lwip库和C语言的UDP通信示例代码。
假设有两个设备A和B,它们之间通过UDP协议进行通信。设备A需要向设备B发送一条消息,设备B接收到消息后回复一个确认消息。具体的实现步骤如下:
1. 在设备A上创建UDP套接字,并设置发送缓冲区大小和接收缓冲区大小:
```c
#include "lwip/sockets.h"
#define BUF_SIZE 1024
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
int send_buf_size = BUF_SIZE;
int recv_buf_size = BUF_SIZE;
setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (const char*)&send_buf_size, sizeof(int));
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (const char*)&recv_buf_size, sizeof(int));
```
2. 设置设备B的IP地址和端口号:
```c
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(1234); // 设备B的端口号为1234
addr.sin_addr.s_addr = inet_addr("192.168.1.2"); // 设备B的IP地址为192.168.1.2
```
3. 向设备B发送消息:
```c
char buf[BUF_SIZE] = "Hello, B! This is A.";
sendto(sock, buf, strlen(buf), 0, (struct sockaddr*)&addr, sizeof(addr));
```
4. 在设备B上创建UDP套接字,并设置发送缓冲区大小和接收缓冲区大小:
```c
#include "lwip/sockets.h"
#define BUF_SIZE 1024
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
int send_buf_size = BUF_SIZE;
int recv_buf_size = BUF_SIZE;
setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (const char*)&send_buf_size, sizeof(int));
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (const char*)&recv_buf_size, sizeof(int));
```
5. 绑定设备B的IP地址和端口号:
```c
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(1234); // 设备B监听的端口号为1234
addr.sin_addr.s_addr = htonl(INADDR_ANY); // 任意IP地址
bind(sock, (struct sockaddr*)&addr, sizeof(addr));
```
6. 接收设备A发送的消息,并回复确认消息:
```c
char buf[BUF_SIZE];
struct sockaddr_in from_addr;
socklen_t from_len = sizeof(from_addr);
int len = recvfrom(sock, buf, BUF_SIZE, 0, (struct sockaddr*)&from_addr, &from_len);
if (len > 0) {
printf("Received message from %s:%d: %s\n", inet_ntoa(from_addr.sin_addr), ntohs(from_addr.sin_port), buf);
char reply[BUF_SIZE] = "Received.";
sendto(sock, reply, strlen(reply), 0, (struct sockaddr*)&from_addr, from_len);
}
```
7. 关闭UDP套接字:
```c
close(sock);
```
完整的代码如下:
设备A的代码:
```c
#include <stdio.h>
#include <string.h>
#include "lwip/sockets.h"
#define BUF_SIZE 1024
int main() {
// 创建UDP套接字
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
int send_buf_size = BUF_SIZE;
int recv_buf_size = BUF_SIZE;
setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (const char*)&send_buf_size, sizeof(int));
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (const char*)&recv_buf_size, sizeof(int));
// 设置设备B的IP地址和端口号
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(1234); // 设备B的端口号为1234
addr.sin_addr.s_addr = inet_addr("192.168.1.2"); // 设备B的IP地址为192.168.1.2
// 向设备B发送消息
char buf[BUF_SIZE] = "Hello, B! This is A.";
sendto(sock, buf, strlen(buf), 0, (struct sockaddr*)&addr, sizeof(addr));
// 关闭UDP套接字
close(sock);
return 0;
}
```
设备B的代码:
```c
#include <stdio.h>
#include <string.h>
#include "lwip/sockets.h"
#define BUF_SIZE 1024
int main() {
// 创建UDP套接字
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
int send_buf_size = BUF_SIZE;
int recv_buf_size = BUF_SIZE;
setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (const char*)&send_buf_size, sizeof(int));
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (const char*)&recv_buf_size, sizeof(int));
// 绑定设备B的IP地址和端口号
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(1234); // 设备B监听的端口号为1234
addr.sin_addr.s_addr = htonl(INADDR_ANY); // 任意IP地址
bind(sock, (struct sockaddr*)&addr, sizeof(addr));
// 接收设备A发送的消息,并回复确认消息
char buf[BUF_SIZE];
struct sockaddr_in from_addr;
socklen_t from_len = sizeof(from_addr);
int len = recvfrom(sock, buf, BUF_SIZE, 0, (struct sockaddr*)&from_addr, &from_len);
if (len > 0) {
printf("Received message from %s:%d: %s\n", inet_ntoa(from_addr.sin_addr), ntohs(from_addr.sin_port), buf);
char reply[BUF_SIZE] = "Received.";
sendto(sock, reply, strlen(reply), 0, (struct sockaddr*)&from_addr, from_len);
}
// 关闭UDP套接字
close(sock);
return 0;
}
```
上述代码中,我们使用了lwip库提供的socket和setsockopt函数来创建和设置UDP套接字,并使用sendto和recvfrom函数来发送和接收数据报。需要注意的是,具体的IP地址和端口号需要根据实际情况进行设置。
阅读全文