C++ and Winsock - Connecting Via Internet

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

44 Replies - 10505 Views - Last Post: 28 October 2013 - 07:38 PM

#1 LunaRebirth  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 21-March 13

C++ and Winsock - Connecting Via Internet

Posted 05 October 2013 - 12:51 PM

Hey guys! I had a question on Winsock.
I tried reading how to do this many times, And I just keep failing.
I've created a very simple Server-Client application where the server sends a char to the client, the client receives it, and then states it (cout).

Yes, This is quite simple. But here's the problem:
This works Via LAN play. but not over the internet.
I tried setting up a port forwarding (The IPv4 Address and the port used in my application).
The Server runs, and the client connects to 'My IPv4 address' in the same port that was in the port forwarding.
(I do not have enabled fire walls on my application, Server or Client).

Here's what I've tried as far as testing if this would work:

LAN:
Open the server. Open the client (Same computer). The server detects the client has connected, and the client has detected the server. The server sends the char, and the client receives it and states what was received.
WORKS. This works perfectly as it should!

Internet:
Open the server. Open the client (Different computer, different router, different location, different person). The server does NOT detect the client. The client does NOT detect the server.

Telnet:
I tried using Telnet on the same PC as the server. This WORKS! The server recognizes telnet as accepted. Telnet receives the char, and states it as normal.

Server Code (Section for setting the server up):
(This is not my code. I got this on a tutorial)
//ect...
    // our masterSocket(socket that listens for connections)
    SOCKET sock;
   
    // for our thread
    DWORD thread; 

    WSADATA wsaData;
    sockaddr_in server;

    // start winsock
    int ret = WSAStartup(0x101,&wsaData); // use highest version of winsock avalible
   
    if(ret != 0) {
         return 0;
    }

    // fill in winsock struct ... 
    server.sin_family=AF_INET; 
    server.sin_addr.s_addr=INADDR_ANY; 
    server.sin_port=htons(2363);
    // create our socket
    sock=socket(AF_INET,SOCK_STREAM,0); 
    
    std::cout << inet_ntoa(server.sin_addr);

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

    // bind our socket to a port(port 123) 
    if( bind(sock,(sockaddr*)&server,sizeof(server)) !=0 ) {
         return 0;
    }

    // listen for a connection  
    if(listen(sock,5) != 0) {
         return 0;
    }
    // socket that we snedzrecv data on
    SOCKET client;
    sockaddr_in from;
    int fromlen = sizeof(from); 

    // loop forever 
    while(true) { 
         // accept connections
         client = accept(sock,(struct sockaddr*)&from,&fromlen);
         //ect...


Client Section (setting up the client and connection):
(I also got this from a tutorial)
//ect...
    //Declarations
    DWORD poll;
    int res,i=1,port=999;
    char buf[100];
    char msg[100] = "";
    char ip[15];
    WSADATA data;

    signal(SIGINT,s_handle);
    signal(SIGKILL,s_handle);
    signal(SIGQUIT,s_handle);

    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(2363);              //Set the port
    ser.sin_addr.s_addr=inet_addr("IPv4 Address (From the server router)");      //Set the address we want to connect to

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

    sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);       //Create the socket

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

    res=connect(sock,&addr,sizeof(addr));               //Connect to the server
    if(res !=0 ) {
            gamerunning = false;
    } else {
            memcpy(&ser,&addr,sizeof(SOCKADDR));
    }
    //ect...

This works LAN. But not Via Internet.

Help???

Is This A Good Question/Topic? 0
  • +

Replies To: C++ and Winsock - Connecting Via Internet

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: C++ and Winsock - Connecting Via Internet

Posted 05 October 2013 - 04:58 PM

How did you verify that the port forwarding is working correctly?

How did you verify that your firewalls are truly disabled?
Was This Post Helpful? 0
  • +
  • -

#3 LunaRebirth  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 21-March 13

Re: C++ and Winsock - Connecting Via Internet

Posted 06 October 2013 - 05:08 PM

View PostSkydiver, on 05 October 2013 - 04:58 PM, said:

How did you verify that the port forwarding is working correctly?

How did you verify that your firewalls are truly disabled?


I did not.
I'm not sure how to check the port forwarding :o
But I did verify the firewalls are disabled. It asked me to allow or disable it when I first ran the program
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: C++ and Winsock - Connecting Via Internet

Posted 06 October 2013 - 09:03 PM

Just use telnet from the client end going to the server side. If the server establishes a connection (verify this by attaching a debugger on the server), then you know that the port forwarding is working.
Was This Post Helpful? 0
  • +
  • -

#5 LunaRebirth  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 21-March 13

Re: C++ and Winsock - Connecting Via Internet

Posted 08 October 2013 - 10:44 AM

I opened up my server, then opened Cmd and used telnet.
I opened the connection in which is listed on my client (with the same port) to connect it to my server.
Telnet works and the server finds a new connection.

So Telnet works, but the client doesn't connect to the server Via Internet? I tried telnet on the same PC, so I'm not sure if that's a problem.

The client connects to the server on LAN, but I want anyone to be able to connect from anywhere, not just LAN.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: C++ and Winsock - Connecting Via Internet

Posted 08 October 2013 - 07:03 PM

You need to run telnet from the client computer.
Was This Post Helpful? 0
  • +
  • -

#7 LunaRebirth  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 21-March 13

Re: C++ and Winsock - Connecting Via Internet

Posted 08 October 2013 - 08:00 PM

It did not work. The telnet didn't connect to the server.

I guess I did my port forwarding wrong??? But everything I look at on 'How-To's, I'm doing it the correct way.

STEPS I TOOK:
Sign into your router (192.168.0.1 in your address bar).
Sign in (Admin and Password as the UserName and Password).
"Port Forwarding/Triggering".
New port forward.
IPv4 (Listed in CMD/ipconfig) as the address.
The port is within the server.
Add the forward.
DONE.

Am I doing something wrong here?
Was This Post Helpful? 0
  • +
  • -

#8 LunaRebirth  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 21-March 13

Re: C++ and Winsock - Connecting Via Internet

Posted 10 October 2013 - 03:09 PM

I was looking at previous posts I had, And one of them was a link.
I checked out the site, and tried seeing if the port was open.
This was the link: Link
I'm obviously doing something wrong here :o

So I have a few questions on this.
When I go to my router to port forward, I'm putting in my IPv4 address found in CMD. And the port in which my server and client connect to.
Is this correct?

I also add this address for my client to connect to.

Is this correct?
Or am I supposed to put my IP found in whatsmyip.org?
If this is my problem, Which should I replace? The client to connect to? Or the port forwarding?
Was This Post Helpful? 0
  • +
  • -

#9 LunaRebirth  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 21-March 13

Re: C++ and Winsock - Connecting Via Internet

Posted 12 October 2013 - 12:57 PM

I don't want to give up on this project. But I'm still not finding the help I need.
I've done search like, "Port forwarding does not work", "Setting a port forward winsock", "Winsock online server", etc..
But nothing seems to work?!

I'm using Netgear as my router..

I seriously need help. Anyone???
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: C++ and Winsock - Connecting Via Internet

Posted 12 October 2013 - 01:15 PM

View PostLunaRebirth, on 10 October 2013 - 06:09 PM, said:

When I go to my router to port forward, I'm putting in my IPv4 address found in CMD. And the port in which my server and client connect to.
Is this correct?

Yes.

View PostLunaRebirth, on 10 October 2013 - 06:09 PM, said:

I also add this address for my client to connect to.

This is incorrect. Your client should be using the result of whatismyip.org from the server's network. When you are testing with telnet from your client, also enter this same address.
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,859
  • Joined: 25-December 09

Re: C++ and Winsock - Connecting Via Internet

Posted 12 October 2013 - 01:22 PM

When you try to connect from the internet you need to use the ip address that is visible on the internet. So you should be using the ip address assigned to you by your network provider. So yes you should be using the address provided by your link whatsmyip.org.

You need to have your router forward to your computer any information the router receives from the port in question. The port forwarding is primarily done in the router.

Also don't forget that some routers have built in firewalls and must be configured to allow connections to the port in question.


Jim
Was This Post Helpful? 0
  • +
  • -

#12 LunaRebirth  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 21-March 13

Re: C++ and Winsock - Connecting Via Internet

Posted 12 October 2013 - 01:56 PM

Okay what I did is gave my friend (in a different area+PC+router) my client project. The client connects to my Global IP (Found in whatsmyip), my server connects to INADDR_ANY, and my port forward is my IPv4 address (192.168.1.111). All of which connect to same port.

As far as the firewall on my router, is there a way to check if it's enabled or disabled?
I know that Telnet returned there was no connection on my IP on Port...
Even after it is port forwarded.

The protocol on my port forwarding is TCP/UDP (Just thought I'd include that)

This post has been edited by LunaRebirth: 12 October 2013 - 01:57 PM

Was This Post Helpful? 0
  • +
  • -

#13 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 678
  • View blog
  • Posts: 2,331
  • Joined: 31-December 10

Re: C++ and Winsock - Connecting Via Internet

Posted 12 October 2013 - 04:02 PM

Typically, you can access your router's settings by going to your localhost IP address in a browser:

Quote

192.168.1.1

You should be able to look on your router's website to get the exact info. But usually if you're successful, it will pop-up with a username/password dialog and the username and password usually default to admin and password or something similar. Again check your router's website or manual for more information.

For my router, I go to: 192.168.2.1

This post has been edited by vividexstance: 12 October 2013 - 04:03 PM

Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,859
  • Joined: 25-December 09

Re: C++ and Winsock - Connecting Via Internet

Posted 12 October 2013 - 04:47 PM

How are you actually connecting to the Internet? DSL? Cable? Modem?



Jim
Was This Post Helpful? 0
  • +
  • -

#15 LunaRebirth  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 130
  • Joined: 21-March 13

Re: C++ and Winsock - Connecting Via Internet

Posted 12 October 2013 - 08:53 PM

Netgear router
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3