// USocket.cpp : 定义控制台应用程序的入口点。
//
//#define printf_s printf
#include "stdafx.h"
#include "USocket.h"
#include <stdio.h>
#include <list>
OnReceive* OnTCPReceive;
OnServerAccept* OnServeAccept;
OnReceiveForm* OnServerReceive;
OnReceiveForm* OnUDPReceive;
void UDPReceiveThread(SOCKET* udpclient);
void TCPReceiveThread(SOCKET* client);
void ServerReceiveThread(PeerSocket* peer);
void ServerAcceptThread(SOCKET* server);
std::list<SOCKET*>* tcpclients;
std::list<SOCKET*>* udpclients;
std::list<SOCKET*>* tcpservers;
WSADATA wsaData;
void Init()
{
tcpclients = new std::list<SOCKET*>();
udpclients = new std::list<SOCKET*>();
tcpservers = new std::list<SOCKET*>();
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result)printf_s("Init error:%d\n", WSAGetLastError());
}
FILE* consolefile;
void OpenConsole()
{
AllocConsole();
freopen_s(&consolefile, "CONOUT$", "w+t", stdout);
//consolefile = freopen("CONOUT$", "w+t", stdout);
}
void CloseConsole()
{
fclose(consolefile);
FreeConsole();
}
void Shutdown()
{
WSACleanup();
}
SOCKET* CreateTCPClient(unsigned short port)
{
SOCKET* sockClient = new SOCKET();
*sockClient = socket(AF_INET, SOCK_STREAM, 0);
int result = 0;
sockaddr_in addrClient;
int addrlen = sizeof(sockaddr);
if (port)
{//绑定自定义端口
addrClient.sin_family = AF_INET;
addrClient.sin_addr.s_addr = inet_addr("127.0.0.1");
addrClient.sin_port = htons(port);
result |= bind(*sockClient, (SOCKADDR*)&addrClient, sizeof(addrClient));
if (result)printf_s("CreateTCPClient error:%d\n", WSAGetLastError());
result |= getsockname(*sockClient, (sockaddr*)&addrClient, &addrlen);
if (result)printf_s("CreateTCPClient error:%d\n", WSAGetLastError());
printf_s("TCP指定端口:%s:%d\n", inet_ntoa(addrClient.sin_addr), ntohs(addrClient.sin_port));
}
else
{
printf_s("TCP端口未指定\n");
}
tcpclients->push_back(sockClient);
return sockClient;
}
SOCKET* CreateTCPServer(unsigned short port)
{
SOCKET* sockServer = new SOCKET();
*sockServer = socket(AF_INET, SOCK_STREAM, 0);
int result = 0;
sockaddr_in addrServer;
int addrlen = sizeof(sockaddr);
if (port)
{//绑定自定义端口
addrServer.sin_family = AF_INET;
addrServer.sin_addr.s_addr = inet_addr("127.0.0.1");
addrServer.sin_port = htons(port);
result |= bind(*sockServer, (SOCKADDR*)&addrServer, sizeof(addrServer));
if (result)printf_s("CreateTCPServer error:%d\n", WSAGetLastError());
result |= getsockname(*sockServer, (sockaddr*)&addrServer, &addrlen);
if (result)printf_s("CreateTCPServer error:%d\n", WSAGetLastError());
printf_s("指定端口:%s:%d\n", inet_ntoa(addrServer.sin_addr), ntohs(addrServer.sin_port));
}
else
{
printf_s("服务端未分配端口\n");
}
tcpservers->push_back(sockServer);
return sockServer;
}
void ServerListenAccept(SOCKET* server, int maxconn)
{
int i = 0;
int result = listen(*server, maxconn);
if (result)printf_s("ServerListenAccept error:%d\n", WSAGetLastError());
sockaddr_in addrClient;
int addrlen;
do
{
SOCKET client = accept(*server, 0, 0);
if (client == INVALID_SOCKET)
{
printf_s("无效socket, error:%d\n", WSAGetLastError());
continue;
}
result |= getsockname(client, (sockaddr*)&addrClient, &addrlen);
if (result)printf_s("ServerListenAccept error:%d\n", WSAGetLastError());
printf_s("连接客户端:%s:%d\n", inet_ntoa(addrClient.sin_addr), ntohs(addrClient.sin_port));
i += 1;
} while (i < maxconn);
printf_s("连接客户端数已经最大");
}
void ServerListenAcceptAsync(SOCKET* server, int maxconn)
{
int result = 0;
result = listen(*server, maxconn);
if (result)printf_s("ServerListenAcceptAsync error:%d\n", WSAGetLastError());
DWORD threadID;
HANDLE hThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)ServerAcceptThread, server, 0, &threadID);
printf_s("接受线程:%d, Thread ID:%d\n", hThread, threadID);
}
SOCKET* CreateUDPClient(unsigned short port)
{
SOCKET* sockClient = new SOCKET();
*sockClient = socket(AF_INET, SOCK_DGRAM, 0);
int result = 0;
sockaddr_in addrClient;
int addrlen = sizeof(sockaddr);
if (port)
{//绑定自定义端口
addrClient.sin_family = AF_INET;
addrClient.sin_addr.s_addr = inet_addr("127.0.0.1");
addrClient.sin_port = htons(port);
result |= bind(*sockClient, (SOCKADDR*)&addrClient, sizeof(addrClient));
if (result)printf_s("CreateUDPClient error:%d\n", WSAGetLastError());
result |= getsockname(*sockClient, (sockaddr*)&addrClient, &addrlen);
if (result)printf_s("CreateUDPClient error:%d\n", WSAGetLastError());
printf_s("UDP指定端口:%s:%d\n", inet_ntoa(addrClient.sin_addr), ntohs(addrClient.sin_port));
}
else
{
getsockname(*sockClient, (sockaddr*)&addrClient, &addrlen);
printf_s("TCP端口未指定\n");
}
udpclients->push_back(sockClient);
return sockClient;
}
int SocketIONonBlocking(SOCKET* s, int mode)
{
u_long iMode = mode;
int result = ioctlsocket(*s, FIONBIO, &iMode);
if (result)printf_s("error:%d\n", WSAGetLastError());
return result;
}
int TCPConnectToServer(SOCKET* sockClient, char* IP, unsigned short port)
{
printf_s("参数 %s:%d\n", IP, port);
int result = 0;
SOCKADDR_IN addrServer;
addrServer.sin_addr.S_un.S_addr = inet_addr(IP);
addrServer.sin_family = AF_INET;
addrServer.sin_port = htons(port);
result |= connect(*sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
if (result)printf_s("TCPConnectToServer error:%d\n", WSAGetLastError());
sockaddr_in addrClient;
int addrlen = sizeof(sockaddr);
result |= getsockname(*sockClient, (sockaddr*)&addrClient, &addrlen);
if (result)printf_s("TCPConnectToServer error:%d\n", WSAGetLastError());
printf_s("TCP连接端口:%s:%d\n", inet_ntoa(addrClient.sin_addr), ntohs(addrClient.sin_port));
return result;//一直为0代
USocket UE4TCP UDP接口
最新推荐文章于 2024-10-16 10:59:46 发布
本文档介绍了如何将USocket编译为DLL,并在UE4中使用。内容包括USocket的DLL实现、头文件接口定义、LIBUSOCKET.h作为蓝图函数库的接入API,以及LibUSocket.cpp中的虚幻DLL调用。此外,还讨论了非阻塞模式下USocket组件的使用接口。
摘要由CSDN通过智能技术生成