Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: UDP Message Receiving Woes  (Read 2553 times)

0 Members and 3 Guests are viewing this topic.

linkman2004

  • Newbie
  • *
  • Posts: 2
    • View Profile
UDP Message Receiving Woes
« on: July 11, 2010, 08:10:33 am »
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:

Code: [Select]
///////////////////////////////////////////////////
// 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:

Code: [Select]
///////////////////////////////////////////////////
// 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. :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
UDP Message Receiving Woes
« Reply #1 on: July 11, 2010, 11:58:35 am »
You're binding two different sockets to the same port on the same computer, this cannot work (you should have an error message in the standard error output).

If you test your code on the same computer, you must use two different ports.
Laurent Gomila - SFML developer

linkman2004

  • Newbie
  • *
  • Posts: 2
    • View Profile
UDP Message Receiving Woes
« Reply #2 on: July 11, 2010, 05:54:59 pm »
Well, crap... I'm a moron.  It works fine now.  Thanks for the help. :)

By the way, this is a nice little networking library; much easier than doing straight up Winsock, at the very least.