Making socket interface
This commit is contained in:
parent
273f88becb
commit
622a996ccf
5 changed files with 85 additions and 6 deletions
5
.clangd
Normal file
5
.clangd
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
CompileFlags:
|
||||||
|
Add: [
|
||||||
|
"--target=i686-w64-windows-gnu",
|
||||||
|
"-D_M_IX86"
|
||||||
|
]
|
||||||
14
.vscode/c_cpp_properties.json
vendored
Normal file
14
.vscode/c_cpp_properties.json
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Win32",
|
||||||
|
"compilerPath": "D:/MinGW/bin/g++.exe",
|
||||||
|
"intelliSenseMode": "windows-gcc-x64",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**"
|
||||||
|
],
|
||||||
|
"defines": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
||||||
4
src/Network/Socket.cpp
Normal file
4
src/Network/Socket.cpp
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
#include "Socket.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
Socket::Socket() : m_socket(INVALID_SOCKET) {}
|
||||||
17
src/Network/Socket.hpp
Normal file
17
src/Network/Socket.hpp
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
#pragma once
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
};
|
||||||
47
src/main.cpp
47
src/main.cpp
|
|
@ -1,14 +1,53 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <winsock.h>
|
#include <winsock.h>
|
||||||
|
|
||||||
|
#define NET_SOCK_ADDR "0.0.0.0"
|
||||||
|
#define NET_SOCK_PORT 25565
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Socket {
|
class Network {
|
||||||
public:
|
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 << "Network.Socket.winInit: Socket initialized\n";
|
||||||
|
|
||||||
|
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";
|
cout << "mcc v0.0.0\n";
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue