ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);
功能:从套接字中接受数据
参数: sockfd:套接字文件描述符
buf:存放数据空间首地址
flags:属性 默认为0
src_addr:存放IP地址信息的空间首地址
addrlen:存放接收到IP地址大小空间的首地址
返回值:成功返回实际接收到的字节数;失败返回-1;
int bind(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
功能:在套接字上绑定一个IP地址和端口号
参数: sockfd:套接字文件描述符
addr:绑定IP地址空间首地址
addrlen:绑定IP地址的长度
返回值:成功返回0 ;失败返回-1
send.c(当自己的主机为发送端)
- #include "head.h"
-
- int main(void)
- {
- int sockfd = 0;
- char tmpbuff[1024] = {0};
- struct sockaddr_in sendaddr;
- ssize_t nsize = 0;
- int ret = 0;
- struct sockaddr_in myaddr;
-
- fgets(tmpbuff, sizeof(tmpbuff), stdin);
-
- sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- if (sockfd == -1)
- {
- perror("fail to socket");
- return -1;
- }
-
- myaddr.sin_family = AF_INET;
- myaddr.sin_port = htons(33333);
- myaddr.sin_addr.s_addr = inet_addr("192.168.1.167");
- ret = bind(sockfd, (struct sockaddr *)&myaddr, sizeof(myaddr)); //将发送端的端口和IP地址绑定住(自己这台主机位发送端)
- if (ret == -1)
- {
- perror("fail to bind");
- return -1;
- }
-
- sendaddr.sin_family = AF_INET;
- sendaddr.sin_port = htons(30000); //目的主机端口
- sendaddr.sin_addr.s_addr = inet_addr("192.168.1.176"); //目的主机的IP地址
-
- nsize = sendto(sockfd, tmpbuff, strlen(tmpbuff), 0, (struct sockaddr *)&sendaddr, sizeof(sendaddr));
- if(nsize == -1)
- {
- perror("fail to sendto");
- return -1;
- }
-
- printf("发送了%ld字节\n",nsize);
-
- close(sockfd);
-
- return 0;
- }
recv.c(自己这台主机为接收端)
- #include "head.h"
-
- int main(void)
- {
- int ret = 0;
- int sockfd = 0;
- char tmpbuff[1024] = {0};
- struct sockaddr_in recvaddr;
- ssize_t nsize = 0;
- struct sockaddr_in sendaddr;
- socklen_t len = sizeof(sendaddr);
-
- sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- if (sockfd == -1)
- {
- perror("fail to socket");
- return -1;
- }
-
- recvaddr.sin_family = AF_INET;
- recvaddr.sin_port = htons(33333);
- recvaddr.sin_addr.s_addr = inet_addr("192.168.1.167");
-
- ret = bind(sockfd, (struct sockaddr *)&recvaddr, sizeof(recvaddr)); //需要绑定自己的IP和端口
- if (ret == -1)
- {
- perror("fail to bind");
- return -1;
- }
-
- nsize = recvfrom(sockfd, tmpbuff, sizeof(tmpbuff), 0, (struct sockaddr *)&sendaddr, &len);
- if (nsize == -1)
- {
- perror("fail to recvfrom");
- return -1;
- }
-
- printf("%s:%d>>接受到%ld个字节:%s\n",inet_ntoa(sendaddr.sin_addr),ntohs(sendaddr.sin_port),nsize,tmpbuff);
-
- close(sockfd);
-
- return 0;
- }
点击"虚拟机"
点击"设置"
点击"网络适配器"
选择"桥接模式"
点击"确定"
点击"编辑"
点击"虚拟网络编辑器"
点击"更改设置"
sudo /etc/init.d/networking restart
ifconfig
发端:socket ——> sendto ——>close
收端:socket ——> bind ——> recvfrom ——>close
1.UDP是无连接,发端退出,收端没有任何影响
2.UDP发送数据上线,最好不要超过1500字节
3.UDP是不安全不可靠的,连续且快速的传输数据容易产生数据丢失
操作流程
1.sudo wireshark
打开wireshark抓包工具
2.选择抓取数据包的网卡
any
3.执行通信的代码
4.停止通信
5.设定过滤条件
ip.addr == IP地址
udp
tcp
udp.port == 端口
源端口(2个字节)
目的端口(2字节)
长度(2字节)
校验和(2字节)