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

Author Topic: Simple Test Not Working (I'm a noob)  (Read 6864 times)

0 Members and 1 Guest are viewing this topic.

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Simple Test Not Working (I'm a noob)
« on: January 17, 2011, 04:19:29 pm »
Hi, I'm new to network programming and I was trying to get working a little test. I'm planning to write a small game which I can play with a friend without any central server between. Neither a test with UDP nor with TCP is working for me. I'm testing it locally on my computer.
This is my code for the server, who receives something in this case:
#include <iostream>
#include <stdio.h>

#include <SFML/Network.hpp>

int main(int argc, char **argv)
{
    sf::IPAddress Address = sf::IPAddress::GetPublicAddress();
    std::cout<<"Share Your IP and press enter: "<<Address.ToString()<<std::endl;
   
    std::cin.get();
   
    sf::SocketUDP Socket;
    std::cout<<"Bind Socket\n";
    if(!Socket.Bind(4567))
    {
      std::cout<<"Error!\n";
      exit(1);
    }
    char Buffer[20];
    std::size_t Received;
    sf::IPAddress Sender;
    u16 Port;
    std::cout<<"Receive...\n";
    if(Socket.Receive(Buffer, sizeof(Buffer), Received, Sender, Port)!=sf::Socket::Done)
    {
      std::cout<<"Error!\n";
      exit(1);
    }
    std::cout<<Sender<<":"<<Port<<std::endl;
    std::cout<<Buffer<<std::endl;
    Socket.Close();
 
  return 0;
}
 

And my client, which sends "Hello, World!":
#include <iostream>
#include <stdio.h>

#include <SFML/Network.hpp>

int main(int argc, char **argv)
{
    char ip[32];
    std::cout<<"Type the ip address: ";
    gets(ip);
    sf::IPAddress Address(ip);
    std::cout<<"You'll send to: "<<Address.ToString()<<std::endl;
   
    sf::SocketUDP Socket;
    char Buffer[] = "Hello, World!";
   
    std::cout<<"Send Text\n";
    if(Socket.Send(Buffer, sizeof(Buffer), Address, 4567)!=sf::Socket::Done)
    {
      std::cout<<"Error Sending Text!\n";
      exit(1);
    }
    Socket.Close();
 
  return 0;
}
 


I tried connected using the public ip as well as using the local ip. I start the server, outputting "Receive..." with no end, and the client. The client doesn't output any text and seem to terminate regularly. My message seems to be sent correctly, but the server doesn't receive anything and I don't get any notify by the Firewall or something.


Anybody an idea?
« Last Edit: March 26, 2012, 08:12:23 pm by Shy Guy »
Please note that my previous display name was "Shy Guy".

D

  • Newbie
  • *
  • Posts: 12
    • View Profile
Simple Test Not Working (I'm a noob)
« Reply #1 on: January 20, 2011, 10:19:38 am »
It's working for me (at least if you fix the "std<<cout" in client.cpp :P). Are you sure that you use localhost or 127.0.0.1 as an ip if you're testing in your home network? (Client and the server on the same computer).

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Simple Test Not Working (I'm a noob)
« Reply #2 on: January 20, 2011, 05:02:13 pm »
Quote from: D
It's working for me (at least if you fix the "std<<cout" in client.cpp :P). Are you sure that you use localhost or 127.0.0.1 as an ip if you're testing in your home network? (Client and the server on the same computer).
I tried the local ip returned by the function, and the public one. All on one computer, withouth success. (the "std<<cout" thing is corrected now  :roll: )

I have 'ported' it from UDP to TCP now, but still there is everything working. When using a single computer, or two here at home, using the local ip address, the networking works perfectly! But as soon as I share my public ip with anybody else and he connects to me (or vice versa), the connecting fails. I don't get any FireWall notify here (or if, I check 'allow').

This is my new code for the program (server+client):
#include <iostream>
#include <stdio.h>
#include <string.h>

#include <SFML/Network.hpp>


sf::SelectorTCP Selector;

int main(int argc, char **argv)
{
  if(argc==1) return 0;
  if(!strcmp(argv[1], "-server")) // receive
  {
    sf::SocketTCP Listener;
   
    sf::IPAddress Address = sf::IPAddress::GetLocalAddress();
    std::cout<<"Local IP: "<<Address.ToString()<<std::endl;
    Address = sf::IPAddress::GetPublicAddress();
    std::cout<<"Public IP: "<<Address.ToString()<<std::endl;
    //std::cout<<"Share Your IP and press enter: "<<Address.ToString()<<std::endl;
   
    std::cout<<"Listen...\n";
    if(!Listener.Listen(4567))
    {
      std::cout<<"Error.\n"; exit(1);
    }
    std::cout<<"Accept...\n";
   
    sf::IPAddress ClientAddress;
    sf::SocketTCP Client;
    if(Listener.Accept(Client, &Address)!=sf::Socket::Done)
    {
      std::cout<<"Error.\n"; exit(1);
    }
   
    std::cout<<"Receive...\n";
    char Buffer[128];
    std::size_t Received;
    if (Client.Receive(Buffer, sizeof(Buffer), Received) != sf::Socket::Done)
    {
      std::cout<<"Error.\n"; exit(1);
    }
    std::cout<<"Message: "<<Buffer<<std::endl;
   
    Listener.Close();
  }
  else if(!strcmp(argv[1], "-client")) // send
  {
    sf::SocketTCP Client;
   
    char ip[32];
    std::cout<<"Type the ip address to connect: ";
    gets(ip);
    sf::IPAddress Address(ip);
   
    std::cout<<"Connect...\n";
    if(Client.Connect(4567, Address)!=sf::Socket::Done)
    {
      std::cout<<"Error!\n"; exit(1);
    }
    char Buffer[] = "Hello, World!";
    if(Client.Send(Buffer, sizeof(Buffer))!=sf::Socket::Done)
    {
      std::cout<<"Error sending message!\n"; exit(1);
    }
    Client.Close();
  }
 
  return 0;
}
 
« Last Edit: March 26, 2012, 08:11:41 pm by Shy Guy »
Please note that my previous display name was "Shy Guy".

CJ_COIMBRA

  • Full Member
  • ***
  • Posts: 112
    • ICQ Messenger - 13077864
    • View Profile
    • http://www.cjcoimbra.com
    • Email
Simple Test Not Working (I'm a noob)
« Reply #3 on: January 21, 2011, 02:30:30 am »
Maybe your issue is the same as mine. Are you using a router?
CJ

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Simple Test Not Working (I'm a noob)
« Reply #4 on: January 21, 2011, 03:19:41 pm »
Quote from: "CJ_COIMBRA"
Maybe your issue is the same as mine. Are you using a router?

Yes, I'm using a router. I'm using a laptop, having wireless network.

EDIT: If my code should work in your eyes (who ever ^^), could we test it together so that I can experience the working myself, please?
Please note that my previous display name was "Shy Guy".

D

  • Newbie
  • *
  • Posts: 12
    • View Profile
Simple Test Not Working (I'm a noob)
« Reply #5 on: January 24, 2011, 09:40:54 am »
You should forward the port you're using (4567) from your router settings to your local ip where the server is running. See more: www.portforward.com

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Simple Test Not Working (I'm a noob)
« Reply #6 on: January 24, 2011, 05:48:54 pm »
Quote from: "D"
You should forward the port you're using (4567) from your router settings to your local ip where the server is running. See more: www.portforward.com

I don't catch on there. Do I need to understand the internet well and to know about 'my' (it is actually not mine) router?
Please note that my previous display name was "Shy Guy".

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
Simple Test Not Working (I'm a noob)
« Reply #7 on: January 25, 2011, 12:10:40 am »
Basically, your router is acting as a barrier between the external, big bad inernet, and the safe, nice internal network. Any communications from the internet come through the router, and the router allows only information from approved ports to pass through. The port your test isn't on the approved list. By "forwarding" this port on your router, you tell it to allow communications from that port.
Further reading:
http://portforward.com/help/portforwarding.htm

Hope that helps. :D

D

  • Newbie
  • *
  • Posts: 12
    • View Profile
Simple Test Not Working (I'm a noob)
« Reply #8 on: January 25, 2011, 01:05:27 am »
Great explanation, thanks. :) Just to add some more, your router has only one public ip which other people can see from the internet. The problem is that beneath that public ip, you can have multiple computers with local ip:s. Because of that, the router doesn't know which local ip (computer) the data needs to be sent (which computer runs the server in the local network), unless you tell the router that by forwarding the port.

The forwarding process is quite easy, and does not need any advanced knowledge of the Internet. There's probably a tutorial lying around, if you google your router model + 'port forwarding'.

I drew a (ugly) picture from the situation. (Click to open a bigger one.)


The situation is now that the router (the big question mark box) has an incoming data to port 4567, and it doesn't know what to do before it is given the port forward rule (the green text). After that it knows to send the data to the corresponding local ip (the green arrow).

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Simple Test Not Working (I'm a noob)
« Reply #9 on: January 29, 2011, 04:06:43 pm »
Quote from: "tntexplosivesltd"

Further reading:
http://portforward.com/help/portforwarding.htm

Hope that helps. :D

Hmm, I seem to need to know which router I am using. I can see that it is NetGear, but I don't know which model it is and don't know where I can find it. (It is not my own router.) In addition, as the router is owned by my elder brother, I don't have the password for it, and he will not tell me.
Please note that my previous display name was "Shy Guy".

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
Simple Test Not Working (I'm a noob)
« Reply #10 on: February 03, 2011, 11:48:10 am »
A good rule is to use 127.0.0.1 for easy testing. If it works locally, it should work over the internet.

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Simple Test Not Working (I'm a noob)
« Reply #11 on: February 03, 2011, 10:31:45 pm »
Quote from: "tntexplosivesltd"
A good rule is to use 127.0.0.1 for easy testing. If it works locally, it should work over the internet.

Yeah, I may develop an online game that way, but I might never really play them with a friend far away :(
Please note that my previous display name was "Shy Guy".

 

anything