Client/Server Problem with IP address

vadi

Member
Joined
Jan 14, 2008
Messages
7
Programming Experience
Beginner
Hi guys

My problem is i've created a client/server program using visual basic (vb.net 2005) and it works fine in local network but when i try to connect it to a remote computer (internet) then i get error and i can't send anything. Well i'm using a Router to connect to internet and i tryed the routers ip address and the computers but both don't work what is the problem i hope someone call help me.

Thanks in Advanced!!! :confused:
 
You have to connect to the server by it's public IP (http://www.whatismyip.org/), if this machine is behind a router you also have to configure port forwarding in order for the connection to be passed on from the router to correct host in your local network. Any Firewall present must allow this traffic to pass. If the server is connected to an ISP that give dynamic IP and disconnects you when idle you have to make sure server is connected and current IP is used, a Dynamic DNS service may help in such cases.
 
My code

VB.NET:
Imports System.Net.Sockets
Imports System.Threading
Imports System.IO

Public Class Form1

    Dim Listener As New TcpListener(65535)
    Dim Client As New TcpClient
    Dim Message As String = ""

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))

        ListThread.Start()
    End Sub

    Private Sub Listening()
        Listener.Start()
    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            If Listener.Pending = True Then
                Message = ""
                Client = Listener.AcceptTcpClient()

                Dim Reader As New StreamReader(Client.GetStream())
                While Reader.Peek > -1
                    Message = Message + Convert.ToChar(Reader.Read()).ToString
                End While

                rtbDisplay.AppendText(Message + vbNewLine)
            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Listener.Stop()
    End Sub




    Dim enterkey As Boolean


    Private Sub rtbMessage_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles rtbMessage.KeyDown
        If e.KeyCode = Keys.Enter And e.Control = False Then


            If rtbMessage.TextLength > 0 Then
                Client = New TcpClient("190.168.1.64", 65535)

                Dim Writer As New StreamWriter(Client.GetStream())
                Writer.Write(rtbMessage.Text)
                Writer.Flush()
            End If



            rtbMessage.Clear()
            'This variable sets the EnterKey to True indicating that the EnterKey pressed
            enterkey = True
        End If

        ' Ctrl + Enter Button
        If e.KeyCode = Keys.Control And e.KeyCode = Keys.Enter Then

        End If
    End Sub



    Private Sub rtbMessage_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles rtbMessage.KeyPress
        'If EnterKey is Pressed than do the following action
        If enterKey = True Then
            rtbMessage.SelectAll() 'Select all data in TextBox
            rtbMessage.Clear() 'Clear the TextBox
            rtbMessage.Focus() 'Get Focus on TextBox
        End If

        'This variable sets the EnterKey to False indicating that the EnterKey was not pressed
        enterKey = False
    End Sub
End Class
in the above code i use my routers address (the ip there is fake) and it is static ip so it never changes but it doesnt still work.
Thanks for your response!!!!
 
Last edited by a moderator:
So "190.168.1.64"(faked) is the IP you see in browser when you go to http://www.whatismyip.org/ ?

You have configured your router to forward calls to port 65535 to your private network IP? (this IP may be found in Network connections, status, support. Or with IpConfig command line tool. Also normally in router configuration displaying current connected hosts.)

Verified any firewalls open for TCP on this port? There may be FW in router, Windows, and other client FW you may have installed.

Btw, there isn't any point in your starting thread, all it does it call non-blocking Listener.Start() method and ends. You might as well do it in Load. Also close reader and client when done in Timer, same with writer and client when sent.
 
Thanks for your response!!!

I will correct the code as u said but again to the that problem u told me that i should use port forwarding at first but for example if u install LimeWire or skype on your computer u don't setup anything but they are working perfectly so maybe there is a way to do it programmatically cos otherwise how could these programs do it without changing any settings on my router. And some people are saying that programs such as MSN are solving the issue by using a server but how about LimeWire whic is P2P program. I'm really sorry for taking your time so much but i would really appriciate if u tell me the way this programs use to solve this issue cos even if they use a server the program they want to reach is still behind the Router so how come they solve the issue like that.

Thanks in advanced!!! :)
 
It is not possible to connect to a host behind a router without the router forwarding to it. All P2P apps I've used one had to do the same (port forwarding and firewall), else they would be passive; meaning no incomming connections, but inbound traffic only on a connection that was established out from client. Port forwarding may be configured automatically by an application through UPnP.
 
Thanks for your time and information given to me. Now i know atleast what i have to do to archieve what i want.:cool:
 
Back
Top