added init_net()

This commit is contained in:
PoliEcho 2024-06-02 13:52:38 +02:00
parent 5ef5d6ffa4
commit 9255de6bac
5 changed files with 111 additions and 20 deletions

View File

@ -1,3 +1,4 @@
extern int board_size;
extern bool ishost;
extern unsigned short port;
extern unsigned short port;
extern char *serverIp;

View File

@ -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;
}
}
}

View File

@ -1,3 +1,4 @@
void ishost_check();
void board_size_check();
void port_check();
void port_check();
void ip_check();

View File

@ -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;
}

View File

@ -17,14 +17,85 @@
#include <fcntl.h>
#include <fstream>
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;
}
}