Mọi người giúp với
code này của mình chạy trên laptop ubuntu 19.10 thì ko sao nhưng bỏ sang raspberry pi raspbian thì toàn báo lỗi segment faul hoặc lúc thì broken pipe. Tìm mãi mà ko biết sai cái gì.
#include <iostream>
#include <string>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <fstream>
#include <string>
#include "pong.h"
int main(int argc, char *argv[])
{
int input = 0;
Pong pong;
char *serverIp;
int port;
char msg[6];
int client_socket;
int status;
struct hostent* host;
std::string *menu;
start:
std::cout << "1. single play\n";
std::cout << "2. multi play\n";
std::cout << "3. exit\n";
std::cin >> input;
if(input == 1)goto single;
if(input == 2)goto multi;
if(input == 3)goto exit;
gameover:
std::cout << "1. play again\n";
std::cout << "2. exit\n";
std::cin >> input;
if(input == 1)goto start;
if(input == 2)goto exit;
single:
pong.reset();
while(pong.get_winner())
{
usleep(30000);
pong.move_ball();
}
std::cout<<pong.get_winner()<<std::endl;
goto gameover;
multi:
pong.reset();
strcpy(serverIp, "192.168.2.101");
port = 12345;
host = gethostbyname(serverIp);
sockaddr_in server_address;
bzero((char*)&server_address, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr*)*host->h_addr_list));
server_address.sin_port = htons(port);
client_socket = socket(AF_INET, SOCK_STREAM, 0);
status = connect(client_socket,(sockaddr*) &server_address, sizeof(server_address));
if(status < 0)
{
std::cout<<"Error connecting to socket!"<<std::endl;
}
std::cout << "Connected to the server!" << std::endl;
while(1)
{
usleep(3000);
msg[0] = 'P';
if(msg[2] < 127)
{
if(msg[1] > msg[3] + msg[5] + BALL_SIZE)
{
msg[0] = 'U';
}
if(msg[1] + BAT_LENGTH < msg[3] + msg[5])
{
msg[0] = 'D';
}
}
send(client_socket, (char*)&msg, sizeof(msg), 0);
memset(&msg, 0, sizeof(msg));
recv(client_socket, (char*)&msg, sizeof(msg), 0);
pong.set_data(msg);
if(!pong.get_winner())
{
msg[0] = 'O';
send(client_socket, (char*)&msg, sizeof(msg), 0);
break;
}
for(int i=0; i<sizeof(msg); ++i)printf("%d ", msg[i]);
std::cout<<std::endl;
}
close(client_socket);
std::cout<<pong.get_winner()<<std::endl;
goto gameover;
exit:
std::cout<<"Exit"<<std::endl;
return 0;
}