我要成为嵌入式高手之3月6日Linux高编第十六天!!
————————————————————————————
学习笔记
接收端
recvfrom
#include <sys/types.h>
#include <sys/socket.h>
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;
inet_ntoa
功能:将二进制转换为字符串
ntohs
功能:将大端字节序转换为本地字节序
#include "head.h"
int main(void)
{
int sockfd = 0;
struct sockaddr_in recvaddr;
struct sockaddr_in tmpaddr;
char tmpbuff[1024] = {0};
ssize_t nsize = 0;
ssize_t nsize1 = 0;
char recvbuff[1024] = {0};
socklen_t recvbufflen;
recvbufflen = sizeof(tmpaddr);
fgets(tmpbuff, sizeof(tmpbuff), stdin);
// while (1)
{
/*创建用来通信的UDP套接字*/
sockfd = socket(AF_INET, SOCK_DGRAM, 0);//通信使用的协议族(AF_INET是IPv4协议族),套接字类型:数据报套接字;默认0
if (-1 == sockfd)
{
perror("fail to socket");
return -1;
}
/*对接收方地址赋值*/
recvaddr.sin_family = AF_INET;
recvaddr.sin_port = htons(50001);//本地字节序转换为网络的大端字节序
recvaddr.sin_addr.s_addr = inet_addr("192.168.1.101");//函数接口inet_addr将字符串的ip地址转换为内存当中的IP地址
//前面是ip地址转换为32位之后的变量名
/*发送信息*/
nsize = sendto(sockfd, tmpbuff, strlen(tmpbuff), 0, (struct sockaddr *)&recvaddr, sizeof(recvaddr));
//0为发送的属性,默认为0,发送到哪里(那个空间的首地址),那个地方的空间大小
if (-1 == nsize)
{
perror("fail to sendto");
return -1;
}
//printf("成功发送%ld字节!\n", nsize);
/*接收信息*/
nsize1 = recvfrom(sockfd, recvbuff, sizeof(recvbuff), 0, (struct sockaddr *)&tmpaddr, &recvbufflen);
if (-1 == nsize1)
{
perror("fail to recvform");
return -1;
}
printf("RECV ASCII FROM %s:%d %s\n", inet_ntoa(tmpaddr.sin_addr), ntohs(tmpaddr.sin_port), recvbuff);
}
close(sockfd);
return 0;
}
bind
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
功能:在套接字绑定一个IP地址和端口号
参数:
sockfd:套接字的文件描述符
addr:绑定的IP地址空间的首地址
addrlen:绑定IP地址的长度
返回值:成功0,失败-1;
UDP编程
发端:
socket -> sendto -> close
收端:
socket -> bind -> recvfrom -> close
发端:
#include "head.h"
int main(void)
{
int sockfd = 0;
char tmpbuff[1024] = {0};
struct sockaddr_in myaddr;
struct sockaddr_in recvaddr;
ssize_t nsize = 0;
int ret = 0;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sockfd)
{
perror("fail to socket");
return -1;
}
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(54321);
myaddr.sin_addr.s_addr = inet_addr("192.168.1.172");
ret = bind(sockfd, (struct sockaddr *)&myaddr, sizeof(myaddr));
if (ret == -1)
{
perror("fail to bind");
return -1;
}
fgets(tmpbuff, sizeof(tmpbuff), stdin);
recvaddr.sin_family = AF_INET;
recvaddr.sin_port = htons(30000);
recvaddr.sin_addr.s_addr = inet_addr("192.168.1.167");
nsize = sendto(sockfd, tmpbuff, strlen(tmpbuff), 0, (struct sockaddr *)&recvaddr, sizeof(recvaddr));
if (-1 == nsize)
{
perror("fail to sendto");
return -1;
}
printf("成功发送%ld字节数据\n", strlen(tmpbuff));
close(sockfd);
return 0;
}
收端:
#include "head.h"
int main(void)
{
int sockfd = 0;
struct sockaddr_in myaddr;
int ret = 0;
size_t nsize = 0;
char tmpbuff[1024] = {0};
struct sockaddr_in sendaddr;
socklen_t sendaddrlen;
sendaddrlen = sizeof(sendaddr);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sockfd)
{
perror("fail to sockfd");
return -1;
}
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(33333);
myaddr.sin_addr.s_addr = inet_addr("192.168.1.172");
ret = bind(sockfd, (struct sockaddr *)&myaddr, sizeof(myaddr));
if (-1 == ret)
{
perror("fail to bind");
return -1;
}
nsize = recvfrom(sockfd, tmpbuff, sizeof(tmpbuff), 0, (struct sockaddr *)&sendaddr, &sendaddrlen);
if (-1 == nsize)
{
perror("fail to recvfrom");
return -1;
}
printf("RECV ASCII FROM %s:%d %s", inet_ntoa(sendaddr.sin_addr), ntohs(sendaddr.sin_port), tmpbuff);
return 0;
}
练习:
编写程序实现两台主机间传输一个文件
UPD中需要注意的点
1、UDP是无连接的,发端退出收端没有任何影响
2、UDP发送数据上限,最好不要超过1500字节
3、UDP是不安全不可靠的,连续且快速的传输数据容易产生数据丢失
wireshark
抓包工具
操作流程
1、打开wireshrak
2、选择抓取数据报的网卡(any)
3、执行通信的代码
4、停止通信
5、设定过滤条件
ip.addr == IP地址
UDP\TCP\
udp.port == 端口号
UDP包头
长度:8字节
源端口号(2字节)
目标端口号(2字节)
包长度(2字节)
校验和(2字节)