in the first SFML tutorial where you send a "char Buffer[]="i'm client" it works perfectly with both UDP and TCP, but when i try to use packets it doesn't work.
You should use the sample directly, and only replace the char[] buffer with a packet -- rather than rewriting it entirely. That would seriously limit the number of potential mistakes.
its not working, there is something wrong either with my PC or the class packet.
i'm getting the same Socket state which is 3 (error).
when i do this i get Socket state 3 (error)
Server:
SendPacket << message;
Client:
ReceivePacket >> message;
However when i do this i get Socket state 1 which is (Done) but the data is corrupted.
SendPacket >>message;
Client:
ReceivePacket << message;
Please try take my code and run it on your machine and see if you get the same results, maybe i have a virus or something.
here is the code:
Client
#include <SFML\Network.hpp>
#include <iostream>
void RunClient(unsigned short Port)
{
sf::SocketUDP Client;
sf::IPAddress Address;
sf::Packet ReceivePacket;
if (!Client.Bind(Port))
{
std::cout<<"Could not listen";
}
while (true)
{
sf::Socket::Status status = Client.Receive(ReceivePacket, Address, Port);
switch(status)
{
case sf::Socket::Disconnected:
std::cout << "Disconnected" << std::endl;
break;
case sf::Socket::Error:
std::cout << "Socket Status:" << status << std::endl;
break;
case sf::Socket::Done:
{
std::string message;
ReceivePacket >> message;
std::cout << "IP: " << Address << std::endl << "Port: "<< Port << std::endl << "Message: " << message<< std::endl;
break;
}
case sf::Socket::NotReady:
std::cout << "NotReady" << std::endl;
break;
}
}
Client.Close();
}
int main()
{
const unsigned short Port = 4000;
RunClient(Port);
system("pause");
return 0;
}
Server:
#include <SFML\Network.hpp>
#include <iostream>
void RunServer(unsigned short Port)
{
sf::SocketUDP Server;
sf::IPAddress Address;
sf::Packet SendPacket;
std::cout << "type the ip address you want to send data to" << std::endl;
std::cin >> Address;
if (Address.IsValid())
std::cout << "Valid" << std::endl;
std::cout << "Press Enter to send packet." <<std::endl;
getchar();
char input = getchar();
std::string message = "PLEASE WORK!!!";
SendPacket << message;
while(input == 10)
{
if (Server.Send(SendPacket, Address , Port) != sf::Socket::Done)
{
std::cout<<"Could not send data";
}
input = getchar();
if(input == 'z')
break;
}
Server.Close();
}
int main()
{
const unsigned short Port = 4000;
RunServer(Port);
system("pause");
return 0;
}