Hi all,
I have run into a few obstacles while coding in SFML but this one has been driving me crazy. After many hours of reading everything I could find through google and SFML 2.1 docs, I have decided to let out a desperate plea for help.
The goal here is for the client to spam "hello_server" until it gets a response from the server in recvMsg(). However, after it runs recvMsg(), the socket Status is constantly be sf::Socket::Error. What is weird is that if I don't run the recvMsg(), it will do exactly what I intended it to. Will spam "hello_server" successfully.
On recvMsg(), I get sf::Socket::NotReady obviously because the server is not there to respond.
Here is my code:
void mainLoop()
{
port = 12345;
while(gameWindow->isOpen())
{
sf::Event event;
while(gameWindow->pollEvent(event))
{
if(event.type == sf::Event::Closed)
{
cout << "closing window\n";
gameWindow->close();
}
}
logic();
draw();
}
}
void logic()
{
playButton.update();
socket.setBlocking(false);
if(!handshakeSuccess)
{
const char msg[] = "hello_server";
int sockStatus = socket.send(msg,sizeof(msg),serverIp, 50001);
if (sockStatus == sf::Socket::Done) std::cout << "Sending Hello Server!\n";
else if(sockStatus == sf::Socket::NotReady) std::cout << "Send socket not ready\n";
else if(sockStatus == sf::Socket::Disconnected) std::cout << "Send socket disconnected\n";
else if(sockStatus == sf::Socket::Error) std::cout << "Send socket error\n";
recvMsg();
}
}
void recvMsg()
{
char inMsg[1024];
std::size_t received;
int sockStatus = socket.receive(inMsg,sizeof(inMsg),received,serverIp,port);
if(sockStatus == sf::Socket::Done)
{
std::cout << "message received : " << inMsg << endl;
handShakeSuccess = true;
}
}
Any help is appreciated. Thanks.