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

Author Topic: need help with UDP socket  (Read 4069 times)

0 Members and 1 Guest are viewing this topic.

da_coolest

  • Newbie
  • *
  • Posts: 19
    • View Profile
need help with UDP socket
« on: June 07, 2011, 10:44:15 pm »
Hi, I have never done any programming related to socket and network in c++. I have been learning to program using sfml and I was trying created a small program to see if its perfectly working on my pc. The program sends some small text message and I have set the destination address as my own IP address to test it. But havent got any good results. Is it possible to do such a thing, to capture a UDP packet from the same pc ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
need help with UDP socket
« Reply #1 on: June 07, 2011, 10:50:24 pm »
Yes it is possible.

If you want us to find out what's wrong in your code, show it ;)
Laurent Gomila - SFML developer

da_coolest

  • Newbie
  • *
  • Posts: 19
    • View Profile
need help with UDP socket
« Reply #2 on: June 07, 2011, 11:22:20 pm »
Quote from: "Laurent"
Yes it is possible.

If you want us to find out what's wrong in your code, show it ;)


Thanks for helping :)

Code: [Select]
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
using namespace std;

void sendMsg ()
{
string ip = " ";
char msg[] = "Testing message only.";

cout << "Enter the IP address to send the message : ";
cin >> ip;

sf::IPAddress Address1(ip);
sf::SocketUDP Socket;

if (Socket.Send(msg, sizeof(msg), ip, 4567) != sf::Socket::Done)
{
  cout << "Error sending...\n";
}

}

void getMsg ()
{
sf::SocketUDP Socket;

if (!Socket.Bind(4567))
{
   cout << "Error binding...\n";
}

char Buffer[128];
std::size_t Received;
sf::IPAddress Sender;
unsigned short Port;
if (Socket.Receive(Buffer, sizeof(Buffer), Received, Sender, Port) != sf::Socket::Done)
{
cout << "Error receiving message...\n";
}

cout << Sender << ":" << Port <<endl;

cout << Buffer <<endl;
}

void main ()

{
char ch;
cout << "Send message (S), Get message (G) : ";
cin >> ch;

if (ch == 'S' || ch == 's')
{
sendMsg();
}
else if (ch == 'G' || ch == 'g')
{
getMsg();
}
system ("PAUSE");
}



I was using 2 instances of this program (one for sending and the other one for listening). The receiving program did not produce any response indicating that it has captured anything.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
need help with UDP socket
« Reply #3 on: June 07, 2011, 11:48:25 pm »
Could you slightly modify this code, and use a fixed IP address instead of one that is entered by user? It's easier to debug/test for us, and see if you're using a bad address for example ;)

Quote
The receiving program did not produce any response

Not even an error?
Laurent Gomila - SFML developer

da_coolest

  • Newbie
  • *
  • Posts: 19
    • View Profile
need help with UDP socket
« Reply #4 on: June 08, 2011, 11:26:13 am »
Quote from: "Laurent"
Could you slightly modify this code, and use a fixed IP address instead of one that is entered by user? It's easier to debug/test for us, and see if you're using a bad address for example ;)

Quote
The receiving program did not produce any response

Not even an error?


No error messages. Once I have been using the same port for torrent before I started testing this program, and then the program was receiving different messages from different addresses. So, it seems that the problem is in sending part ?

Code: [Select]
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
using namespace std;

void sendMsg ()
{
char msg[] = "Testing message only.";

sf::IPAddress Address1("112.134.72.168");
sf::SocketUDP Socket;

if (Socket.Send(msg, sizeof(msg), Address1, 4568) != sf::Socket::Done)
{
  cout << "Error sending...\n";
}
}

void getMsg ()
{
sf::SocketUDP Socket;

if (!Socket.Bind(4568))
{
   cout << "Error binding...\n";
}

char Buffer[128];
std::size_t Received;
sf::IPAddress Sender;
unsigned short Port;
if (Socket.Receive(Buffer, sizeof(Buffer), Received, Sender, Port) != sf::Socket::Done)
{
cout << "Error receiving message...\n";
}

cout << Sender << ":" << Port <<endl;

cout << Buffer <<endl;
}

void main ()
{
char ch;
cout << "Send message (S), Get message (G) : ";
cin >> ch;

if (ch == 'S' || ch == 's')
{
sendMsg();
}
else if (ch == 'G' || ch == 'g')
{
getMsg();
}
system ("PAUSE");
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
need help with UDP socket
« Reply #5 on: June 08, 2011, 11:33:02 am »
Quote
Code: [Select]
sf::IPAddress Address1("112.134.72.168");

That's what I expected ;)

You're using your public IP, which means that the communication goes through the internet and is probably blocked by your router.

Use either your local network address (192.168.xxx.xxx) or localhost (127.0.0.1).
Laurent Gomila - SFML developer

da_coolest

  • Newbie
  • *
  • Posts: 19
    • View Profile
need help with UDP socket
« Reply #6 on: June 08, 2011, 01:24:45 pm »
Quote from: "Laurent"
Quote
Code: [Select]
sf::IPAddress Address1("112.134.72.168");

That's what I expected ;)

You're using your public IP, which means that the communication goes through the internet and is probably blocked by your router.

Use either your local network address (192.168.xxx.xxx) or localhost (127.0.0.1).


it works with the local network address!  But I have disabled firewalls and done the port forwarding as well. Any idea to get it working with the public addresses?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
need help with UDP socket
« Reply #7 on: June 08, 2011, 01:28:36 pm »
Quote
But I have disabled firewalls and done the port forwarding as well. Any idea to get it working with the public addresses?

Nop, nothing more than what you already did, sorry. That should be enough.
Laurent Gomila - SFML developer

da_coolest

  • Newbie
  • *
  • Posts: 19
    • View Profile
need help with UDP socket
« Reply #8 on: June 08, 2011, 02:04:42 pm »
Quote from: "Laurent"
Quote
But I have disabled firewalls and done the port forwarding as well. Any idea to get it working with the public addresses?

Nop, nothing more than what you already did, sorry. That should be enough.


oh ok. I'll check those settings again then. Thanks a lot for helping me !

Kian

  • Newbie
  • *
  • Posts: 44
    • View Profile
need help with UDP socket
« Reply #9 on: June 14, 2011, 10:52:04 pm »
Not sure if this helps, but port forwarding usually refers to messages arriving from the WAN side of the router, being routed to a specific IP on the LAN. Not sure how the router will handle a request to the router's WAN IP from the LAN.

Have you considered trying to mail the program to a friend for them to try hitting your router's IP from outside the LAN?