Added Socket.cpp
This commit is contained in:
parent
74ddf46078
commit
8ca744f936
1 changed files with 45 additions and 0 deletions
45
src/Network/Socket.cpp
Normal file
45
src/Network/Socket.cpp
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
#include "Socket.hpp"
|
||||||
|
|
||||||
|
class Socket
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
Logger log;
|
||||||
|
|
||||||
|
public:
|
||||||
|
int winInit()
|
||||||
|
{
|
||||||
|
WSADATA wsaData;
|
||||||
|
int result = WSAStartup( MAKEWORD(2, 2), & wsaData );
|
||||||
|
if (result != 0) log.err("Network.Socket.winInit: Initialization error");
|
||||||
|
|
||||||
|
log.info("Network.Socket.winInit: Socket initialized");
|
||||||
|
|
||||||
|
SOCKET mainSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
|
||||||
|
if (mainSocket == INVALID_SOCKET) {
|
||||||
|
log.err("Network.Socket.winInit: Fatal error: Error creating socket");
|
||||||
|
log.err(WSAGetLastError());
|
||||||
|
WSACleanup();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("Network.Socket.winInit: Socket created");
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
log.info("Network.Socket.winInit: Socket configured");
|
||||||
|
|
||||||
|
if (bind(mainSocket, (SOCKADDR *) & service, sizeof(service)) == SOCKET_ERROR ) {
|
||||||
|
log.err("Network.Socket.winInit: Fatal error: Bind failed");
|
||||||
|
closesocket(mainSocket);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("Network.Socket.winInit: Socket bound to address " + NET_SOCK_ADDR + " on port " + string to_string(NET_SOCK_PORT) + ". Ready to listen for connections");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue