Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: UDP Chat doesn't work - cannot to send message.  (Read 3048 times)

0 Members and 2 Guests are viewing this topic.

domin568

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
UDP Chat doesn't work - cannot to send message.
« on: December 01, 2013, 03:24:38 pm »
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;
}
 

domin568

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: UDP Chat doesn't work - cannot to send message.
« Reply #1 on: December 08, 2013, 11:58:07 am »
Anybody can help me :) ??

razzorflame

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: UDP Chat doesn't work - cannot to send message.
« Reply #2 on: December 08, 2013, 12:20:49 pm »
Why you're binding client socket? You don't need to bind client socket (i think).

domin568

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: UDP Chat doesn't work - cannot to send message.
« Reply #3 on: December 08, 2013, 10:58:43 pm »
I think That if i want to receive data on client i have to bind on port wchich can receive data. If i delete thread receive_data on client this app works, but only for sending data from client to server, but not from server to client. Problem is thread, but i dont know why it doesnt work, Because when i have created a TCP chat, thread worked great. // Razzorflame are you from cpp0x.pl ??

razzorflame

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: UDP Chat doesn't work - cannot to send message.
« Reply #4 on: December 15, 2013, 04:54:16 pm »
Yes, i'm from cpp0x.pl but it doesn't matter. Look at this example: https://github.com/SFML/SFML/blob/master/examples/sockets/UDP.cpp. You won't need to bind socket at client's side. Bind method is like listen method in TCP sockets.