UDP网络编程应用

UDP网络通信流程
1,首先,服务器端需要调用socket()函数,建立socket对象,指定通信协议(tcp,udp,ipv4,ipv6),调用bind()函数,将创建的socket对象与某一个udp端口绑定。
2,接着通信双方进行数据传输,发送方调用sendto()函数或sendmsg()函数发送数据,而接收方调用recvfrom()或recvmsg()函数接收数据,通信完成后双方都要调用close()或shutdown()函数关闭socket对象。
一、使用AF_INET实现UDP点对点通信示例
(1)首先使用AF_INET协议,创建基于数据报的socket对象
(2)绑定IP地址和端口,保持端口一致(接收端和发送端一致)
(3)接收端阻塞式接收数据(来一个接收一个,不然等待)
(4)如果接收到数据,将其打印出来

接收端源代码如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<math.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/mman.h>
#include<time.h>
#include<dirent.h>
#include<sys/errno.h>
#include<sys/wait.h>
#include<signal.h>
#include<sys/param.h>
#include<sys/syslog.h>
#include<limits.h>
#include<sys/time.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/sem.h>
#include<errno.h>
#include<sys/shm.h>
#include<pthread.h>
#include<sys/syscall.h>
#include<semaphore.h>
#include<bits/pthreadtypes.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<sys/socket.h>
int main(int argc,char argv[])
{
struct sockaddr_in s_addr,c_addr;
int sock;
socklen_t addr_len;
int len;
char buff[128];
if((sock=socket(AF_INET,SOCK_DGRAM,0))==-1) //使用AF)INET,UDP方式创建
{
perror(“socket”);
exit(errno);
}
else
printf(“create socket.\n”);
memset(&s_addr,0,sizeof(struct sockaddr_in));
s_addr.sin_family=AF_INET; //协议设为AF_INET
s_addr.sin_port=htons(7838); //接收端口开饭7838端口
s_addr.sin_addr.s_addr=INADDR_ANY; //表示本地任意IP信息,为自己绑定ip信息
if((bind(sock,(struct sockaddr
)&s_addr,sizeof(s_addr)))==-1) //绑定socket对象信息
{
perror(“bind”);
exit(errno);
}
else
printf(“bind addres to socket.\n\r”);
addr_len=sizeof(s_addr);
while(1) // 一直接收消息
{
//接收数据
len=recvfrom(sock,buff,sizeof(buff)-1,0,(struct sockaddr *)&c_addr,&addr_len);
if(len<0)
{
perror(“recvfrom”);
exit(errno);
}
buff[len]=’\0’;
printf(“recive come from %s:%d messae:%s”,inet_ntoa(c_addr.sin_addr),ntohs(c_addr.sin_port),buff);

}
return 0;
}

发送端源代码如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<math.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/mman.h>
#include<time.h>
#include<dirent.h>
#include<sys/errno.h>
#include<sys/wait.h>
#include<signal.h>
#include<sys/param.h>
#include<sys/syslog.h>
#include<limits.h>
#include<sys/time.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/sem.h>
#include<errno.h>
#include<sys/shm.h>
#include<pthread.h>
#include<sys/syscall.h>
#include<semaphore.h>
#include<bits/pthreadtypes.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<sys/socket.h>
int main(int argc,char *argv[])
{
struct sockaddr_in s_addr;
int sock;
int addr_len;
int len;
char buff[128];
if((sock=socket(AF_INET,SOCK_DGRAM,0))==-1) //创建socket对象
{
perror(“socket”);
exit(errno);
}
else
printf(“create socket.\n\r”);
s_addr.sin_family=AF_INET; //指定IP协议
s_addr.sin_port=htons(7838); //与接送端对应
if(argv[1])
{
s_addr.sin_addr.s_addr=inet_addr(argv[1]); //inet_addr 将点分十进制ip地址转换为网络字节顺序的ip地址信息。
}
else
{
printf(“input sever ip!\n”);
exit(0);
}
addr_len=sizeof(s_addr);
strcpy(buff,“hello i’m here”); //将hello i’m here写入buff里面,作为数据传输
//发送数据j,将buff里面的字符串发送给接收端
len=sendto(sock,buff,strlen(buff),0,(struct sockaddr *)&s_addr,addr_len);
if(len<0)
{
printf("\n\rsend error.\n\r");
return 3;
}
printf(“send success\n\r”);

return 0;
}
运行结果
UDP网络编程应用
UDP网络编程应用

点对点通信成功,在同一个主机上正常运行,在输出端需要输入指定IP地址。sendto函数的参数如下;
UDP网络编程应用
recvfrom函数用法如下:
UDP网络编程应用