Mon serveur reçoit le packet envoyé par mon client mais le client ne reçoit pas le packet envoyer du serveur.
- J'ai fais le port fowarding vers mon pc pour le port 14532.
- Mon serveur et mon client son exécuter sur la même machine.
- Mon ip local est 192.168.0.102
Mon but est de faire un petit chat en ligne. Normalement, je devrais utiliser TCP mais ce n'est que pour la pratique.
Voici le code serveur:
// ***************************************************************************************
#include <iostream>
// ***************************************************************************************
#include <sfml/network.hpp>
// ***************************************************************************************
using namespace std;
using namespace sf;
// ***************************************************************************************
void NetworkListener(void *Data)
{
SocketUDP Receiver;
Packet m;
IPAddress Address;
unsigned short port = 14532;
Receiver.Bind(port);
int i;
while (true)
{
Receiver.Receive(m, Address, port);
m >> i;
if(i == 104)
{
cout << "Received from client" << endl;
SocketUDP Sender;
Packet packet;
packet << 105;
Sender.Send(packet, Address, port);
Sender.Close();
}
}
Receiver.Close();
}
Thread NetworkListener_THREAD(&NetworkListener);
// ***************************************************************************************
string ToLower(string s)
{
unsigned int a = s.size();
for (unsigned int i = 0; i < a; ++i)
{
s[i] = tolower(s[i]);
}
return s;
}
// ***************************************************************************************
int main()
{
NetworkListener_THREAD.Launch();
string s;
while (getline(cin, s) && ToLower(s) != "/stop")
{
}
NetworkListener_THREAD.Terminate();
return 0;
}
// ***************************************************************************************
Voici le code client:
// ***************************************************************************************
#include <iostream>
// ***************************************************************************************
#include <sfml/network.hpp>
// ***************************************************************************************
using namespace std;
using namespace sf;
// ***************************************************************************************
unsigned short port = 14532;
// ***************************************************************************************
void NetworkListener(void *Data)
{
SocketUDP Receiver;
Packet m;
IPAddress Add;
unsigned short p = port;
Receiver.Bind(p);
int i;
while (true)
{
Receiver.Receive(m, Add, p);
m >> i;
if(i == 105)
{
cout << "Received from server" << endl;
}
}
Receiver.Close();
}
Thread NetworkListener_THREAD(&NetworkListener);
// ***************************************************************************************
string ToLower(string s)
{
unsigned int a = s.size();
for (unsigned int i = 0; i < a; ++i)
{
s[i] = tolower(s[i]);
}
return s;
}
// ***************************************************************************************
int main()
{
NetworkListener_THREAD.Launch();
SocketUDP Sender;
Packet packet;
packet << 104;
Sender.Send(packet, "192.168.0.102", port);
Sender.Close();
string s;
while (getline(cin, s) && ToLower(s) != "/stop")
{
}
NetworkListener_THREAD.Terminate();
return 0;
}