seg fault help

This commit is contained in:
PoliEcho 2024-06-02 17:35:38 +02:00
parent 9255de6bac
commit b37dc50a98
7 changed files with 124 additions and 18 deletions

0
src/gameplay.cpp Normal file
View File

0
src/gameplay.hpp Normal file
View File

View File

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

View File

@ -6,6 +6,7 @@ using std::cout;
using std::cin;
using std::cerr;
int length;
int board_size;
bool ishost;
unsigned short port;
@ -53,6 +54,31 @@ while (repeat) {
}
}
void length_check() {
repeat = true;
while (repeat) {
cout << "\nplease enter needed length(e.g. how many symbols need to be next to each other to get point): ";
cin >> length;
if(cin.fail()) {
cerr << "integer please\n";
cin.clear();
cin.ignore(256,'\n');
} else {
if(length > 0) {
if(length > board_size) {
cerr << "length cannot be biger than board size";
} else {
repeat = false;
}
} else {
cerr << "positive integer please\n";
}
}
}
}
void port_check() {
repeat = true;
@ -65,7 +91,7 @@ void port_check() {
cin.clear();
cin.ignore(256,'\n');
} else {
if(board_size > 0) {
if(port > 0) {
repeat = false;
} else {
cerr << "positive integer please\n";
@ -76,14 +102,17 @@ void port_check() {
void ip_check() {
repeat = true;
char *serverIp;
std::string serverIp_temp;
while(repeat) {
cout << "enter host ip or hostname: ";
cin >> serverIp;
if(serverIp[0] == '\0') {
cin >> serverIp_temp;
if(serverIp_temp == "") {
cerr << "enter something!\n";
} else {
repeat = false;
const char *serverIp = serverIp_temp.c_str();
}
}
}

View File

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

View File

@ -1,3 +1,4 @@
#include <cstddef>
#include<iostream>
#include"global.hpp"
#include"net.hpp"
@ -10,16 +11,16 @@ using std::cerr;
int main() {
cout << R"( ___ __ _______ ___ ________ ________ _____ ______ _______ _________ ________
cout << R"( ___ __ _______ ___ ________ ________ _____ ______ _______ _________ ________
|\ \ |\ \|\ ___ \ |\ \ |\ ____\|\ __ \|\ _ \ _ \|\ ___ \ |\___ ___\\ __ \
\ \ \ \ \ \ \ __/|\ \ \ \ \ \___|\ \ \|\ \ \ \\\__\ \ \ \ __/| \|___ \ \_\ \ \|\ \
\ \ \ __\ \ \ \ \_|/_\ \ \ \ \ \ \ \ \\\ \ \ \\|__| \ \ \ \_|/__ \ \ \ \ \ \\\ \
\ \ \|\__\_\ \ \ \_|\ \ \ \____\ \ \____\ \ \\\ \ \ \ \ \ \ \ \_|\ \ \ \ \ \ \ \\\ \
\ \____________\ \_______\ \_______\ \_______\ \_______\ \__\ \ \__\ \_______\ \ \__\ \ \_______\
\|____________|\|_______|\|_______|\|_______|\|_______|\|__| \|__|\|_______| \|__| \|_______|
________ ___ ___ _______ ________ _________ ___ ___ _______ ________ _______ _________ ___ __ ________ ________ ___ __
|\ __ \|\ \ / /|\ ___ \ |\ __ \ |\___ ___\\ \|\ \|\ ___ \ |\ ___ \|\ ___ \|\___ ___\\ \ |\ \|\ __ \|\ __ \|\ \|\ \
\ \ \|\ \ \ \ / / | \ __/|\ \ \|\ \ \|___ \ \_\ \ \\\ \ \ __/| \ \ \\ \ \ \ __/\|___ \ \_\ \ \ \ \ \ \ \|\ \ \ \|\ \ \ \/ /|_
@ -27,9 +28,9 @@ cout << R"( ___ __ _______ ___ ________ ________ _____ ______
\ \ \\\ \ \ / / \ \ \_|\ \ \ \\ \| \ \ \ \ \ \ \ \ \ \_|\ \ \ \ \\ \ \ \ \_|\ \ \ \ \ \ \ \|\__\_\ \ \ \\\ \ \ \\ \\ \ \\ \ \
\ \_______\ \__/ / \ \_______\ \__\\ _\ \ \__\ \ \__\ \__\ \_______\ \ \__\\ \__\ \_______\ \ \__\ \ \____________\ \_______\ \__\\ _\\ \__\\ \__\
\|_______|\|__|/ \|_______|\|__|\|__| \|__| \|__|\|__|\|_______| \|__| \|__|\|_______| \|__| \|____________|\|_______|\|__|\|__|\|__| \|__|
_________ ___ ________ _________ ________ ________ _________ ________ _______
|\___ ___\\ \|\ ____\ |\___ ___\\ __ \|\ ____\ |\___ ___\\ __ \|\ ___ \
\|___ \ \_\ \ \ \ \___| ___________\|___ \ \_\ \ \|\ \ \ \___| ___________\|___ \ \_\ \ \|\ \ \ __/|
@ -53,15 +54,27 @@ cout << R"( ___ __ _______ ___ ________ ________ _____ ______
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];
int ncord = 0;
for (int i = 0; i < sizeof(board); i++) {
board[ncord] = '_';
ncord++;
}
if(ishost) {
}
return 0;
}

View File

@ -20,12 +20,21 @@
using std::cout;
using std::cin;
using std::cerr;
using std::string;
void handshake_failed() {
cerr << "handshake failed";
exit(3);
}
int init_net() {
//buffer to send and receive messages with
char msg[1500];
if(ishost) {
//code for host
//setup a socket and connection tools
sockaddr_in servAddr;
bzero((char*)&servAddr, sizeof(servAddr));
@ -68,7 +77,36 @@ int init_net() {
//also keep track of the amount of data sent as well
int bytesRead, bytesWritten = 0;
} else {
memset(&msg, 0, sizeof(msg));//clear the buffer
bytesRead += recv(newSd, (char*)&msg, sizeof(msg), 0);
if( strcmp(msg, "concheck") != 0) {
handshake_failed();
}
string data = std::to_string(board_size);
memset(&msg, 0, sizeof(msg)); //clear the buffer
strcpy(msg, data.c_str());
bytesWritten += send(newSd, (char*)&msg, strlen(msg), 0);
memset(&msg, 0, sizeof(msg));//clear the buffer
bytesRead += recv(newSd, (char*)&msg, sizeof(msg), 0);
if(board_size != std::stoi( msg )) {
handshake_failed();
}
data = std::to_string(length);
memset(&msg, 0, sizeof(msg)); //clear the buffer
strcpy(msg, data.c_str());
bytesWritten += send(newSd, (char*)&msg, strlen(msg), 0);
memset(&msg, 0, sizeof(msg));//clear the buffer
bytesRead += recv(newSd, (char*)&msg, sizeof(msg), 0);
if(length != std::stoi( msg )) {
handshake_failed();
}
} else {
// code for client
char *serverIp;
//setup a socket and connection tools
@ -94,6 +132,30 @@ int init_net() {
struct timeval start1, end1;
gettimeofday(&start1, NULL);
string data;
data = "concheck";
memset(&msg, 0, sizeof(msg));//clear the buffer
strcpy(msg, data.c_str());
bytesWritten += send(clientSd, (char*)&msg, strlen(msg), 0);
memset(&msg, 0, sizeof(msg));//clear the buffer
bytesRead += recv(clientSd, (char*)&msg, sizeof(msg), 0);
board_size = std::stoi( msg );
data = std::to_string(board_size);
memset(&msg, 0, sizeof(msg));//clear the buffer
strcpy(msg, data.c_str());
bytesWritten += send(clientSd, (char*)&msg, strlen(msg), 0);
memset(&msg, 0, sizeof(msg));//clear the buffer
bytesRead += recv(clientSd, (char*)&msg, sizeof(msg), 0);
length = std::stoi( msg );
data = std::to_string(length);
memset(&msg, 0, sizeof(msg));//clear the buffer
strcpy(msg, data.c_str());
bytesWritten += send(clientSd, (char*)&msg, strlen(msg), 0);
}
return 0;