本文主要是介绍嵌入式学习——Linux高级编程复习(UDP编程)——day43,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. UDP编程——函数接口
1.1 socket
1. 定义
int socket(int domain, int type, int protocol);
2. 功能
创建一个用来进程通信的套接字,返回文件描述符
3. 参数
domain:AF_INET IPv4协议族
type:SOCK_STREAM 流式套接字 tcp传输协议
SOCK_DGRAM 数据报套接字 udp传输协议
SOCK_RAW 原始套接字
protocol:
默认为0
4. 返回值
成功返回套接字新文件描述符
失败返回-1
5. 注意
1.2 inet_addr
1. 定义
in_addr_t inet_addr(const char *cp);
2. 功能
将字符串IP地址转换为二进制IP地址
3. 参数
cp:字符串IP地址空间首地址
4. 返回值
成功返回二进制IP地址
5. 注意
1.3 htons
1. 定义
uint16_t htons(uint16_t hostshort);
2. 功能
将本地字节序(小端)转换为网络字节序(大端)
3. 参数
hostshort:本地端口号
4. 返回值
返回网络字节序端口号
uint16_t ntohs(uint16_t netshort);
功能:
将网络字节序(大端)转换为本地字节序(小端)
1.4 bind
1. 定义
int bind(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
2. 功能
将套接字与IP地址端口绑定在一起
3. 参数
sockfd:文件描述符
addr:结构体空间首地址
addrlen:信息的长度
4. 返回值
成功返回0
失败返回-1
5. 注意
1.5 sendto
1. 定义
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen);
2. 功能
给另一个套接字发送消息
3. 参数
sockfd:套接字文件描述符
buf:要发送数据存放空间的首地址
len:要发送数据的长度
flags:发送属性 默认为0
dest_addr:目的地址
addrlen:目的地址信息长度
struct sockaddr_in {
sa_family_t sin_family; /* address family: AF_INET */(协议族)
in_port_t sin_port; /* port in network byte order */(端口号、小端存储要改成大端存储)
struct in_addr sin_addr; /* internet address */(IP地址)
};
/* Internet address. */
struct in_addr {
uint32_t s_addr; /* address in network byte order */
};
4. 返回值
成功返回发送字节个数
失败返回-1
1.6 recvfrom
1. 定义
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);
2. 功能
接收数据
3. 参数
sockfd:套接字文件描述符
buf:存放接收到数据空间的首地址
len:最多允许接收的字节数
flags:属性 默认为0
src_addr:存放发送端地址信息空间首地址
addrlen:想要接收发送端地址大小的变量空间首地址
4. 返回值
成功返回实际接收字节数
失败返回-1
5. 注意
该函数具有阻塞功能
2. UDP编程示例程序
2.1 单方向收发
1. 头文件
#ifndef __HEAD_H__
#define __HEAD_H__#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <arpa/inet.h>#endif
2.发送端代码(send.c)
#include "head.h"int main(int argc, char const *argv[])
{int sockfd = 0;char tmpbuff[1024] = {"hello world"};struct sockaddr_in recvaddr;ssize_t nsize = 0;sockfd = socket(AF_INET, SOCK_DGRAM, 0);if (-1 == sockfd){perror("fail to socket");return -1;}recvaddr.sin_family = AF_INET;recvaddr.sin_port = htons(50000);recvaddr.sin_addr.s_addr = INADDR_ANY;nsize = sendto(sockfd, tmpbuff, strlen(tmpbuff)+1, 0, (struct sockaddr *)&recvaddr, sizeof(recvaddr));if (-1 == nsize){perror("fail to sendto");return -1;}printf("send %ld bytes success\n", nsize);close(sockfd);return 0;
}
3. 接收端代码(recv.c)
#include "head.h"int main(int argc, char const *argv[])
{int sockfd = 0;int ret = 0;ssize_t nsize = 0;char tmpbuff[4096] = {0};struct sockaddr_in recvaddr;sockfd = socket(AF_INET, SOCK_DGRAM, 0);if (-1 == sockfd){perror("fail to socket");return -1;}recvaddr.sin_family = AF_INET;recvaddr.sin_port = htons(50000);recvaddr.sin_addr.s_addr = INADDR_ANY;ret = bind(sockfd, (struct sockaddr *)&recvaddr, sizeof(recvaddr));if (-1 == ret){perror("fail to bind");return -1;}nsize = recvfrom(sockfd, tmpbuff, sizeof(tmpbuff), 0, NULL, NULL);if (-1 == nsize){perror("fail to recvfrom");return -1;}printf("recv %ld bytes success\n", nsize);printf("RECV:%s\n", tmpbuff);close(sockfd);return 0;
}
2.2 双向通信
1. 头文件
#ifndef __HEAD_H__
#define __HEAD_H__#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <arpa/inet.h>#endif
2.发送端代码(send.c)
#include "head.h"int main(int argc, char const *argv[])
{int sockfd = 0;char tmpbuff[4096] = {"你在吗?"};struct sockaddr_in recvaddr;ssize_t nsize = 0;sockfd = socket(AF_INET, SOCK_DGRAM, 0);if (-1 == sockfd){perror("fail to socket");return -1;} recvaddr.sin_family = AF_INET;recvaddr.sin_port = htons(50000);recvaddr.sin_addr.s_addr = INADDR_ANY;nsize = sendto(sockfd, tmpbuff, strlen(tmpbuff)+1, 0, (struct sockaddr *)&recvaddr, sizeof(recvaddr));if (-1 == nsize){perror("fail to sendto");return -1;}memset(tmpbuff, 0, sizeof(tmpbuff));nsize = recvfrom(sockfd, tmpbuff, sizeof(tmpbuff), 0, NULL, NULL);if (-1 == nsize){perror("fail to recvfrom");return -1;}printf("RECV:%s\n", tmpbuff);close(sockfd);return 0;
}
3. 接收端代码(recv.c)
#include "head.h"int main(int argc, char const *argv[])
{int sockfd = 0;int ret = 0;struct sockaddr_in recvaddr;struct sockaddr_in sendaddr;char tmpbuff[4096] = {0};ssize_t nsize = 0;socklen_t addrlen = sizeof(sendaddr);sockfd = socket(AF_INET, SOCK_DGRAM, 0);if (-1 == sockfd){perror("fail to socket");return -1;} recvaddr.sin_family = AF_INET;recvaddr.sin_port = htons(50000);recvaddr.sin_addr.s_addr = INADDR_ANY;ret = bind(sockfd, (struct sockaddr *)&recvaddr, sizeof(recvaddr));if (-1 == ret){perror("fail to bind");return -1;}nsize = recvfrom(sockfd, tmpbuff, sizeof(tmpbuff), 0, (struct sockaddr *)&sendaddr, &addrlen);if (-1 == nsize){perror("fail to recvfrom");return -1;}printf("[%s:%d]%s\n", inet_ntoa(sendaddr.sin_addr), ntohs(sendaddr.sin_port), tmpbuff);sprintf(tmpbuff, "%s --------echo", tmpbuff);nsize = sendto(sockfd, tmpbuff, strlen(tmpbuff), 0, (struct sockaddr *)&sendaddr, sizeof(sendaddr));if (-1 == nsize){perror("fail to sendto");return -1;}close(sockfd);return 0;
}
2.3 使用UDP进行文件复制(CP)
1.服务端
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>typedef struct sockaddr * (SA);int main(int argc, char *argv[])
{int sockfd = socket(AF_INET,SOCK_DGRAM,0);if(-1 == sockfd){perror("socket");exit(1);}struct sockaddr_in ser,cli;//man 7 ip bzero(&ser,sizeof(ser)); bzero(&cli,sizeof(ser));ser.sin_family = AF_INET;ser.sin_port = htons(50000);//host to net short ser.sin_addr.s_addr = INADDR_ANY ;//inet_addr("127.0.0.1")int ret = bind(sockfd,(SA)&ser,sizeof(ser));if(-1 == ret){perror("bind");exit(1);}int fd = open("2.png",O_CREAT|O_WRONLY|O_TRUNC,0666);if(-1 == fd){perror("open");exit(1);}while(1){char buf[1024]={0};socklen_t len = sizeof(cli);int rd_ret = recvfrom(sockfd,buf,sizeof(buf),0,(SA)&cli,&len);if(0 == strcmp(buf,"^_^")){break;}write(fd,buf,rd_ret);bzero(buf,sizeof(buf));strcpy(buf,"go on");sendto(sockfd,buf,strlen(buf),0,(SA)&cli,sizeof(cli));}close(fd);close(sockfd);return 0;
}
2. 客户端
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>
#include <fcntl.h>typedef struct sockaddr * (SA);
int main(int argc, char *argv[])
{int sockfd = socket(AF_INET,SOCK_DGRAM,0);if(-1 == sockfd){perror("socket");exit(1);}struct sockaddr_in ser,cli;//man 7 ip bzero(&ser,sizeof(ser)); bzero(&cli,sizeof(ser));ser.sin_family = AF_INET;ser.sin_port = htons(50000);//host to net short ser.sin_addr.s_addr = INADDR_ANY;int fd = open("/home/linux/1.png",O_RDONLY);if(-1 == fd){perror("open");exit(1);}char buf[1024]={0};socklen_t len = sizeof(ser);while(1){bzero(buf,sizeof(buf));int rd_ret = read(fd,buf,sizeof(buf));if(rd_ret<=0){break;}sendto(sockfd,buf,rd_ret,0,(SA)&ser,len);bzero(buf,sizeof(buf));recvfrom(sockfd,buf,sizeof(buf),0,NULL,NULL);}strcpy(buf,"^_^");sendto(sockfd,buf,strlen(buf),0,(SA)&ser,len);close(fd);close(sockfd);return 0;
}
2.4 使用UDP进行两端聊天(chat)
1. 服务端
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>
#include <time.h>
#include <pthread.h>typedef struct sockaddr * (SA);struct sockaddr_in ser,cli;void* th1(void* arg)
{int sockfd =* (int*)arg;while(1){char buf[256]={0};socklen_t len = sizeof(cli);recvfrom(sockfd,buf,sizeof(buf),0,NULL,NULL);printf("from c:%s\n",buf);}}void* th2(void* arg)
{int sockfd =* (int*)arg;while(1){printf("to c:");char buf[256]={0};fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1]='\0';sendto(sockfd,buf,strlen(buf),0,(SA)&cli,sizeof(cli));}}int main(int argc, char *argv[])
{int sockfd = socket(AF_INET,SOCK_DGRAM,0);if(-1 == sockfd){perror("socket");exit(1);}//man 7 ip bzero(&ser,sizeof(ser)); bzero(&cli,sizeof(ser));ser.sin_family = AF_INET;ser.sin_port = htons(50000);//host to net short ser.sin_addr.s_addr = INADDR_ANY;int ret = bind(sockfd,(SA)&ser,sizeof(ser));if(-1 == ret){perror("bind");exit(1);}pthread_t tid1,tid2;socklen_t len = sizeof(cli);char buf[256]={0};recvfrom(sockfd,buf,sizeof(buf),0,(SA)&cli,&len);pthread_create(&tid1,NULL,th1,&sockfd);pthread_create(&tid2,NULL,th2,&sockfd);pthread_join(tid1,NULL);pthread_join(tid2,NULL);return 0;
}
2. 客户端
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>
#include <pthread.h>typedef struct sockaddr * (SA);struct sockaddr_in ser,cli;
void* th1(void* arg)
{int sockfd =* (int*)arg;while(1){char buf[256]={0};socklen_t len = sizeof(cli);recvfrom(sockfd,buf,sizeof(buf),0,NULL,NULL);printf("from s:%s\n",buf);}}
void* th2(void* arg)
{int sockfd =* (int*)arg;while(1){printf("to s");char buf[256]={0};fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1]='\0';sendto(sockfd,buf,strlen(buf),0,(SA)&ser,sizeof(ser));}}
int main(int argc, char *argv[])
{int sockfd = socket(AF_INET,SOCK_DGRAM,0);if(-1 == sockfd){perror("socket");exit(1);}//man 7 ip bzero(&ser,sizeof(ser)); bzero(&cli,sizeof(ser));ser.sin_family = AF_INET;ser.sin_port = htons(50000);//host to net short ser.sin_addr.s_addr = INADDR_ANY; pthread_t tid1,tid2;char buf[256]="start";sendto(sockfd,buf,strlen(buf),0,(SA)&ser,sizeof(ser));pthread_create(&tid1,NULL,th1,&sockfd);pthread_create(&tid2,NULL,th2,&sockfd);pthread_join(tid1,NULL);pthread_join(tid2,NULL);return 0;
}
3. UDP特性
1.实现简单
2.占用资源较小
3.不安全、不可靠
这篇关于嵌入式学习——Linux高级编程复习(UDP编程)——day43的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!