From 622a996ccf5fffe92590b23d766a3eaad0b7d273 Mon Sep 17 00:00:00 2001 From: dawidg81 Date: Tue, 3 Mar 2026 17:52:42 +0100 Subject: [PATCH] Making socket interface --- .clangd | 5 ++++ .vscode/c_cpp_properties.json | 14 ++++++++++ src/Network/Socket.cpp | 4 +++ src/Network/Socket.hpp | 17 ++++++++++++ src/main.cpp | 51 ++++++++++++++++++++++++++++++----- 5 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 .clangd create mode 100644 .vscode/c_cpp_properties.json create mode 100644 src/Network/Socket.cpp create mode 100644 src/Network/Socket.hpp diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..0b7bd91 --- /dev/null +++ b/.clangd @@ -0,0 +1,5 @@ +CompileFlags: + Add: [ + "--target=i686-w64-windows-gnu", + "-D_M_IX86" + ] \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..bf16f54 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,14 @@ +{ + "configurations": [ + { + "name": "Win32", + "compilerPath": "D:/MinGW/bin/g++.exe", + "intelliSenseMode": "windows-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/src/Network/Socket.cpp b/src/Network/Socket.cpp new file mode 100644 index 0000000..e49d8b7 --- /dev/null +++ b/src/Network/Socket.cpp @@ -0,0 +1,4 @@ +#include "Socket.hpp" +#include + +Socket::Socket() : m_socket(INVALID_SOCKET) {} diff --git a/src/Network/Socket.hpp b/src/Network/Socket.hpp new file mode 100644 index 0000000..3ada307 --- /dev/null +++ b/src/Network/Socket.hpp @@ -0,0 +1,17 @@ +#pragma once +#include +#include + +class Socket { +private: + SOCKET m_socket; + +public: + Socket(); + ~Socket(); + + int init(); + int bind(const std::string& address, uint16_t port); + int listen(int backlog = SOMAXCONN); + +}; diff --git a/src/main.cpp b/src/main.cpp index 51c48fc..fdaad45 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,15 +1,54 @@ #include #include +#define NET_SOCK_ADDR "0.0.0.0" +#define NET_SOCK_PORT 25565 + using namespace std; -class Socket { +class Network { public: - void winInit() { cout << "Initializing socket\n"; } -} + class Socket { + public: + int winInit() { + WSADATA wsaData; + int result = WSAStartup( MAKEWORD(2, 2), & wsaData ); + if (result != 0) cout << "Network.Socket.winInit: Error: Initialization error\n"; -int main(int argc, char *argv[]) { - cout << "mcc v0.0.0\n"; + cout << "Network.Socket.winInit: Socket initialized\n"; - return 0; + SOCKET mainSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); + if (mainSocket == INVALID_SOCKET) { + cout << "Network.Socket.winInit: Fatal error: Error creating socket: " << WSAGetLastError(); + WSACleanup(); + return 1; + } + + cout << "Network.Socket.winInit: Socket created\n"; + + sockaddr_in service; + memset( & service, 0, sizeof(service) ); + service.sin_family = AF_INET; + service.sin_addr.s_addr = inet_addr(NET_SOCK_ADDR); + service.sin_port = htons(NET_SOCK_PORT); + + cout << "Network.Socket.winInit: Socket configured\n"; + + if (bind(mainSocket, (SOCKADDR *) & service, sizeof(service)) == SOCKET_ERROR ) { + cout << "Network.Socket.winInit: Fatal error: Bind failed\n"; + closesocket(mainSocket); + return 1; + } + + cout << "Network.Socket.winInit: Socket bound to address " << NET_SOCK_ADDR << " on port " << NET_SOCK_PORT << ". Ready to listen for connections\n"; + + return 0; + } + }; +}; + +int main() { + cout << "mcc v0.0.0\n"; + + return 0; }