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

Author Topic: UDP socket not sending packets.  (Read 4551 times)

0 Members and 1 Guest are viewing this topic.

LordNani

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
UDP socket not sending packets.
« on: June 20, 2019, 11:12:48 pm »
I get error that packet ain't sent. With TCP this code worked.Things I've tried:
1)change port ::AnyPort
2)Use IP from hamachi with my friend
3)put debugging couts. After the first "while msg!=stop" loop it changes port and IP to 0 and 0000000000(Or so would seem)
4)Removing it from thread partially, *exclamation mark* partially fixed the problem, it worked 1 out of 10 times!
Help...I'll answer all questions quick.
sf::IpAddress hostIp = sf::IpAddress::getLocalAddress();
sf::UdpSocket udpSocket;
unsigned short port = 51234;
string recMsg ;
string msg;
void sendData(string msg);
sf::Thread secondThread(&sendData, msg);
int main() {
  udpSocket.bind(port);
  secondThread.launch();
while (msg != "stop") {
   
    if (udpSocket.receive(packet2, hostIp, port) != sf::Socket::Done)
      cout << "~~error receiving data" << endl;
        else {
                cout << "--data received" << endl;
                packet2 >> recMsg;
                if (recMsg != "")
                        cout << "Other user: " << recMsg << endl;
        }
  }

}
void sendData(string msg) {
  while (msg != "stop") {
    getline(cin, msg);
   
    packet << msg;

    if (udpSocket.send(packet, hostIp, port) != sf::Socket::Done)
      std::cout <<"~~Could not send data"<<endl;
}
 

LordNani

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: UDP socket not sending packets.
« Reply #1 on: June 21, 2019, 08:00:35 pm »
Does someone know what it could be? Maybe code mistakes?

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Re: UDP socket not sending packets.
« Reply #2 on: June 21, 2019, 10:16:38 pm »
First of all, can you provide a minimal code showing the problem ?

I'll try it on my own computer ;)
Mindiell
----

LordNani

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: UDP socket not sending packets.
« Reply #3 on: June 22, 2019, 12:24:17 pm »
First of all, can you provide a minimal code showing the problem ?

I'll try it on my own computer ;)

Sure, thank you!
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <math.h>
#include <vector>

enum Nodemode : char { SERVER = 's', CLIENT = 'c' };
sf::IpAddress hostIp = sf::IpAddress::getLocalAddress();
sf::UdpSocket udpSocket;
uint16_t port = 51234;
std::string recMsg = "";
std::string msg;

std::istream &operator>>(std::istream &is, Nodemode &i);
std::ostream &operator<<(std::ostream &out, const Nodemode value);

void sendData(std::string msg);
Nodemode nodestate;
sf::Thread secondThread(&sendData, msg);

int main() {

  std::cout << "Type 's' for server, and 'c' for client. If you want to close "
               "the app, type in 'stop' "
            << std::endl;
  std::cin >> nodestate;

  if (nodestate == SERVER) {
          udpSocket.bind(port);
  } else if (nodestate == CLIENT) {
          udpSocket.bind(port);
  }

  std::cout << "Current IP-address: " << hostIp.toString() << std::endl;
  std::cout << "Node state: " << nodestate << std::endl;
  std::cout << recMsg << std::endl;

  secondThread.launch();
  while (msg != "stop") {

    sf::Packet packet2;
        if (udpSocket.receive(packet2, hostIp, port) != sf::Socket::Done)
                std::cout << "~~error receiving data" << std::endl;
        else {
                std::cout << "--data received" << std::endl;
                packet2 >> recMsg;
                if (recMsg != "")
                        std::cout << "Other user: " << recMsg << std::endl;
        }
  }
  return 0;
}

std::istream &operator>>(std::istream &is, Nodemode &i) {
  char tmp;
  if (is >> tmp)
    i = static_cast<Nodemode>(tmp);
  return is;
}

std::ostream &operator<<(std::ostream &out, const Nodemode value) {
  static std::map<Nodemode, std::string> strings;
  if (strings.size() == 0) {
#define INSERT_ELEMENT(p) strings[p] = #p
    INSERT_ELEMENT(SERVER);
    INSERT_ELEMENT(CLIENT);
#undef INSERT_ELEMENT
  }
  return out << strings[value];
}

void sendData(std::string msg) {
  while (msg != "stop") {

    getline(std::cin, msg);
    sf::Packet packet;
    packet << msg;

        if (udpSocket.send(packet, hostIp, port) != sf::Socket::Done)
                std::cout << "~~Could not send data" << std::endl;
  }
}
 

LordNani

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: UDP socket not sending packets.
« Reply #4 on: June 22, 2019, 03:42:19 pm »
First of all, can you provide a minimal code showing the problem?

I'll try it on my own computer ;)

I fixed it... Firstly I had to change ports because I am using the local address for testing.
so now it is like this:
Server: bind to 20000, receive on 20000, send to 20001.
Client: bind to 20001, receive on 20001, send to 20000.
And the core problem
It seems that after udpSocket fails to send\receive data on some determined ip+port, it resets it completely, so I had to create temporary variables. If someone knows how to get rid of it, or optimize the current way, tell me please.