11 Replies - 5920 Views - Last Post: 10 July 2013 - 12:27 PM

#1 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

C++ - Winsock - Sending and Receiving through the Internet

Posted 04 July 2013 - 11:12 PM

Hello!

So a basic explination of what i'm trying to do is this:

1.Open a server in which multiple clients can join.
2.Clients join the server (From anywhere around the world - Through the internet).
3.Clients can play with eachother (SDL Images that send and receive player_x and player_y values).

I current have this working!
Multiple clients can join and play with eachother.
HOWEVER:

QUESTION:
---------
THEY CANNOT PLAY WITH EACHOTHER AROUND THE WORLD???

The players can play together - This works.. But only on the connection in which the server is on?
I can't ask one of you to play with me while the server is running: It doesn't work.


WHAT I'VE TRIED:
----------------
(I know the address is the problem, So that is the code I will be showing.)
SERVER CONNECTING:
server.sin_family=AF_INET; 
server.sin_addr.s_addr=INADDR_ANY; 
server.sin_port=htons(2359);


CLIENT CONNECTING:
ser.sin_family=AF_INET;
ser.sin_port=htons(2359);
ser.sin_addr.s_addr=inet_addr("IPv4 Address (From ipconfig)");

Now, I know this isn't the correct info (On the client's code). The IPv4 is not online play. I say this because the Address from "www.whatsmyip.org" seems to STOP my client program from even running. (Windows - Does the whole "Not responding" thing.)


CORRECT ME IF I DID THIS WRONG, PLEASE:
---------------------------------------
I did some 'Port Forwarding' to get this working Via Internet to everyone whoever wants to play around the world.
So I went in, and put the port forward onto my IPv4 address and made the port to forward the same as in the code, 2359.

I had a friend from somewhere else in the world try to play with me. Didn't work.
Client states that he is not connected (His client).
Server states that he is not connected (My server).

ONLY when the client address is set to the IPv4 address, can anyone play. But that anyone must be connected to MY internet connection.

All help is appreciated!

Thanks,
Fysez

Is This A Good Question/Topic? 0
  • +

Replies To: C++ - Winsock - Sending and Receiving through the Internet

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,213
  • Joined: 07-September 06

Re: C++ - Winsock - Sending and Receiving through the Internet

Posted 04 July 2013 - 11:48 PM

Not certain here, but it sounds like you are using your internal home IP address and not your external IP address. I am not sure how much you know about networking, so I will go over the basics, this isn't meant to be demeaning of anything of that nature, just getting us on the same page.

Each computer in a home network has an IP address relative to that network. If you have multiple routers in your house you likely have multiple independent subnets all able to access the internet, but not necessarily able to talk with one another. This same concept continues out of your house. All the traffic you send to the internet goes through your external facing home IP (the one given to you by your ISP, not your home router). So if someone wants to connect to a server running in your home network you must give them your external (ISP) IP address, and set up port forwarding to ensure that the appropriate traffic gets to the appropriate place. Furthermore, IP addresses change. Newer routers try to keep reservations around for longer, but that doesn't change the fact that ISPs normally change dynamic IPs every 30 days or so (meaning that if you hardcode the value into your program you will need to update it every month or so).

Now, a fairly easy way to get around this is to set up a static IP address for your home (read pay more money to your ISP so they don't change your IP address on you).

What I would suggest instead of that is getting a server outside of your home that allows you to run C/C++ level programs on it (such as Amazon Web Services -- AWS) and put your code there. They are pretty good about only charging you minimally (such as shutting down your Virtual server when people aren't looking for it and only charging you when it is actually being used) so it may be a viable option. That way you can give your program a hostname, have it run a DNS lookup on that hostname and get the IP address for the server each time it runs without you needing to change it.

I will warn you (now that you have read all of that) that I am not by any means a networking expert. I have set up subnets and static routes in my house before, but that is pretty much the extent of my networking knowledge. I haven't ever tried getting people to access an in-home server externally (normally I am attempting to block of my internal network from everyone externally in fact). So there will likely be other members of the forum with more information than I am able to give.

Anyways, hope that was of some use.
Was This Post Helpful? 0
  • +
  • -

#3 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: C++ - Winsock - Sending and Receiving through the Internet

Posted 05 July 2013 - 12:22 AM

I love the fact that you tried your best to help me out with a great explination.
I will not be looking into purchasing any online server-stuff (As you suggested); Not because I don't trust your info, But because I want this to be free for me.=)

I'm not saying this to be in any way offensive, But I did state that i've tried my Address that is connected to the internet from "whatsmyip.org", and it failed.

The only thing I KNOW works is the IPv4 Address listed in CMD's ipconfig.
However, that is LAN only.
So this will not work.

Thank you so much for your help, and thanks for the SPEEDY reply!
I'm glad you put your time into an explination for me.=D

Thanks,
Fysez
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: C++ - Winsock - Sending and Receiving through the Internet

Posted 05 July 2013 - 05:36 AM

Please try to help fysez. I know he is really excited about getting his code to work. As background information, you can refer to the older C/C++ thread to see various things he's already attempted to get this to work for him.

Did you try running telnet from the client computer to see if a connection can be established? Have your friend run 'telnet a.b.c.d 2359' where a.b.c.d is the results from 'whatismyip.org' on your server computer. If successful, your server should at report that a connection has been established, but will probably be receiving junk data, and your telnet on your friend's computer should at least show that a connection has been established and possibly be getting junk characters.

Without having him enabling remote desktop, port forwarding for port 3389, and trying to see if he can get his friend to connect from the client computer to his computer, the only other thing I can think of is for him to try running known good server-client sample code from MSDN: http://msdn.microsof...9(v=vs.85).aspx
Was This Post Helpful? 0
  • +
  • -

#5 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: C++ - Winsock - Sending and Receiving through the Internet

Posted 05 July 2013 - 11:51 AM

View PostSkydiver, on 05 July 2013 - 05:36 AM, said:

Please try to help fysez. I know he is really excited about getting his code to work. As background information, you can refer to the older C/C++ thread to see various things he's already attempted to get this to work for him.

Did you try running telnet from the client computer to see if a connection can be established? Have your friend run 'telnet a.b.c.d 2359' where a.b.c.d is the results from 'whatismyip.org' on your server computer. If successful, your server should at report that a connection has been established, but will probably be receiving junk data, and your telnet on your friend's computer should at least show that a connection has been established and possibly be getting junk characters.

Without having him enabling remote desktop, port forwarding for port 3389, and trying to see if he can get his friend to connect from the client computer to his computer, the only other thing I can think of is for him to try running known good server-client sample code from MSDN: http://msdn.microsof...9(v=vs.85).aspx


Yes, We tried the telnet and it failed. Gave him some error, and did not connect to my server.
Something about servername not supported for socket type? Not sure.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,896
  • Joined: 25-December 09

Re: C++ - Winsock - Sending and Receiving through the Internet

Posted 05 July 2013 - 06:32 PM

If I understand correctly you can connect and "play" your game on your local Intranet but you can't connect via the Internet. Is that correct?

If so please describe how the server is connected to the net.

And what type of connection is being supplied by the Internet Provider. Does your IP allow you to connect your server directly to the net?

Jim
Was This Post Helpful? 0
  • +
  • -

#7 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: C++ - Winsock - Sending and Receiving through the Internet

Posted 07 July 2013 - 12:32 PM

Sorry for the late reply, I've been gone without wifi.

Anywho, That is correct.
Local play works GREAT.
Via Internet does not...

SERVER---
int main() {
    SOCKET sock;
   
    DWORD thread; 

    WSADATA wsaData;
    sockaddr_in server;

    int ret = WSAStartup(0x101,&wsaData);
   
    if(ret != 0) {
         return 0;
    }

    server.sin_family=AF_INET; 
    server.sin_addr.s_addr=INADDR_ANY; 
    server.sin_port=htons(2359);
    sock=socket(AF_INET,SOCK_STREAM,0); 

    if(sock == INVALID_SOCKET) {
         return 0;
    }

    if( bind(sock,(sockaddr*)&server,sizeof(server)) !=0 ) {
         return 0;
    }
  
    if(listen(sock,5) != 0) {
         return 0;
    }

    SOCKET client;
    sockaddr_in from;
    int fromlen = sizeof(from); 

    while(true) { 
         client = accept(sock,(struct sockaddr*)&from,&fromlen);
    ...

This is connecting the server, to the client.

CLIENT---
[Local-Address: Works]
DWORD poll;
    int res,i=1,port=999;
    char buf[100];
    char msg[100] = "";
    char ip[15];
    WSADATA data;

    //signal stuff

    sockaddr_in ser;
    sockaddr addr;
    
    if (WSAStartup(MAKEWORD(1, 1), &data) != 0) {
        return 255;
    }
    
    int retval = doit(argc, argv);

    ser.sin_family=AF_INET;
    ser.sin_port=htons(2359);
    ser.sin_addr.s_addr=inet_addr("IPv4 Address here");      //Set the address we want to connect to

    memcpy(&addr,&ser,sizeof(SOCKADDR_IN));
    
    res = WSAStartup(MAKEWORD(1,1),&data);
 
    if(res != 0)
        gamerunning = false;

    sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

    if(sock==INVALID_SOCKET )
            gamerunning = false;
        else if(sock==SOCKET_ERROR)
            gamerunning = false;

    res=connect(sock,&addr,sizeof(addr));
    if(res !=0 ) {
            gamerunning = false;
    } else {
            memcpy(&ser,&addr,sizeof(SOCKADDR));
    }
    ...


Again, That was for local play - Which works.
Otherwise, There's the online play (That does not work).
All I did was change ser.sin_addr.s_addr=inet_addr("IPv4 Address here");
To:
ser.sin_addr.s_addr=inet_addr("IP found in whatsmyip.org");

As far as port forwarding,
I port forwarded my IP Address under "IPv4", then used the port within my game program, 2359.

I then ran the server program and had my friend use telnet and see if it worked.
He did the telnet, Used MY 'whatmyip.org' code, and then openned the port 2359.
This did not work, either.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,896
  • Joined: 25-December 09

Re: C++ - Winsock - Sending and Receiving through the Internet

Posted 08 July 2013 - 11:57 AM

If your program is "playing well" on the local intranet I would suspect that your problem may not be in your program, but in your network setup.

So the fist thing to verify is that you can ping your server from the Internet. Make sure if ping appears to be working to disconnect your server to make sure it's actually your server that is responding.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: C++ - Winsock - Sending and Receiving through the Internet

Posted 09 July 2013 - 08:13 PM

Okay I tried doing this "ping" thing,
But I'm not sure I did it right.
If I did:
It failed.

I went to "cmd" and typed in:
ping -j {IPv4 Address here - without brackets}
and got "General Failure" 4 times.

This post has been edited by fysez: 09 July 2013 - 08:14 PM

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,896
  • Joined: 25-December 09

Re: C++ - Winsock - Sending and Receiving through the Internet

Posted 09 July 2013 - 09:33 PM

Try going to this website from either the server or client and entering the appropriate ip address. Actually go to this site from both the server and client.

Also does the your IP block contain what you think is your correct IP address?

Jim

This post has been edited by jimblumberg: 09 July 2013 - 09:33 PM

Was This Post Helpful? 0
  • +
  • -

#11 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: C++ - Winsock - Sending and Receiving through the Internet

Posted 10 July 2013 - 11:41 AM

View Postjimblumberg, on 09 July 2013 - 09:33 PM, said:

Try going to this website from either the server or client and entering the appropriate ip address. Actually go to this site from both the server and client.

Also does the your IP block contain what you think is your correct IP address?

Jim


Sorry, i'm not really following what you're asking me to do :o

Not sure about this IP block, either :o
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,896
  • Joined: 25-December 09

Re: C++ - Winsock - Sending and Receiving through the Internet

Posted 10 July 2013 - 12:27 PM

Sorry forgot to add the link.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1