cOMS/platform/win32/Server.h
2024-11-26 01:11:10 +01:00

59 lines
1.3 KiB
C

/**
* Jingga
*
* @copyright Jingga
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
#ifndef TOS_PLATFORM_WIN32_SERVER_H
#define TOS_PLATFORM_WIN32_SERVER_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include "../../network/SocketConnection.h"
#include "../../utils/EndianUtils.h"
#pragma comment(lib, "Ws2_32.lib")
void socket_server_udp_create(const char* hostname, SocketConnection* con) {
WSADATA wsaData;
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
return;
}
// Create socket
con->sd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (con->sd == INVALID_SOCKET) {
WSACleanup();
return;
}
// Set socket to non-blocking mode
u_long mode = 1;
if (ioctlsocket(con->sd, FIONBIO, &mode) != NO_ERROR) {
closesocket(con->sd);
WSACleanup();
return;
}
// Bind socket
con->addr.sin6_family = AF_INET6;
con->addr.sin6_addr = in6addr_any;
con->addr.sin6_port = SWAP_ENDIAN_BIG(con->port);
if (bind(con->sd, (struct sockaddr *) &con->addr, sizeof(con->addr)) == SOCKET_ERROR) {
closesocket(con->sd);
WSACleanup();
return;
}
}
#endif