Je suis entrain de développer un petit
serveur de chat et je veux qu'il fonctionne en sur internet, pas
seulement en réseau local. Présentement, mon client envoi une demande de login à mon serveur,
et mon serveur reçoit belle et bien la demande. Mais lorsque mon serveur envoi quelque chose au client,
le client ne re^coit rien. Voici le code serveur
// ***************************************************************************************
#include <iostream>
// ***************************************************************************************
#include "include\tserver.h"
#include "include\tloginattemptinguser.h"
#include "include\tloginqueueduser.h"
// ***************************************************************************************
#include <sfml/network.hpp>
// ***************************************************************************************
using namespace std;
using namespace sf;
// ***************************************************************************************
TServer Server;
// ***************************************************************************************
void NetworkListener(void *Data)
{
SocketUDP Receiver;
Packet m;
IPAddress Add;
unsigned short p = 14532;
Receiver.Bind(p);
int i;
while (true)
{
Receiver.Receive(m, Add, p);
m >> i;
if(i == 1)//USER LOGIN
{
string UserName;
string Password;
m >> UserName >> Password;
Server.Add_UserToLoginAttempingUsersQueue(new TLoginAttemptingUser(Add, p, UserName, Password));
}
}
Receiver.Close();
}
Thread NetworkListener_THREAD(&NetworkListener);
// ***************************************************************************************
void SendPacket(Packet p, IPAddress ip, unsigned short port)
{
SocketUDP Sender;
Sender.Send(p, ip, port);
Sender.Close();
}
// ***************************************************************************************
void ManageLoginAttempts(void *Data)
{
TPureUser *u;
while (true)
{
u = Server.Get_LoginAttemptingUserInQueue();
if (u != NULL)
{
if (u->Get_UserName() == "lol" && u->Get_Password() == "lol")//GOOD LOGIN
{
u = new TLoginQueuedUser(u->Get_UserIpAddress(), u->Get_SendingPort(), 1);
if (Server.Count_Users() < 16 && Server.Count_LoginUsers() == 0)
{
//TRUE LOGIN (u.Get_IDAccount())
Server.Add_User(u);
}
else if(Server.Count_LoginUsers() < 64)
{
Server.Add_UserToLoginQueue(u);
}
else
{
//SERVER IS FULL(WITH LOGIN QUEUE)
}
}
else
{
Packet p;
p << 2;//WRONG LOGIN
cout << "Sending to " << u->Get_UserIpAddress().ToString() << ":" << u->Get_SendingPort() << endl;
SendPacket(p, u->Get_UserIpAddress(), u->Get_SendingPort());
}
}
}
}
Thread ManageLoginAttempts_THREAD(&ManageLoginAttempts);
// ***************************************************************************************
void ManageLoginQueue(void *Data)
{
TPureUser *u;
while(true)
{
if (Server.Count_Users() < 16)
{
u = Server.Get_LoginUserInQueue();
if (u != NULL)
{
//TRUE LOGIN (u.Get_IDAccount())
Server.Add_User(u);
}
}
}
}
Thread ManageLoginQueue_THREAD(&ManageLoginQueue);
// ***************************************************************************************
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()
{
Server.Set_MaximumUsersInLoginQueueNumber(64);
Server.Set_MaximumUsersNumber(16);
NetworkListener_THREAD.Launch();
ManageLoginAttempts_THREAD.Launch();
ManageLoginQueue_THREAD.Launch();
string s;
while (getline(cin, s) && ToLower(s) != "stop")
{
}
NetworkListener_THREAD.Terminate();
ManageLoginAttempts_THREAD.Terminate();
ManageLoginQueue_THREAD.Terminate();
return 0;
}
// ***************************************************************************************
Et voici le client:
#include <iostream>
#include <sfml/network.hpp>
using namespace std;
using namespace sf;
// ***************************************************************************************
unsigned short SP = 14532;
// ***************************************************************************************
void NetworkListener(void *Data)
{
SocketUDP Receiver;
Packet m;
IPAddress Add;
unsigned short p = SP;
Receiver.Bind(p);
int i;
while (true)
{
Receiver.Receive(m, Add, p);
m >> i;
if(i == 2)//WRONG LOGIN
{
cout << "Wrong login." << 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;
}
// ***************************************************************************************
void ConnectToServer(string i, string u, string p)//USER CLASS
{
SocketUDP Sender;
Packet o;
o << 1 << u << p;
Sender.Send(o, i, SP);
Sender.Close();
}
// ***************************************************************************************
int main()
{
string i;
string u;
string p;
cout << "IPAddress : ";
cin >> i;
cout << "UserName : ";
cin >> u;
cout << "Password : ";
cin >> p;
NetworkListener_THREAD.Launch();
ConnectToServer(i, u, p);
string s;
while (getline(cin, s) && ToLower(s) != "stop")
{
}
NetworkListener_THREAD.Terminate();
return 0;
}
Quand je fais un "cout" au moment ou
le server doit envoyer wrong login, tous me semble parfait,
donc je suspect que c'est mon client qui est défectueux.