UDP协议介绍
UDP协议分为首部字段和数据字段,其中首部字段只占用8个字节,分别是个占用两个字节的源端口、目的端口、长度和检验和。具体协议字段信息如下:
- 长度:UDP报文的整个大小,最小为8个字节(仅为首部)
- 检验和:在进行检验和计算时,会添加一个伪首部一起进行运算。伪首部(占用12个字节)为:4个字节的源IP地址、4个字节的目的IP地址、1个字节的0、一个字节的数字17、以及占用2个字节UDP长度。这个伪首部不是报文的真正首部,只是引入为了计算校验和。相对于IP协议的只计算首部,UDP检验和会把首部和数据一起进行校验。接收端进行的校验和与UDP报文中的校验和相与,如果无差错应该全为1。如果有误,则将报文丢弃或者发给应用层、并附上差错警告。
UDP实现
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,struct sockaddr *src_addr, socklen_t *addrlen);
- dest_addr : 是接收端的IP地址和端口信息;
- src_addr : 是发送端的IP地址和端口信息;
一个简单的例子:
udp_client.c
#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define BUF_SIZE 1024
int main(int argc, char *argv[])
{
struct addrinfo hint, *result;
int res, sfd;
char buf[BUF_SIZE];
sfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sfd == -1)
{
perror("socket error!\n");
exit(1);
}
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
unsigned short port = 8080;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr("192.168.100.168");
if (connect(sfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) != 0)
{
perror("connect error");
exit(1);
}
snprintf(buf, BUF_SIZE, "hello server, now i will send data to you");
if(send(sfd, buf, strlen(buf), 0) <= 0)
{
perror("send failed.");
exit(1);
}
printf("send data to clint: %s\n", buf);
memset(buf, 0, sizeof(buf));
res = recv(sfd, buf, BUF_SIZE, 0);
printf("recved %d bytes from server.\n", res);
printf("reveive data from server : %s. \n", buf);
return 0;
}
udp_server.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#define BUF_SIZE 1024
int main(int argc, char *argv[])
{
struct addrinfo hint, *result;
struct sockaddr_in client_addr;
int res, sfd, client_addr_len;
char buf[BUF_SIZE];
struct sockaddr_in addr;
memset(&addr, 0, sizeof(struct sockaddr_in));
unsigned short port = 8080;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
sfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sfd == -1)
{
perror("socket error!\n");
exit(1);
}
res = bind(sfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
if (res == -1)
{
perror("bind error!\n");
exit(1);
}
while (1)
{
printf("waiting for client ...\n");
res = recv(sfd, buf, BUF_SIZE, 0);
if (res == -1)
{
perror("recvfrom error\n");
exit(1);
}
printf("revceived data : %s\n", buf);
snprintf(buf, BUF_SIZE, "hello client! Total reveive : %d.", res);
// res = sendto(sfd, buf, strlen(buf), 0, (struct sockaddr*)&client_addr, client_addr_len);
res = send(sfd, buf, strlen(buf), 0);
if (res != strlen(buf) )
{
perror("sendto error\n");
exit(1);
}
printf("send %d bytes to peer", res);
printf("send data < %s > to client\n\n", buf);
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。