I'm about to rip my hair out because of problems with my network, having spent the better part of my evening getting absolutely nowhere. So, I have come here for help.
I'm currently working on a basic UDP client/server setup. Clients send a message to the server, the server acknowledges them with a message of its own, everything is hunky dory... at least, that's the way it should be. The client sends the message fine, the server
receives it fine, but the client isn't receiving the server's message. Nothing is erroring out, so I don't know where the problem is.
Server code:
///////////////////////////////////////////////////
// Server
///////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <SFML/network.hpp>
using namespace std;
int main()
{
// Socket
sf::SocketUDP mySock;
// Bind the socket
mySock.Bind(3333);
// Check for messages
char buffer[128];
size_t received;
sf::IPAddress sender;
unsigned short port;
if (mySock.Receive(buffer, sizeof(buffer), received, sender, port) == sf::Socket::Done())
{
// Check what the message says
string message = buffer;
if (message == "join")
{
// A client is attempting to join
cout << "A client is attempting to join" << endl;
// Send a reply to the sender of this message
char reply[] = "accepted";
mySock.Send(reply, sizeof(reply), sender, port);
}
}
return 0;
}
Client code:
///////////////////////////////////////////////////
// Client
///////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <SFML/network.hpp>
using namespace std;
int main()
{
// Socket
sf::SocketUDP mySock;
// Bind the socket
mySock.Bind(3333);
// Send a message to the server
char request[] = "join";
mySock.Send(request, sizeof(request), "127.0.0.1", 3333);
// Check for messages
char buffer[128];
size_t received;
sf::IPAddress sender;
unsigned short port;
if (mySock.Receive(buffer, sizeof(buffer), received, sender, port) == sf::Socket::Done())
{
// Check what the message says
string message = buffer;
if (message == "join")
{
// Accepted
cout << "Accepted" << endl;
}
}
return 0;
}
Note that I don't have access to my main computer at the moment, so this is as accurate a recreation of the code that I could create from memory. I'll put up the real deal later if necessary.
I'd be really thankful if somebody could point me in the right direction with this.