网络编程---UDP

**1.UDP:**用户数据报协议,位于传输层

2.特性:

a.无连接---发送完数据后,发送的链路自动释放,所以每次发送数据前都需要简历链路连接

b.不可靠---在发送过程中,数据在任何一个节点上都可能会丢失

c.大数据---同等情况下,UDP协议比TCP协议一包数据的正文更多

**3.UDP框架:**C/S模式

注意:服务端一定要先收后发

4.示例:向客户端发送一张图片

ser端

cs 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
 #include <arpa/inet.h>
typedef struct sockaddr*(SA);
int	main(int argc, char **argv)
{
    if(argc!=2)
    {
        printf("Usage:%s<filename>\n",argv[0]);
        return 1;
    }
    FILE*fd=fopen(argv[1], "r");
    if(fd==NULL)
   {
    perror("fopen fail");
    return 1;
   }

    int sockfd=socket(AF_INET, SOCK_DGRAM, 0);
    if(-1==sockfd)
    {
        perror("sockt fail\n");
        return 1;
    }
//man 7 ip
     struct sockaddr_in ser,cli;
     ser.sin_family=AF_INET;
     //host to net short
     ser.sin_port=htons(50000);
     ser.sin_addr.s_addr=inet_addr("192.168.1.53");

    int ret=bind(sockfd,(SA)&ser,sizeof(ser));
    if(-1==ret)
    {
        perror("bind fail\n");
        return 1;
    }
    socklen_t len=sizeof(cli);
    
    char buf[1024]={0};
    int n;
    while (1)
    {   
        char temp[512]={0};
        recvfrom(sockfd, temp, sizeof(temp),0,(SA) &cli, &len);
         bzero(buf, sizeof(buf));
        n=fread(buf, sizeof(char), sizeof(buf), fd);
        if(n<=0)
        {
            break;
        }
        sendto(sockfd, buf, n, 0, (SA)&cli, len); 
    }

    char temp[512]="finish";
    sendto(sockfd, temp, sizeof(temp), 0, (SA)&cli, len);
    close(sockfd);
    fclose(fd);
    return 0;
}

cli端

cs 复制代码
#include <stdlib.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <arpa/inet.h>
typedef struct sockaddr*(SA);
int main(int argc, char **argv)
{
   FILE*fd=fopen("picture_2.png", "w");
   if(fd==NULL)
   {
    perror("fopen fail");
    return 1;
   }
    
    int sockfd=socket(AF_INET, SOCK_DGRAM, 0);
    if(-1==sockfd)
    {
        perror("socket fail\n");
        return 1;
    }
    struct sockaddr_in ser,cli;
    ser.sin_family=AF_INET;
    //host to net short
    ser.sin_port=htons(50000);
    ser.sin_addr.s_addr=inet_addr("192.168.1.53");
     
   while(1)
   {
        char buf[1024]={0};   
        char temp[512]="hello";
        sendto(sockfd, temp,sizeof(temp), 0, (SA)&ser, sizeof(ser));
        ssize_t n=recvfrom(sockfd, buf, sizeof(buf), 0, NULL, NULL);
        if(n < 0) 
        {
            perror("recvfrom error");
            break;
        }
        if(0==strcmp(buf, "finish"))
        {
            break;
        }
        fwrite(buf, sizeof(char), n, fd);
   }
    close(sockfd);
    fclose(fd);
    return 0;
 }
相关推荐
ARoger_miu5721 小时前
11月13号作业
网络·智能路由器
Yurko1321 小时前
【计网】基于三层交换机的多 VLAN 局域网组建
网络·学习·计算机网络·智能路由器
Aric_Jones1 天前
HTTP和HTTPS的区别
网络协议·http·https
Ll13045252981 天前
如何在校园网让虚拟机联网并固定IP
网络·网络协议·tcp/ip
ha20428941941 天前
Linux操作系统学习之---初识网络
linux·网络·学习
陌路201 天前
Linux 34TCP服务器多进程并发
linux·服务器·网络
科技块儿1 天前
【IP】公有&私有IP地址?
服务器·网络协议·tcp/ip
2501_915918411 天前
移动端 HTTPS 抓包实战,多工具组合分析与高效排查指南
数据库·网络协议·ios·小程序·https·uni-app·iphone
3***49961 天前
前端WebSocket教程,实时通信案例
网络·websocket·网络协议
TangDuoduo00051 天前
【IO模型与并发服务器】
运维·服务器·网络·tcp/ip