1
Network / Re: Trying to communicate with packages
« on: June 09, 2015, 07:10:11 pm »
Unfortunately it's going to be really hard to both master network practices and deliver this project in time (as this is supposed to be done by Friday), but I followed your advice and tried to do it less complicated - instead of sending the whole "Carta" class, I'm sending only its values:
The Client part is working just fine, but the Server's isn't. I run the Server and it's listener keep waiting until a Client connect - when it does, the project running the Server crashes and only the Client keeps running (and it obviously can't read the cards because it didn't receive any). Where exactly am I doing wrong?
I understand the problem here is my lack of knowledge and I agree with you. I am reading everything I can about SFML network in the few free time I have, although I face many difficulties understanding this part. The main problem here is the deadline (this Friday) to deliver this, that's why I'm comming to you with these questions. Sorry for any inconvenience.
int main()
{
Servidor s;
sf::Packet j0;
if (eServer == true) // there is a boolean here to set if it's the server or a cliente
{
sf::TcpListener listener;
listener.listen(2000);
listener.accept(socket);
s.novoTurno();
// all the values below represent 3 cards with 4 variables each
j0 << s.getJogador(0).getMaoCartas(0).getNumero() << s.getJogador(0).getMaoCartas(0).getNaipe() << s.getJogador(0).getMaoCartas(0).getValor() << s.getJogador(0).getMaoCartas(0).getImagem() << s.getJogador(0).getMaoCartas(1).getNumero() << s.getJogador(0).getMaoCartas(1).getNaipe() << s.getJogador(0).getMaoCartas(1).getValor() << s.getJogador(0).getMaoCartas(1).getImagem() << s.getJogador(0).getMaoCartas(2).getNumero() << s.getJogador(0).getMaoCartas(2).getNaipe() << s.getJogador(0).getMaoCartas(2).getValor() << s.getJogador(0).getMaoCartas(2).getImagem();
socket.send(j0); // this is where the cards are supposed to be sent
while (servLoop == true)
{
}
}
else
{
socket.connect(sf::IpAddress::getLocalAddress(), 2000);
Controlador c(width, height, title); // client's class
Carta c1, c2, c3;
int nm1, np1, vl1,
nm2, np2, vl2,
nm3, np3, vl3;
std::string im1, im2, im3;
socket.receive(j0);
j0 >> nm1 >> np1 >> vl1 >> im1 >> nm2 >> np2 >> vl2 >> im2 >> nm3 >> np3 >> vl3 >> im3;
c1.setNumero(nm1);
c1.setNaipe(np1);
c1.setValor(vl1);
c1.setImagem(im1);
c2.setNumero(nm2);
c2.setNaipe(np2);
c2.setValor(vl2);
c2.setImagem(im2);
c3.setNumero(nm3);
c3.setNaipe(np3);
c3.setValor(vl3);
c3.setImagem(im3);
c.getJogador().setMaoCartas(0, c1);
c.getJogador().setMaoCartas(1, c2);
c.getJogador().setMaoCartas(2, c3);
c.defineCartas(); // cards received and stored in client's class
while (c.getWindow().isOpen())
{
sf::Event event;
while (c.getWindow().pollEvent(event))
{
if (event.type == sf::Event::Closed){ c.getWindow().close(); }
eventosMouse(event);
c.getWindow().clear();
c.abreJogo();
c.getWindow().display();
}
}
}
return 0;
}
{
Servidor s;
sf::Packet j0;
if (eServer == true) // there is a boolean here to set if it's the server or a cliente
{
sf::TcpListener listener;
listener.listen(2000);
listener.accept(socket);
s.novoTurno();
// all the values below represent 3 cards with 4 variables each
j0 << s.getJogador(0).getMaoCartas(0).getNumero() << s.getJogador(0).getMaoCartas(0).getNaipe() << s.getJogador(0).getMaoCartas(0).getValor() << s.getJogador(0).getMaoCartas(0).getImagem() << s.getJogador(0).getMaoCartas(1).getNumero() << s.getJogador(0).getMaoCartas(1).getNaipe() << s.getJogador(0).getMaoCartas(1).getValor() << s.getJogador(0).getMaoCartas(1).getImagem() << s.getJogador(0).getMaoCartas(2).getNumero() << s.getJogador(0).getMaoCartas(2).getNaipe() << s.getJogador(0).getMaoCartas(2).getValor() << s.getJogador(0).getMaoCartas(2).getImagem();
socket.send(j0); // this is where the cards are supposed to be sent
while (servLoop == true)
{
}
}
else
{
socket.connect(sf::IpAddress::getLocalAddress(), 2000);
Controlador c(width, height, title); // client's class
Carta c1, c2, c3;
int nm1, np1, vl1,
nm2, np2, vl2,
nm3, np3, vl3;
std::string im1, im2, im3;
socket.receive(j0);
j0 >> nm1 >> np1 >> vl1 >> im1 >> nm2 >> np2 >> vl2 >> im2 >> nm3 >> np3 >> vl3 >> im3;
c1.setNumero(nm1);
c1.setNaipe(np1);
c1.setValor(vl1);
c1.setImagem(im1);
c2.setNumero(nm2);
c2.setNaipe(np2);
c2.setValor(vl2);
c2.setImagem(im2);
c3.setNumero(nm3);
c3.setNaipe(np3);
c3.setValor(vl3);
c3.setImagem(im3);
c.getJogador().setMaoCartas(0, c1);
c.getJogador().setMaoCartas(1, c2);
c.getJogador().setMaoCartas(2, c3);
c.defineCartas(); // cards received and stored in client's class
while (c.getWindow().isOpen())
{
sf::Event event;
while (c.getWindow().pollEvent(event))
{
if (event.type == sf::Event::Closed){ c.getWindow().close(); }
eventosMouse(event);
c.getWindow().clear();
c.abreJogo();
c.getWindow().display();
}
}
}
return 0;
}
The Client part is working just fine, but the Server's isn't. I run the Server and it's listener keep waiting until a Client connect - when it does, the project running the Server crashes and only the Client keeps running (and it obviously can't read the cards because it didn't receive any). Where exactly am I doing wrong?
I understand the problem here is my lack of knowledge and I agree with you. I am reading everything I can about SFML network in the few free time I have, although I face many difficulties understanding this part. The main problem here is the deadline (this Friday) to deliver this, that's why I'm comming to you with these questions. Sorry for any inconvenience.