SFML community forums
Help => Network => Topic started by: Nexy on September 20, 2010, 09:27:25 am
-
Hello,
I've problem with server and client connection.
Server:
#include <SFML/Network.hpp>
#include <iostream>
int main()
{
const unsigned short Port = 2435;
sf::SocketUDP Server;
if (!Server.Bind(Port))
{
std::cout << " error" << std::endl;
}
sf::Packet Packet;
sf::IPAddress ClientAddress;
unsigned short ClientPort;
Server.Receive(Packet, ClientAddress, ClientPort);
int a;
Packet >> a;
std::cout << "packet : " << a << " from "<< ClientAddress <<" port: "<< ClientPort << std::endl;
a=12345;
Packet.Clear();
Packet << a;
Server.Send(Packet, ClientAddress, ClientPort);
std::cin.ignore(10000, '\n');
Server.Close();
return EXIT_SUCCESS;
}
Client:
#include <SFML/Network.hpp>
#include <iostream>
int main()
{
const unsigned short Port = 2400;
sf::SocketUDP Client;
if (!Client.Bind(Port))
{
std::cout << " error" << std::endl;
}
sf::Packet Packet;
sf::IPAddress ServerAddress;
unsigned short ServerPort=2435;
int a;
a=54321;
Packet << a;
Client.Send(Packet, ServerAddress, ServerPort);
Packet.Clear();
Client.Receive(Packet, ServerAddress, ServerPort);
Packet >> a;
std::cout << "packet : " << a << " from "<< ServerAddress <<" port: "<< ServerPort << std::endl;
std::cin.ignore(10000, '\n');
Client.Close();
return EXIT_SUCCESS;
}
When I use "IPAddress ClientAddress;" it works. But when I use:
IPAddress::GetPublicAddress();
or IPAddress Address("83.145.154.170");
it doesn't work!
This IP is from http://whatismyip.org
-
What is your network configuration ?
Have you 2 computers ?
Are you connected to the Internet ? How ?
-
Thanks for reply. I'm using one computer with Motorola modem:
(http://4.bp.blogspot.com/_29mmOeNQRzc/TGwhtlEOEQI/AAAAAAAAAMg/EyWgN43n7CE/s1600/MotorolaSURFboardSB5120cablemodem-large.jpeg)
When I use IPAddress ClientAddress;
program use "255.255.255.255", not "83.145.154.170". Why? Someone can help me?
-
When I use
IPAddress ClientAddress;
program use "255.255.255.255", not "83.145.154.170". Why? Someone can help me?
It gives an invalid address because you don't put a valid address in it ;)
IPAddress ClientAddress = "83.145.154.170";
-
When I use
IPAddress ClientAddress/ServerAddress = "83.145.154.170";
it doesn't work! It works only with: IPAddress ClientAddress/ServerAddress;
Why?
-
Using a default-constructed IPAddress gives an invalid address, but this address is also the UDP broadcast address. That's why you can reach everyone on your local network when using such an address, but not outside (broadcast messages are blocked by the router).
If you want to use a public IP address, make sure that the router doesn't block the corresponding port.
-
I used about 30 port and nothing. They are reserved or what? How I can check free ports in my router?
-
You need to open your own port, ports that are already opened are obviously already used as well.
-
Have you a firewall ?
Can you modify the ports the modem let available ?
-
Have you a firewall ?
Can you modify the ports the modem let available ?
No, I don't have firewall and I don't know why it doesn't work.
-
So, can you try the SFML network samples ? Is this working ?
-
So, can you try the SFML network samples ? Is this working ?
No :(
It's working only with "127.0.0.1", not "83.145.154.170".
-
is the modem just a modem or is it a router?
your pc behind the modem has a private or public ip?
thanks..
-
your pc behind the modem has a private or public ip?
Under Windows : Execute : cmd
then ipconfig
Under Linux : ifconfig in a terminal ;)