作者:shengxin11 | 来源:互联网 | 2023-08-06 07:51
实验过程:
1.线程A在socket s上投递1号WSARecvFrom
2.Client发送"woyougexiaozhinv,kewoburenshita"
3.1号WSARecvFrom接收数据,创建线程B
4.线程B投递2号WSARecvFrom,然后线程A投递3-10号WSARecvFrom
5.线程B每隔两秒发送"nihaihaoma"
6.Client接两次后发送"woyougexiaozhinv,kewoburenshita",然后退出
实验结果:
1.Client最后发送的数据由2号WSARecvFrom接收
2.线程B第三次发送数据,显示:发送成功 影响:3号WSARecvFrom以“ret=false, dwNum=0, GetLastError()=1234, 目标地址”返回
线程B第四次发送数据,显示:发送成功 影响:4号WSARecvFrom以“ret=false, dwNum=0, GetLastError()=1234, 目标地址”返回
线程B第五次发送数据,显示:发送成功 影响:5号WSARecvFrom以“ret=false, dwNum=0, GetLastError()=1234, 目标地址”返回
。。。
实验结论:
用socket s向一个不存在的地址投递WSASendTo,显示:发送成功,结果:在s上等待接收的WSARecvFrom按投递顺序以”ret=false,dwNum=0,GetLastError()=1234,ppiod->clientAddr=目标地址“四参数返回
实验结果图:
实验代码:
#include
#include
#include
#include
#include
#include
#include
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Kernel32.lib")
#pragma comment(lib, "Mswsock.lib")
#define BUF_LEN 1024
bool flag = true;
enum OperateType
{
OP_RECV,
OP_SEND,
};
typedef struct PER_HANDLE_DATA
{
SOCKET s;
SOCKADDR_IN addr;
}PER_HANDLE_DATA, *LPPER_HANDLE_DATA;
typedef struct PER_IO_DATA
{
OVERLAPPED overlapped;
int no;
char buf[BUF_LEN];
int operationType;
sockaddr clientAddr;
}PER_IO_DATA, *LPPER_IO_DATA;
typedef struct Arg
{
SOCKET s;
sockaddr addr;
}Arg;
SOCKET SocketInitBind()
{
SOCKET s = socket(AF_INET, SOCK_DGRAM, 0);
if(INVALID_SOCKET == s)
{
std::cout<<"create socket failed : "<overlapped), sizeof(OVERLAPPED));
ppiod->operatiOnType= OP_SEND;
ppiod->clientAddr = *clientAddr;
ppiod->no = 128;
memset(ppiod->buf, 0, BUF_LEN);
memcpy(ppiod->buf, buf, len);
WSABUF databuf;
databuf.buf = ppiod->buf;
databuf.len = len;
DWORD dwRecv = 0;
DWORD dwFlags = 0;
int ret = WSASendTo(s, &databuf, 1, &dwRecv, dwFlags, clientAddr, sizeof(sockaddr), &ppiod->overlapped, NULL);
if(SOCKET_ERROR == ret && WSA_IO_PENDING != GetLastError())
{
delete ppiod;
return false;
}
return true;
}
bool PostRecvFrom(SOCKET s, int n)
{
LPPER_IO_DATA ppiod = new PER_IO_DATA;
//ZeroMemory(&(ppiod->overlapped), sizeof(OVERLAPPED));
ZeroMemory(ppiod, sizeof(PER_IO_DATA));
ppiod->operatiOnType= OP_RECV;
ppiod->no = n;
memset(ppiod->buf, 0, BUF_LEN);
WSABUF databuf;
databuf.buf = ppiod->buf;
databuf.len = BUF_LEN;
DWORD dwRecv = 0;
DWORD dwFlags = 0;
int len = sizeof(sockaddr);
int ret = WSARecvFrom(s, &databuf, 1, &dwRecv, &dwFlags, &ppiod->clientAddr, &len, &ppiod->overlapped, NULL);
if(SOCKET_ERROR == ret && WSA_IO_PENDING != GetLastError())
{
delete ppiod;
return false;
}
return true;
}
unsigned int __stdcall Func(void *param)
{
Arg *arg = (Arg*)param;
SOCKET s = arg->s;
sockaddr addr = arg->addr;
PostRecvFrom(s, 2);
while(1)
{
Sleep(2000);
std::string str = "nihaihaoma";
PostSendTo(s, &addr, str.c_str(), str.length());
}
_endthreadex(0);
return 0;
}
unsigned int __stdcall ThreadFunc(void *arg)
{
HANDLE hcp = (HANDLE)arg;
if(NULL == hcp)
{
std::cout<<"thread arg error"<no<To:"<operationType;
if(OP_RECV == type)
{
//
std::cout<<"接收完成"<clientAddr, sizeof(sin));
char *ip = inet_ntoa(sin.sin_addr);
int port = sin.sin_port;
std::cout<<"From:"<buf[dwNum] = '\0';
std::cout<<"Receiver : "<no<<" "<buf<s;
arg.addr = ppiod->clientAddr;
//测试在s等待接收时,另一线程在s上投递发送会怎样
_beginthreadex(NULL, 0, Func, (void*)&arg, 0, NULL);
//等待子线程成功读取参数
Sleep(500);
//投递3-10号接收请求
for(int i = 3; i <11; i++)
{
bool ret = PostRecvFrom(pphd->s, i);
if(false == ret)
{
std::cout<<"PostAccept Failed"<overlapped), sizeof(OVERLAPPED));
ZeroMemory(ppiod->buf, BUF_LEN);
ppiod->no = 14;
WSABUF databuf;
databuf.buf = ppiod->buf;
databuf.len = BUF_LEN;
DWORD dwRecv = 0;
DWORD dwFlags = 0;
int len = sizeof(sockaddr);
WSARecvFrom(pphd->s, &databuf, 1, &dwRecv, &dwFlags, &ppiod->clientAddr, &len, &ppiod->overlapped, NULL);*/
}
else if(OP_SEND == type)
{
//
std::cout<<"发送完成"<clientAddr, sizeof(sin));
std::cout<<"To:"< setWorkers;
SYSTEM_INFO si;
GetSystemInfo(&si);
for(int i = 0; i s = s;
CreateIoCompletionPort((HANDLE)s, hcp, (DWORD)pphd, 0);
//投递编号1接收请求
bool ret = PostRecvFrom(s, 1);
if(false == ret)
{
std::cout<<"PostAccept Failed"<
客户端代码:
#include
#include
#include
#pragma comment(lib, "Ws2_32.lib")
int main()
{
WSADATA ws;
if(WSAStartup(MAKEWORD(2, 2), &ws) != 0)
return -1;
sockaddr_in addr;
int len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(4444);
addr.sin_addr.s_addr = inet_addr("192.168.15.14");
SOCKET s = socket(AF_INET, SOCK_DGRAM, 0);
std::string str = "woyougexiaozhinv,kewoburenshita";
int ret = sendto(s, str.c_str(), str.length(), 0, (sockaddr*)&addr, len);
if (SOCKET_ERROR == ret)
{
std::cout<<"Data Send Fail"<