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

Author Topic: Socket Unresponsive?  (Read 2953 times)

0 Members and 1 Guest are viewing this topic.

Shay9999

  • Newbie
  • *
  • Posts: 24
    • View Profile
Socket Unresponsive?
« on: August 05, 2011, 04:17:24 am »
I'm having some problems with these two programs that seem to be unresponsive after a certain point:

Program 1:
Code: [Select]
#include <SFML/Network.hpp>
#include <iostream>

int main()
{
unsigned short Port;
sf::IpAddress Address1 = sf::IpAddress::GetPublicAddress();
sf::UdpSocket Socket;
char Buffer[] = "Hi guys!";
std::cin>>Port;
if (Socket.Send(Buffer, sizeof(Buffer), Address2, Port) != sf::Socket::Done)
{
std::cout<<"Socket (Send) Failed to Send"<<std::endl;
}
else if (Socket.Send(Buffer, sizeof(Buffer), Address1, Port) == sf::Socket::Done)
{
std::cout<<"Message sent"<<std::endl;
}
return 0;
}


Program 2:
Code: [Select]
#include <SFML/Network.hpp>
#include <iostream>

int main()
{
unsigned short Port;
sf::IpAddress Address1 = sf::IpAddress::GetPublicAddress();
        std::cin>>Port;
sf::UdpSocket Listen;
if (Listen.Bind(Port) == sf::Socket::Done)
{
std::cout<<"Successfully bound to Port: "<<Port<<std::endl;
}
unsigned short portn = Listen.GetLocalPort();

std::size_t Received;
char Buffer2[128];
if (Listen.Receive(Buffer2, sizeof(Buffer2),Received,Address1,portn) != sf::Socket::Done)
{
std::cout<<"Error"<<std::endl;
}
else if (Listen.Receive(Buffer2, sizeof(Buffer2),Received,Address1,portn) == sf::Socket::Done)
{
std::cout<<"Message From:"<<std::endl;
std::cout<<Address1<<":"<<Listen.GetLocalPort()<<std::endl;
std::cout<<Buffer2<<std::endl;
}
else
std::cout<<"Unknown Problem";
return 0;
}



These are just snippings from the code, so if there's an error, it's probably me doing bad copy/pasting. Anyways, the send program seems to work fine, but the Listen program becomes unresponsive when it hits the .Receive...

So, what am I doing wrong?
C++-Trainee
SFML-Trainee

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Socket Unresponsive?
« Reply #1 on: August 05, 2011, 07:58:48 am »
In "Program 1", you send to Address2 then Address1. Is it just a typo or do you really send to two different addresses?

And what's the console output of these programs?
Laurent Gomila - SFML developer

Shay9999

  • Newbie
  • *
  • Posts: 24
    • View Profile
Socket Unresponsive?
« Reply #2 on: August 11, 2011, 12:48:14 am »
sorry for long continuation time, I was completely lost when I posted this. It's a typo, sending to only 1 address (the other program). Address1 is sf::IpAddress, and Address2 is a string.

There is no output. I've done things like

Code: [Select]
if (Listen.Receive(/*etc.*/) == sf::Socket::Error)

and the code is still just completely responsive, as though it's not even looking at the sockets.

And again, sorry for the long reply and thanks for your quick response ^_^ =P
C++-Trainee
SFML-Trainee

sknnywhiteman

  • Newbie
  • *
  • Posts: 38
    • View Profile
    • Email
Socket Unresponsive?
« Reply #3 on: August 11, 2011, 02:18:40 am »
Also, the IPAddress.getPublicAddress() function has a bug in it, when I used it in the past, it made my program very unresponsive, and lagged a lot, but when I got rid of that function, and just used the normal Address, it was fine and ran fast.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Socket Unresponsive?
« Reply #4 on: August 11, 2011, 08:18:17 am »
Quote
There is no output

??
Nothing at all? So every function fails?

Quote
Also, the IPAddress.getPublicAddress() function has a bug in it, when I used it in the past, it made my program very unresponsive, and lagged a lot, but when I got rid of that function, and just used the normal Address, it was fine and ran fast.

It is slow by design, because it retrieves your address from an external server. Call it once at the beginning of your program, and store the result.
Laurent Gomila - SFML developer

Shay9999

  • Newbie
  • *
  • Posts: 24
    • View Profile
Socket Unresponsive?
« Reply #5 on: August 12, 2011, 07:50:17 am »
Sorry Laurent, I wrote a false statement.

It has no output once it hits:

Code: [Select]
  if (Listen.Receive(Buffer2, sizeof(Buffer2),Received,Address1,portn) != sf::Socket::Done)
   {
      std::cout<<"Error"<<std::endl;
   }
   else if (Listen.Receive(Buffer2, sizeof(Buffer2),Received,Address1,portn) == sf::Socket::Done)
   {
      std::cout<<"Message From:"<<std::endl;
      std::cout<<Address1<<":"<<Listen.GetLocalPort()<<std::endl;
      std::cout<<Buffer2<<std::endl;
   }
   else
      std::cout<<"Unknown Problem";
C++-Trainee
SFML-Trainee

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Socket Unresponsive?
« Reply #6 on: August 12, 2011, 08:05:05 am »
That's because your first program sends one thing, and your second program tries to receive two: the second one will block because it will wait forever.
Laurent Gomila - SFML developer

Shay9999

  • Newbie
  • *
  • Posts: 24
    • View Profile
Socket Unresponsive?
« Reply #7 on: August 13, 2011, 11:10:58 pm »
Thanks Laurent ^_^
C++-Trainee
SFML-Trainee

 

anything