SFML community forums
Help => Network => Topic started by: Nexy on September 16, 2010, 05:47:13 pm
-
Hello,
I'm having problem with using packets. Can someone makes easy server and client using UDP?
// With UDP sockets
Socket.Send(Packet, Address, Port);
Socket.Receive(Packet, Address);
Example:
client sends "123" to server and server receives it,
server sends "456" to client and client receives it.
-
The use of sockets and packets is explained in the tutorials (http://www.sfml-dev.org/tutorials/1.6/).
-
The use of sockets and packets is explained in the tutorials (http://www.sfml-dev.org/tutorials/1.6/).
I know. But I can't make "server <-> client" using UDP packets.
-
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="127.0.0.1";
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;
}
this will do?
-
Look at the "sockets" sample in the SFML SDK.
-
@purkskis
It works! Thanks :)
//Topic to close