udp program
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的更多相关文章
- Java study 1:The note of studying Socket which based UDP
UDP concept: UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interconnection,开放式系统互联) 参 ...
- Cygwin Run in the Windows(Simulation of UNIX)
Preface Environment Cygwin Run in the Windows(Simulation of UNIX) Resource Cygwin Install:http://cyg ...
- NFS排错案例
1.检验rpcinfo从客户端 # rpcinfo -p nfsserverip ,可以看到服务器端开的tcp/udp端口.默认都是打开的,客户端可以自己选择使用TCP/UDP program ver ...
- java_udp编程
两个重要的类: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/DatagramPacket.html ht ...
- 采用UDP协议的PIC32MZ ethernet bootloader
了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 经过千辛万苦,今天终于 ...
- 采用UDP协议实现PIC18F97J60 ethernet bootloader
了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). TCP/IP Stac ...
- [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 ...
- TCP/UDP端口列表
http://zh.wikipedia.org/wiki/TCP/UDP%E7%AB%AF%E5%8F%A3%E5%88%97%E8%A1%A8 TCP/UDP端口列表 本条目可通过翻译外语维 ...
- 获取Windows下某进程监听的TCP/UDP端口
1.在Windows下用CMD netstat命令可以获得当前进程监听端口号的信息,如netstat -ano可以看到IP.port.状态和监听的PID. 那么可以执行CMD这个进程得到监听的端口号信 ...
随机推荐
- IOS APP开发UI上的尺寸注意问题(屏幕、适配、分辨率)
- Java浮点运算-BigDecimal
package com.hsun.test; import static java.lang.System.out; import java.math.BigDecimal; public class ...
- Array.prototype.slice.call 和 slice以及call
单独的简单介绍,后续再补上一些资料. 对象转换为数组. /** * slice : 数组->slice(截取) * 参数有两个,开始截取和结束截取,并返回原数组: * a.slice(1) || ...
- ASP.NET Core Kestrel 随机404错误
一.Bug 出现 最近遇到一个很诡异的bug,Visual Studio 2017调试ASP.NET Core 2.2 Web程序的时候,随机性的出现404错误.如下图 事实上这个css文件是存在的, ...
- IOS手机使用Fiddler抓获HTTPS报文方法
Configure Fiddler Click Tools > Fiddler Options > Connections. Click the checkbox by Allow rem ...
- 转:介绍shell_notifyicon,SendMessage,CallWindowProc,GetWindowLong,SetWindowLong的用法
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias " Shell_NotifyIconA& ...
- Generator [ˈdʒenəreɪtə(r)] 函数结构
Generator函数是ES6新增的一种异步编程方案. 说明:Generator函数指的是一种新的语法结构,是一个遍历器对象生成器,它内部可以封装多个状态,非常适合用于异步操作. Generator函 ...
- profiler跟踪事件存为表之后性能分析工具
使用profiler建立跟踪,将跟踪结果存到表中,使用下面存储过程执行 exec temp_profiler 'tra_tablename'对表数据进行处理归类,然后进行性能分析 1.先建存储过程 2 ...
- Qt 5.3更新无数,更改C++控制台输出最为赞
迁移至 多色网
- eclipse中查看某个方法(函数)被谁调用
用了好久一直不知道eclipse中怎样实现vs中查找全部引用的功能,今天最终发现了哈哈 选中要查找的方法名,右键->References->Workspace 能够定位到详细的调用位置,快 ...