C++ winsock 接受抛出的异常
c++ winsock accept thrown exception
我用的是UE4 4.26.2 github源码,windows10 1909,visual studio 2019.
代码:
// 32-bit unsigned integer
typedef unsigned int uint32;
// 64-bit unsigned integer
typedef unsigned long long uint64;
template<typename T32BITS, typename T64BITS>
struct SelectIntPointerType<T32BITS, T64BITS, 8>
{
// Select the 64 bit type.
typedef T64BITS TIntPointer;
};
typedef SelectIntPointerType<uint32, uint64, sizeof(void*)>::TIntPointer UPTRINT;
UPTRINT TcpSocketListen(uint16 Port)
{
// See TcpSocketConnect() for why WSASocket() is used here.
SOCKET Socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, nullptr, 0, WSA_FLAG_NO_HANDLE_INHERIT);
sockaddr_in SockAddr;
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = 0;
SockAddr.sin_port = htons(Port);
int Result = bind(Socket, (SOCKADDR*)&SockAddr, sizeof(SockAddr));
Result = listen(Socket, 1);
if (!TcpSocketSetNonBlocking(Socket, 1))
{
closesocket(Socket);
return 0;
}
return UPTRINT(Socket) + 1;
}
////////////////////////////////////////////////////////////////////////////////
int32 TcpSocketAccept(UPTRINT Socket, UPTRINT& Out)
{
SOCKET Inner = Socket - 1;
Inner = accept(Inner, nullptr, nullptr);
}
static UPTRINT GControlListen = 0;
GControlListen = TcpSocketListen(1985);
UPTRINT Socket;
int Return = TcpSocketAccept(GControlListen, Socket);
调试时,visual studio在
line:accept(内部, nullptr, nullptr);
输出:
在 0x00007FFCDF673B19 处抛出异常(在 UE4Editor-Win64-Debug.exe 中):Microsoft C++ 异常:内存位置 0x0000001F892FFC38 处的 int。
最良好的祝愿,
点
accept 上的套接字文档:
The argument sockfd is a socket that has been created with
socket(2), bound to a local address with bind(2), and is
listening for connections after a listen(2).
所以第一个参数必须只是一个套接字描述符。
您正在将该套接字转换为指针,将地址加 1,这将完全改变套接字中返回值的值。然后传递该数字以接受并减去一个。套接字库不知道该数字的含义并崩溃。
只需在后续调用中使用从套接字库返回的原始值。
我用的是UE4 4.26.2 github源码,windows10 1909,visual studio 2019.
代码:
// 32-bit unsigned integer
typedef unsigned int uint32;
// 64-bit unsigned integer
typedef unsigned long long uint64;
template<typename T32BITS, typename T64BITS>
struct SelectIntPointerType<T32BITS, T64BITS, 8>
{
// Select the 64 bit type.
typedef T64BITS TIntPointer;
};
typedef SelectIntPointerType<uint32, uint64, sizeof(void*)>::TIntPointer UPTRINT;
UPTRINT TcpSocketListen(uint16 Port)
{
// See TcpSocketConnect() for why WSASocket() is used here.
SOCKET Socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, nullptr, 0, WSA_FLAG_NO_HANDLE_INHERIT);
sockaddr_in SockAddr;
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = 0;
SockAddr.sin_port = htons(Port);
int Result = bind(Socket, (SOCKADDR*)&SockAddr, sizeof(SockAddr));
Result = listen(Socket, 1);
if (!TcpSocketSetNonBlocking(Socket, 1))
{
closesocket(Socket);
return 0;
}
return UPTRINT(Socket) + 1;
}
////////////////////////////////////////////////////////////////////////////////
int32 TcpSocketAccept(UPTRINT Socket, UPTRINT& Out)
{
SOCKET Inner = Socket - 1;
Inner = accept(Inner, nullptr, nullptr);
}
static UPTRINT GControlListen = 0;
GControlListen = TcpSocketListen(1985);
UPTRINT Socket;
int Return = TcpSocketAccept(GControlListen, Socket);
调试时,visual studio在 line:accept(内部, nullptr, nullptr);
输出: 在 0x00007FFCDF673B19 处抛出异常(在 UE4Editor-Win64-Debug.exe 中):Microsoft C++ 异常:内存位置 0x0000001F892FFC38 处的 int。
最良好的祝愿, 点
accept 上的套接字文档:
The argument sockfd is a socket that has been created with socket(2), bound to a local address with bind(2), and is listening for connections after a listen(2).
所以第一个参数必须只是一个套接字描述符。 您正在将该套接字转换为指针,将地址加 1,这将完全改变套接字中返回值的值。然后传递该数字以接受并减去一个。套接字库不知道该数字的含义并崩溃。
只需在后续调用中使用从套接字库返回的原始值。