首先,需要明确的是UDP(User Datagram Protocol)是一种连接不稳定、数据包传输的协议。C语言主要通过socket编程实现UDP传输系统。
以下是实现UDP传输系统的基本步骤:
1.初始化socket,并指定协议为UDP:
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
其中,AF_INET表示IPv4地址族,SOCK_DGRAM表示使用UDP协议传输。
2.绑定服务器地址和端口:
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT); // 端口号
inet_aton("127.0.0.1", &(server_addr.sin_addr)); // IP地址
bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
3.接收客户端的数据包:
char buffer[MAX_BUFFER_SIZE];
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
recvfrom(sockfd, buffer, MAX_BUFFER_SIZE, 0, (struct sockaddr *)&client_addr, &addr_len);
其中,MAX_BUFFER_SIZE为接收缓冲区的大小。
4.发送数据到客户端:
sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&client_addr, addr_len);
至此,UDP传输系统的基本框架就已经实现了。下面给出两个示例:
示例一:UDP传输文本消息
服务器端:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
#define MAX_BUFFER_SIZE 1024
int main()
{
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
inet_aton("127.0.0.1", &(server_addr.sin_addr));
bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
char buffer[MAX_BUFFER_SIZE];
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
recvfrom(sockfd, buffer, MAX_BUFFER_SIZE, 0, (struct sockaddr *)&client_addr, &addr_len);
printf("收到客户端消息:%s\n", buffer);
sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&client_addr, addr_len);
return 0;
}
客户端:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
#define MAX_BUFFER_SIZE 1024
int main()
{
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
inet_aton("127.0.0.1", &(server_addr.sin_addr));
char buffer[MAX_BUFFER_SIZE];
printf("请输入你要发送的消息:");
fgets(buffer, MAX_BUFFER_SIZE, stdin);
sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
struct sockaddr_in recv_addr;
socklen_t addr_len = sizeof(recv_addr);
recvfrom(sockfd, buffer, MAX_BUFFER_SIZE, 0, (struct sockaddr *)&recv_addr, &addr_len);
printf("收到服务器消息:%s", buffer);
return 0;
}
以上代码实现了客户端向服务器发送一条文本消息,服务器收到后将消息返回给客户端。
示例二:UDP传输文件
服务器端:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
#define MAX_BUFFER_SIZE 1024
int main()
{
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
inet_aton("127.0.0.1", &(server_addr.sin_addr));
bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
FILE *fp = fopen("test.txt", "wb");
if (!fp)
{
fprintf(stderr, "打开文件失败!\n");
exit(1);
}
char buffer[MAX_BUFFER_SIZE];
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
int len = 0;
while ((len = recvfrom(sockfd, buffer, MAX_BUFFER_SIZE, 0, (struct sockaddr *)&client_addr, &addr_len)) > 0)
{
fwrite(buffer, 1, len, fp);
}
fclose(fp);
return 0;
}
客户端:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
#define MAX_BUFFER_SIZE 1024
int main()
{
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
inet_aton("127.0.0.1", &(server_addr.sin_addr));
FILE *fp = fopen("test.txt", "rb");
if (!fp)
{
fprintf(stderr, "打开文件失败!\n");
exit(1);
}
char buffer[MAX_BUFFER_SIZE];
int len = 0;
while ((len = fread(buffer, 1, MAX_BUFFER_SIZE, fp)) > 0)
{
sendto(sockfd, buffer, len, 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
usleep(100); // 等待0.1秒,避免数据包丢失
}
fclose(fp);
return 0;
}
以上代码实现了客户端将文件“test.txt”发送给服务器,服务器接收到后保存在本地。注意,由于UDP协议的不可靠性,需要在发送数据包后等待一段时间,避免数据包丢失。
以上就是C语言UDP传输系统的基本攻略,希望能对你有帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C语言UDP传输系统源码 - Python技术站