本篇文章和大家大家分享的是UE4中Socket测试,不过仅作简单测试,尚未进行多线程测试。
环境:
win10 64bit
UE4.15
纯win32 console application客户端 / 服务端 (阻塞通信)
服务端测试代码:
// SocketTest.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include
#include
#include
#include
#include
#pragma comment(lib,"WS2_32.lib")
using namespace std;
int main()
{
WSADATA wsaData;
SOCKET serverSock;
SOCKADDR_IN serverAddr;
SOCKET clientSock;
SOCKADDR_IN clientAddr;
cout << "Server Start!" << endl;
int err=WSAStartup(MAKEWORD(2, 2), &wsaData);
if (0 != err)
{
cout << "WSAStartup failed!" << endl;
return 1;
}
serverSock = socket(AF_INET, SOCK_STREAM, 0);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(4399);
serverAddr.sin_addr.S_un.S_addr = INADDR_ANY;
int retVal = bind(serverSock, (SOCKADDR*)&serverAddr, sizeof(serverAddr));
if (SOCKET_ERROR == retVal)
{
cout << "bind failed!" << endl;
closesocket(serverSock);
WSACleanup();
return -1;
}
retVal = listen(serverSock, 5);
if (SOCKET_ERROR == retVal)
{
cout << "listen failed!" << endl;
closesocket(serverSock);
WSACleanup();
return -1;
}
int addrClientlen = sizeof(clientAddr);
clientSock = accept(serverSock, (SOCKADDR*)&clientAddr, &addrClientlen);
while (1)
{
char sendBuff[50];
//sprintf_s(sendBuff, "welcome %s to here", inet_ntoa(clientAddr.sin_addr));
char IPdotdec[20] = { '\0' };
inet_ntop(AF_INET, (void*)&clientAddr.sin_addr, IPdotdec, 16);
sprintf_s(sendBuff, "From Server: welcome %s to here", IPdotdec);
send(clientSock, sendBuff, st