From 9255de6bacd23ac073951000e222e55509edc1b5 Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Sun, 2 Jun 2024 13:52:38 +0200 Subject: [PATCH] added init_net() --- src/global.hpp | 3 +- src/input.cpp | 14 +++++++++ src/input.hpp | 3 +- src/main.cpp | 34 ++++++++++++---------- src/net.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 111 insertions(+), 20 deletions(-) diff --git a/src/global.hpp b/src/global.hpp index 2966983..7ede7ef 100644 --- a/src/global.hpp +++ b/src/global.hpp @@ -1,3 +1,4 @@ extern int board_size; extern bool ishost; -extern unsigned short port; \ No newline at end of file +extern unsigned short port; +extern char *serverIp; \ No newline at end of file diff --git a/src/input.cpp b/src/input.cpp index 5b5efff..e7127d4 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -72,4 +72,18 @@ void port_check() { } } } +} + +void ip_check() { + repeat = true; + char *serverIp; + while(repeat) { + cout << "enter host ip or hostname: "; + cin >> serverIp; + if(serverIp[0] == '\0') { + cerr << "enter something!\n"; + } else { + repeat = false; + } + } } \ No newline at end of file diff --git a/src/input.hpp b/src/input.hpp index 9f9f207..111b3c4 100644 --- a/src/input.hpp +++ b/src/input.hpp @@ -1,3 +1,4 @@ void ishost_check(); void board_size_check(); -void port_check(); \ No newline at end of file +void port_check(); +void ip_check(); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 1f01950..835b9cf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,25 +39,29 @@ cout << R"( ___ __ _______ ___ ________ ________ _____ ______ \|__| \|__|\|_______| \|__| \|__|\|__|\|_______| \|__| \|_______|\|_______| )" << '\n'; -int board_size; + int board_size; -ishost_check(); + ishost_check(); -if(ishost) { - board_size_check(); + + if(ishost) { + board_size_check(); + } else { + ip_check(); + } port_check(); -} -{ -int init_net_status = init_net(); -if(init_net_status != 0){ - cerr << "failed to initialize network\n"; - return 2; -} -} + + { + int init_net_status = init_net(); + if(init_net_status != 0){ + cerr << "failed to initialize network\n"; + return 2; + } + } -char board[board_size*board_size]; + char board[board_size*board_size]; - return 0; -} + return 0; + } diff --git a/src/net.cpp b/src/net.cpp index 3478c71..c613e9c 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -17,14 +17,85 @@ #include #include - +using std::cout; +using std::cin; +using std::cerr; int init_net() { + //buffer to send and receive messages with + char msg[1500]; if(ishost) { + //setup a socket and connection tools + sockaddr_in servAddr; + bzero((char*)&servAddr, sizeof(servAddr)); + servAddr.sin_family = AF_INET; + servAddr.sin_addr.s_addr = htonl(INADDR_ANY); + servAddr.sin_port = htons(port); + + //open stream oriented socket with internet address + //also keep track of the socket descriptor + int serverSd = socket(AF_INET, SOCK_STREAM, 0); + if(serverSd < 0) { + cerr << "Error establishing the server socket\n"; + return 2; + } + //bind the socket to its local address + int bindStatus = bind(serverSd, (struct sockaddr*) &servAddr, + sizeof(servAddr)); + if(bindStatus < 0) { + cerr << "Error binding socket to local address\n"; + return 2; + } + cout << "Waiting for a guest to connect...\n"; + //listen for up to 5 requests at a time + listen(serverSd, 5); + //receive a request from client using accept + //we need a new address to connect with the client + sockaddr_in newSockAddr; + socklen_t newSockAddrSize = sizeof(newSockAddr); + //accept, create a new socket descriptor to + //handle the new connection with client + int newSd = accept(serverSd, (sockaddr *)&newSockAddr, &newSockAddrSize); + if(newSd < 0) { + cerr << "Error accepting request from guest\n"; + return 2; + } + cout << "Connected with guest!\n"; + //lets keep track of the session time + struct timeval start1, end1; + gettimeofday(&start1, NULL); + //also keep track of the amount of data sent as well + int bytesRead, bytesWritten = 0; + + } else { + + char *serverIp; + //setup a socket and connection tools + struct hostent* host = gethostbyname(serverIp); + sockaddr_in sendSockAddr; + bzero((char*)&sendSockAddr, sizeof(sendSockAddr)); + sendSockAddr.sin_family = AF_INET; + sendSockAddr.sin_addr.s_addr = + inet_addr(inet_ntoa(*(struct in_addr*)*host->h_addr_list)); + sendSockAddr.sin_port = htons(port); + int clientSd = socket(AF_INET, SOCK_STREAM, 0); + //try to connect... + int status = connect(clientSd, + (sockaddr*) &sendSockAddr, sizeof(sendSockAddr)); + + if(status < 0) { + cout<<"Error connecting to socket!\n"; + return 2; + } + + cout << "Connected to the host!\n"; + int bytesRead, bytesWritten = 0; + struct timeval start1, end1; + gettimeofday(&start1, NULL); } + return 0; -} - +}