Hello i created simple chat using UDP, i have 2 applications : Client and Server. Only difference between them are another port to - send and - receive. When i run 2 applications, and I want to send message i get a message : Unsuccesfull data send like I write in code ;/ Why this code doesn't work ?
CLIENT :
#include "stdafx.h"
#include <SFML/Network.hpp>
#include <iostream>
using namespace std;
sf::Packet packet_send;
sf::Packet packet_receive;
char data_send[100];
char data_receive[100];
sf::UdpSocket socketUDP;
sf::IpAddress local_host = "127.0.0.1";
unsigned short port_receive = 5000;
unsigned short port_send = 5001;
void receive_data()
{
bool wyjscie = false;
while (wyjscie != true)
{
if (socketUDP.receive(packet_receive, local_host, port_receive) == sf::Socket::Done)
{
cout << "Receive data" << endl;
packet_receive >> data_receive;
cout << data_receive << endl;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Client" << endl;
if (socketUDP.bind(port_receive) == sf::Socket::Done)
{
cout << "Binded for port : " << port_receive << endl;
}
sf::Thread receive_data(&receive_data);
receive_data.launch();
while (true)
{
cin.getline(data_receive, 100);
packet_send.clear();
packet_send << data_send;
if (socketUDP.send(packet_send, local_host, port_send) == sf::Socket::Done)
{
cout << "Succesfull send data" << endl;
}
else
{
cout << "Unsuccesfull data send" << endl;
}
}
return 0;
}
SERVRER:
#include "stdafx.h"
#include <SFML/Network.hpp>
#include <iostream>
using namespace std;
sf::Packet packet_send;
sf::Packet packet_receive;
char data_send[100];
char data_receive[100];
sf::UdpSocket socketUDP;
sf::IpAddress local_host = "127.0.0.1";
unsigned short port_receive = 5001;
unsigned short port_send = 5000;
void receive_data()
{
bool wyjscie = false;
while (wyjscie != true)
{
if (socketUDP.receive(packet_receive, local_host, port_receive) == sf::Socket::Done)
{
cout << "Receive data" << endl;
packet_receive >> data_receive;
cout << data_receive << endl;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Server" << endl;
if ( socketUDP.bind(port_receive)== sf::Socket::Done )
{
cout << "Binded for port : " << port_receive << endl;
}
sf::Thread receive_data(&receive_data);
receive_data.launch();
while (true)
{
cin.getline(data_receive,100);
packet_send.clear();
packet_send << data_send;
if (socketUDP.send(packet_send, local_host, port_send) == sf::Socket::Done)
{
cout << "Succesfull send data" << endl;
}
else
{
cout << "Unsuccesfull data send" << endl;
}
}
return 0;
}