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

Author Topic: Cannot receive when sending to myself  (Read 2290 times)

0 Members and 1 Guest are viewing this topic.

yumi_cheeseman

  • Newbie
  • *
  • Posts: 1
    • View Profile
Cannot receive when sending to myself
« on: July 02, 2011, 12:24:20 am »
I'm completely new to Networking, and have been attempting to send myself UDP packets, but I'm just not receiving them.

Sending to 127.0.0.1
gets to binding socket but cannot receive.

I have done it both with and without port forwarding (I assumed it wouldn't make a difference, but tried anyway).

Code: [Select]
int main ()
{
sf::IPAddress mycomp("127.0.0.1");
sf::SocketUDP mycompSocket;
char buffer[] = "hello computer";
std::cout << "sending data" << std::endl;

if (mycompSocket.Send( buffer, sizeof(buffer), sf::IPAddress::LocalHost, 4567) != sf::Socket::Done)
{
std::cout << "error sending" << std::endl;
}

std::cout << "data sent" << std::endl;

sf::SocketUDP receiver;
char thingo[128] = "";
std::size_t receivedsize;
sf::IPAddress sender;
unsigned short port;
receiver.SetBlocking(false);

if (!receiver.Bind(4567))
{
std::cout << "could not bind socket" << std::endl;
}

std::cout << "port boundedededed: " << receiver.GetPort() << std::endl;
std::cout << "socket bound" << std::endl;

if (receiver.Receive(thingo, sizeof(thingo), receivedsize, sender, port) != sf::Socket::Done)
{
std::cout << "could not receive" << std::endl;

}

std::cout << "the IP Address: " << sender << std::endl;
std::cout << "received: " << thingo << std::endl;

mycompSocket.Close();
receiver.Close();

}


Any help would be greatly appreciated!

Fouf

  • Newbie
  • *
  • Posts: 23
    • View Profile
Cannot receive when sending to myself
« Reply #1 on: July 02, 2011, 03:57:13 am »
- Fouf

Haikarainen

  • Guest
Cannot receive when sending to myself
« Reply #2 on: July 02, 2011, 01:58:48 pm »
Try separating the send and recieve in to 2 different apps, also, fix your error handling.

Code: [Select]
if (mycompSocket.Send( buffer, sizeof(buffer), sf::IPAddress::LocalHost, 4567) != sf::Socket::Done){
      std::cout << "error sending" << std::endl;
}else{
  std::cout << "something went wrong" << std::endl;
}


Or better yet make a switch out of the returntype.