mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2025-07-01 23:46:59 +00:00
134 lines
3.6 KiB
C++
134 lines
3.6 KiB
C++
////
|
|
//// Created by Anonymous275 on 3/3/2020.
|
|
////
|
|
|
|
#include "enet.h"
|
|
#include <WinSock2.h>
|
|
#include <Windows.h>
|
|
#include <WS2tcpip.h>
|
|
#include <cstdio>
|
|
#include <string>
|
|
#define DEFAULT_BUFLEN 64000
|
|
#define DEFAULT_PORT "4444"
|
|
|
|
std::string TCPData;
|
|
std::string RUDPData;
|
|
std::string TCPToSend;
|
|
std::string RUDPToSend;
|
|
|
|
void TCPServerThread(){
|
|
WSADATA wsaData;
|
|
int iResult;
|
|
|
|
SOCKET ListenSocket = INVALID_SOCKET;
|
|
SOCKET ClientSocket = INVALID_SOCKET;
|
|
|
|
struct addrinfo *result = NULL;
|
|
struct addrinfo hints;
|
|
|
|
int iSendResult;
|
|
char recvbuf[DEFAULT_BUFLEN];
|
|
int recvbuflen = DEFAULT_BUFLEN;
|
|
|
|
// Initialize Winsock
|
|
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
|
|
if (iResult != 0) {
|
|
printf("WSAStartup failed with error: %d\n", iResult);
|
|
}
|
|
|
|
ZeroMemory(&hints, sizeof(hints));
|
|
hints.ai_family = AF_INET;
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
// Resolve the server address and port
|
|
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
|
|
if ( iResult != 0 ) {
|
|
printf("getaddrinfo failed with error: %d\n", iResult);
|
|
WSACleanup();
|
|
}
|
|
|
|
// Create a socket for connecting to server
|
|
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
|
if (ListenSocket == INVALID_SOCKET) {
|
|
printf("socket failed with error: %d\n", WSAGetLastError());
|
|
freeaddrinfo(result);
|
|
WSACleanup();
|
|
}
|
|
|
|
// Setup the TCP listening socket
|
|
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
|
|
if (iResult == SOCKET_ERROR) {
|
|
printf("bind failed with error: %d\n", WSAGetLastError());
|
|
freeaddrinfo(result);
|
|
closesocket(ListenSocket);
|
|
WSACleanup();
|
|
}
|
|
|
|
freeaddrinfo(result);
|
|
|
|
iResult = listen(ListenSocket, SOMAXCONN);
|
|
if (iResult == SOCKET_ERROR) {
|
|
printf("listen failed with error: %d\n", WSAGetLastError());
|
|
closesocket(ListenSocket);
|
|
WSACleanup();
|
|
}
|
|
ClientSocket = accept(ListenSocket, NULL, NULL);
|
|
if (ClientSocket == INVALID_SOCKET) {
|
|
printf("accept failed with error: %d\n", WSAGetLastError());
|
|
closesocket(ListenSocket);
|
|
WSACleanup();
|
|
}
|
|
closesocket(ListenSocket);
|
|
|
|
do {
|
|
if(!RUDPData.empty()){
|
|
iSendResult = send( ClientSocket, RUDPData.c_str(), int(RUDPData.length())+1, 0);
|
|
if (iSendResult == SOCKET_ERROR) {
|
|
printf("send failed with error: %d\n", WSAGetLastError());
|
|
closesocket(ClientSocket);
|
|
WSACleanup();
|
|
}else{
|
|
RUDPData.clear();
|
|
printf("Bytes sent: %d\n", iSendResult);
|
|
}
|
|
}
|
|
|
|
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
|
|
if (iResult > 0) {
|
|
printf("Bytes received: %d\n", iResult);
|
|
printf("Data : %s\n",recvbuf);
|
|
RUDPToSend = recvbuf;
|
|
}
|
|
|
|
else if (iResult == 0)
|
|
printf("Connection closing...\n");
|
|
else {
|
|
printf("recv failed with error: %d\n", WSAGetLastError());
|
|
closesocket(ClientSocket);
|
|
WSACleanup();
|
|
}
|
|
|
|
} while (iResult > 0);
|
|
|
|
iResult = shutdown(ClientSocket, SD_SEND);
|
|
if (iResult == SOCKET_ERROR) {
|
|
printf("shutdown failed with error: %d\n", WSAGetLastError());
|
|
closesocket(ClientSocket);
|
|
WSACleanup();
|
|
}
|
|
|
|
closesocket(ClientSocket);
|
|
WSACleanup();
|
|
}
|
|
|
|
void RUDPClientThread(){
|
|
|
|
}
|
|
|
|
|
|
void Start(){
|
|
TCPServerThread();
|
|
RUDPClientThread();
|
|
} |