天马阁

 找回密码
 立即注册
                                        →→→→→→→→→→→→ 1点击查看所有VIP教程目录长列表(总教程数269个) 2办理VIP详情进入 ←←←←←←←←←←←←
1 x64CE与x64dbg入门基础教程 7课 已完结 2 x64汇编语言基础教程 16课 已完结 3 x64辅助入门基础教程 9课 已完结 4 C++x64内存辅助实战技术教程 149课 已完结
5 C++x64内存检测与过检测技术教程 10课 已完结 6 C+x64二叉树分析遍历与LUA自动登陆教程 19课已完结 7 C++BT功能原理与x64实战教程 29课 已完结 8 C+FPS框透视与自瞄x64实现原理及防护思路 30课完结
64驱?封? 9 64反驱? 10 64位V? 11 绝? 12 ???课?
13 64透 ? 14 64U ? 15 64Q ? 16 64功 ?
17 64U ? 18 64模 ? 19 64多 ? 20 64网 ?
21 64注 ? 22 64火 ? 23 64棋 ? 24 64自二链L?
25 64破 ? VIP会员办理QQ: 89986068   
【请先加好友,然后到好友列表双击联系客服办理,不然可能无法接受到信息。】
27 加入2000人交流群637034024 3 28 免责声明?
查看: 4247|回复: 0

WSASendTo()与WSARecvfrom()函数发送wsabuf结构体数组

[复制链接]

8

主题

0

回帖

10

积分

编程入门

Rank: 1

天马币
16
发表于 2024-3-2 09:51:15 | 显示全部楼层 |阅读模式
发送端:
void main() {

//---------------------------------------------
// Declare and initialize variables
WSADATA wsaData;
WSABUF DataBuf[2];
WSAOVERLAPPED Overlapped;
SOCKET SendSocket;
sockaddr_in RecvAddr;
sockaddr_in LocalAddr;
int RecvAddrSize = sizeof(RecvAddr);
int LocalAddrSize = sizeof(LocalAddr);
int Port = 27015;
char *ip;
char SendBuf1[1024] = "Data buffer to send1";
char SendBuf2[1024] = "Data buffer to send2";
int BufLen = 1024;
DWORD BytesSent = 0, Flags = 0;

//---------------------------------------------
// Initialize Winsock
WSAStartup(MAKEWORD(2,2), &wsaData);

//---------------------------------------------
// Create a socket for sending data
SendSocket = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0,
WSA_FLAG_OVERLAPPED);

//---------------------------------------------
// Set up the RecvAddr structure with the IP address of
// the receiver (in this example case "123.123.123.1")
// and the specified port number.
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

//---------------------------------------------
// Set up the LocalAddr structure with the local IP address
// and the specified port number.
hostent* localHost;
localHost = gethostbyname("");
ip = inet_ntoa (*(struct in_addr *)*localHost->h_addr_list);

LocalAddr.sin_family = AF_INET;
LocalAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
LocalAddr.sin_port = htons(Port);

//---------------------------------------------
// Bind the sending socket to the LocalAddr structure
// that has the internet address family, local IP address
// and specified port number.  
bind(SendSocket, (sockaddr*) &LocalAddr, LocalAddrSize);

//---------------------------------------------
// Send a datagram to the receiver
printf("Sending a datagram...\n");
DataBuf[0].len = BufLen;
DataBuf[0].buf = SendBuf1;
DataBuf[1].len = BufLen;
DataBuf[1].buf = SendBuf2;
WSASendTo(SendSocket,
  DataBuf,
  2,
  &BytesSent,
  Flags,
  (SOCKADDR*) &RecvAddr,
  RecvAddrSize,
  NULL,
  NULL);

//---------------------------------------------
// When the application is finished sending, close the socket.
printf("Finished sending. Closing socket.\n");
closesocket(SendSocket);
printf("Exiting.\n");

//---------------------------------------------
// Clean up and quit.
WSACleanup();
return;
}
接收端:
void main() {

WSADATA wsaData;
WSABUF DataBuf[2];
WSAOVERLAPPED Overlapped;
SOCKET RecvSocket;
sockaddr_in RecvAddr;
sockaddr_in SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
int Port = 27015;
char RecvBuf1[1024];
int  BufLen1 = 1024;
char RecvBuf2[1024];
int  BufLen2 = 1024;
DWORD BytesRecv = 0, Flags = 0, BytesTransferred = 0;

//-----------------------------------------------
// Initialize Winsock
WSAStartup(MAKEWORD(2,2), &wsaData);

//-----------------------------------------------
// Create a receiver socket to receive datagrams
RecvSocket = WSASocket(AF_INET,
  SOCK_DGRAM,
  IPPROTO_UDP,
  NULL,
  0,
  WSA_FLAG_OVERLAPPED);

//-----------------------------------------------
// Bind the socket to any address and the specified port.
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);

bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr));


//-----------------------------------------------
// Call the recvfrom function to receive datagrams
// on the bound socket.
DataBuf[0].len = BufLen1;
DataBuf[0].buf = RecvBuf1;
DataBuf[1].len = BufLen2;
DataBuf[1].buf = RecvBuf2;
printf("Listening for incoming datagrams...\n");
WSARecvFrom(RecvSocket,
  DataBuf,
  2,
  &BytesRecv,
  &Flags,
  (SOCKADDR *) &SenderAddr,
  &SenderAddrSize,
  NULL,
  NULL);

//---------------------------------------------
// When the application is finished receiving, close the socket.
printf("Finished receiving. Closing socket.\n");
closesocket(RecvSocket);
printf("Exiting.\n");

//---------------------------------------------
// Clean up and quit.
WSACleanup();
return;
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

天马阁|C/C++辅助教程|安卓逆向安全| 论坛导航|免责申明|Archiver||网站地图
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表天马阁立场!
任何人不得以任何方式翻录、盗版或出售本站视频,一经发现我们将追究其相关责任!
我们一直在努力成为最好的编程论坛!
Copyright© 2010-2021 All Right Reserved.
快速回复 返回顶部 返回列表