文件名称:UDP.rar
介绍说明--下载内容均来自于网络,请自行研究使用
1、首先需要定义的对象:
SOCKET ReceivingSocket //接收端的Socket
SOCKADDR_IN ReceiverAddr //接收端的地址信息
char ReceiveBuf[1024] //接收数据缓冲区大小
int BufLength //缓冲区长度
SOCKADDR_IN SenderAddr //发送数据端地址信息
int SenderAddrSize = sizeof(SenderAddr) //发送端地址信息的长度
2、启动WSA动态库:
WSAStartup(MAKEWORD(2,2),&wsd)
3、创建socket:
ReceivingSocket=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)
4、初始化服务器地址及监听端口
ReceiverAddr.sin_family=AF_INET //家族
ReceiverAddr.sin_port=htons(9000) //设置端口号
ReceiverAddr.sin_addr.s_addr=htonl(INADDR_ANY) //地址
5、绑定端口
6、接收数据:
ReceivingSocket,//接收端Socket
ReceiveBuf,//接收端用来存储数据的缓冲区
BufLength,//缓冲区的大小
0,//接收的附加选项,设置为0即可
(SOCKADDR*)&SenderAddr,//发送端的Socket地址
&SenderAddrSize//发送端Socket地址的大小 )
7、关闭socket,关闭WSA,1, you first need to define the object: the SOCKET ReceivingSocket // the the receiving end Socket SOCKADDR_IN distant ReceiverAddr // the receiving end the address information char ReceiveBuf of [1024] / / receive data buffer size int BufLength // buffer length SOCKADDR_IN distant SenderAddr // send data end address information int SenderAddrSize = sizeof (SenderAddr) // length of the address information of the sender, start WSA dynamic library: the WSAStartup (MAKEWORD (2,2), & wsd) 3, create socket: ReceivingSocket = socket ( AF_INET, SOCK_DGRAM, IPPROTO_UDP) initialize server address and listen port ReceiverAddr.sin_family = AF_INET //family ReceiverAddr.sin_port = htons (9000) //set port number ReceiverAddr.sin_addr.s_addr = htonl (INADDR_ANY) //address 5, binding port 6 the the receiver Data: ReceivingSocket,// receiving end the the Socket ReceiveBuf,// receiving end is used to store data buffer BufLength// buffer size 0,//
(系统自动生成,下载前可以参看下载内容)