Welcome,
Guest
. Please
login
or
register
. Did you miss your
activation email?
French forum
Home
Help
Search
Login
Register
SFML community forums
»
Help
»
Network
»
Simple UDP connection that transfers message using packets
Print
Pages: [
1
]
Author
Topic: Simple UDP connection that transfers message using packets (Read 3491 times)
0 Members and 1 Guest are viewing this topic.
paupav
Full Member
Posts: 156
Simple UDP connection that transfers message using packets
«
on:
January 02, 2016, 11:36:43 pm »
So, I don't know why this isn't working. I've read some documentation and I've made this code. Both server and client are ran on the same machine.
SERVER
(click to show/hide)
///////SERVER
#include <iostream>
#include <SFML/Network.hpp>
#include <string>
using namespace std;
int main()
{
sf::UdpSocket socket;
sf::Packet packet;
sf::Time time;
sf::Clock clock;
int i = 0;
std::string hello = "Hello World!";
packet << hello;
// bind the socket to a port
socket.bind(4000);
sf::IpAddress recipient = sf::IpAddress::getPublicAddress();
unsigned short port = 5400;
while(i == 0)
{
time = clock.getElapsedTime();
if(time.asSeconds() >= 2)
{
std::cout << "Sent to: " << recipient << ":" << port << std::endl;
clock.restart();
socket.send(packet, recipient, port);
}
}
std::cout << "Adress: " << recipient << std::endl;
return 0;
}
CLIENT:
(click to show/hide)
/////////////CLIENT
#include <iostream>
#include <SFML/Network.hpp>
#include <string>
int main()
{
sf::UdpSocket socket;
sf::Packet packet;
sf::IpAddress sender;
unsigned short senderPort, socketPort;
socketPort = 5400;
socket.bind(socketPort);
std::string hello;
std::cout << sf::IpAddress::getPublicAddress() <<":"<< socketPort << std::endl;
socket.receive(packet, sender, senderPort);
packet >> hello;
std::cout << "received: " << hello << std::endl;
}
«
Last Edit: January 02, 2016, 11:48:03 pm by paupav
»
Logged
Laurent
Administrator
Hero Member
Posts: 32498
Re: Simple UDP connection that transfers message using packets
«
Reply #1 on:
January 03, 2016, 09:56:38 am »
Using your public IP, the packet will go through your router, the public internet and then back to your router and finally to your PC. So it will most likely be blocked.
If you run it locally, either use your private address or localhost.
Logged
Laurent Gomila - SFML developer
paupav
Full Member
Posts: 156
Re: Simple UDP connection that transfers message using packets
«
Reply #2 on:
January 03, 2016, 04:04:22 pm »
Thanks
Logged
Print
Pages: [
1
]
SFML community forums
»
Help
»
Network
»
Simple UDP connection that transfers message using packets
anything