UDP program

UDP常用函数:recvfrom和sendto

  • recvfrom

ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr * from, socklen_t *addrlen);

  • sendto

ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t addrlen);

sockfd, buf,len和read,write一样。

recvfrom负责从sockfd接收数据,如果from不是NULL,那么在from里面存储了信息来源的情况,如果对信息来源不感兴趣,可以将from和addrlen设置为NULL。

sendto负责向to发送信息,此时在to里面存储了收信息方的详细资料。

flags一般设置为0即可。

返回值:成功返回发送或接收的字节数,失败返回-1,并且设置errno。

注:recvfrom中addrlen一定要正确初始化,否则引起错误。

The argument addrlen is a value-result argument, which the caller should initialize before the call to the size of the buffer associated with from, and modified on return to indicate the actual size of the source address. The returned address is truncated if the buffer provided is too small; in this case, addrlen will return a value greater than was supplied to the call.

注:sendto对方主机崩溃或主程序关闭,也可成功返回发送字节数。(UDP直接把数据发送给IP层就算成功了。)

服务器端:socket -> bind -> recvfrom -> sendto -> close

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in.h> char rbuf[]; int main()
{
int sockfd;
int size;
int ret;
int on =;
struct sockaddr_in saddr;
struct sockaddr_in raddr; //设置地址信息,ip信息
size = sizeof(struct sockaddr_in);
bzero(&saddr,size);
saddr.sin_family = AF_INET;
saddr.sin_port = htons();
saddr.sin_addr.s_addr = htonl(INADDR_ANY); //创建udp 的套接字
sockfd = socket(AF_INET,SOCK_DGRAM,);
if(sockfd<)
{
perror("socket failed");
return -;
} //设置端口复用
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); //绑定地址信息,ip信息
ret = bind(sockfd,(struct sockaddr*)&saddr,sizeof(struct sockaddr));
if(ret<)
{
perror("sbind failed");
return -;
} int val = sizeof(struct sockaddr);
//循环接收客户端发来的消息
while()
{
puts("waiting data");
ret=recvfrom(sockfd,rbuf,,,(struct sockaddr*)&raddr,&val);
if(ret <)
{
perror("recvfrom failed");
} printf("the data :%s\n",rbuf);
bzero(rbuf,);
}
//关闭udp套接字,这里不可达的。
close(sockfd);
return ;
}

客户端: socket -> sendto -> recvfrom -> close

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in.h> char wbuf[]; int main()
{
int sockfd;
int size,on = ;
struct sockaddr_in saddr;
int ret; size = sizeof(struct sockaddr_in);
bzero(&saddr,size); //设置地址信息,ip信息
saddr.sin_family = AF_INET;
saddr.sin_port = htons();
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");//192.168.152.128为服务端所在的ip,由于本代码是本机测试,所以写的是自己的ip //创建udp 的套接字
sockfd= socket(AF_INET,SOCK_DGRAM,);
if(sockfd<)
{
perror("failed socket");
return -;
}
//设置端口复用
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); //循环发送信息给服务端
while()
{
puts("please enter data:");
scanf("%s",wbuf);
ret=sendto(sockfd,wbuf,,,(struct sockaddr*)&saddr,
sizeof(struct sockaddr));
if(ret<)
{
perror("sendto failed");
} bzero(wbuf,);
}
close(sockfd);
return ;
}

udp program的更多相关文章

  1. Java study 1:The note of studying Socket which based UDP

    UDP concept: UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interconnection,开放式系统互联) 参 ...

  2. Cygwin Run in the Windows(Simulation of UNIX)

    Preface Environment Cygwin Run in the Windows(Simulation of UNIX) Resource Cygwin Install:http://cyg ...

  3. NFS排错案例

    1.检验rpcinfo从客户端 # rpcinfo -p nfsserverip ,可以看到服务器端开的tcp/udp端口.默认都是打开的,客户端可以自己选择使用TCP/UDP program ver ...

  4. java_udp编程

    两个重要的类: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/DatagramPacket.html ht ...

  5. 采用UDP协议的PIC32MZ ethernet bootloader

    了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 经过千辛万苦,今天终于 ...

  6. 采用UDP协议实现PIC18F97J60 ethernet bootloader

    了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). TCP/IP Stac ...

  7. [Top-Down Approach]My First C/S Program [Python]

    These days I was learning from Computer Networking --A Top-Down Approach by Kurose Ross. I modified ...

  8. TCP/UDP端口列表

    http://zh.wikipedia.org/wiki/TCP/UDP%E7%AB%AF%E5%8F%A3%E5%88%97%E8%A1%A8 TCP/UDP端口列表     本条目可通过翻译外语维 ...

  9. 获取Windows下某进程监听的TCP/UDP端口

    1.在Windows下用CMD netstat命令可以获得当前进程监听端口号的信息,如netstat -ano可以看到IP.port.状态和监听的PID. 那么可以执行CMD这个进程得到监听的端口号信 ...

随机推荐

  1. tableLayoutPanel 列宽度设置

    public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); ].W ...

  2. 用CSS下划线距离

    但在我在CSS中新加了TEXT-DECORATION: underline; 后发现下划线离文本太近了,很难看. 代码一: a { text-decoration: none; background: ...

  3. Python-urllib学习记录

    urllib是python自带库,不要专门安装,还挺好用的. 脚本语言的好处之一就是随写随用,有些东西用C语言写真的是能把人累死,换成python就是几行代码,so easy,对于喜欢偷懒的同学绝对是 ...

  4. SQL语句中拆分字段

    SELECT PARSENAME(replace(MONITOR_ROOM_ID,'^' , '.'), 1) AS RoomID FROM ZY_MONITOR_ROOM 遇到以前系统高人设计的表, ...

  5. Word该值小于列表中的前一条目

    在Word中进行多级编号时,前面一个二级标题是2.1,可后面一个二级标题是4.1,于是想把4.1改成2.2,使用 选中有问题的编号,右击"设置列表缩进",在打开的对话框中我们可以看 ...

  6. win10 家庭中文版打开本地组策略编辑器

      win10 家庭中文版打开本地组策略编辑器 CreateTime--2018年5月14日09:01:25 Author:Marydon 1.问题描述 2.问题解析 win10家庭版没有访问本地组策 ...

  7. 36、TreeSet详解

    TreeSet是SortedSet接口的实现类,TreeSet可以保证元素处于排序状态.与HashSet相比,TreeSet还提供了如下几个而外的方法: 1).Comparator comparato ...

  8. Ubuntu和windows文件共享问题

    ubuntu访问windows共享文件夹(ubuntu桌面系统):          最简单的方法,随便打开一个文件夹,按Ctrl+L,然后地址栏敲smb://xxx.xxx.xxx.xxx(wind ...

  9. Linux下挂载硬盘分区的几种方法

    1.使用Autofs自动挂载分区 2.修改/etc/fstab 3.编写shell脚本,开机自动运行mount命令  方法一.使用Autofs  1.Autofs的特点:Autofs与Mount/Um ...

  10. Android中TextView内容过长加省略号

          textview中有个内容过长加省略号的属性,即ellipsize,用法如下: 在xml中 Android:ellipsize = "end"   省略号在结尾 and ...