TCP connection over WAN

  • 12 years ago

    Hi guyz,

     I'm trying to make a program that can send simple data back and forth between 2 computers over the the internet... At the moment i can only get it to send data over the LAN, i have setup my router to forward all incoming connections to a specific port to my computer but still no luck... anyone have any hints, here is the source code for the server and client...

     

     SERVER CODE:-

    Try

    Listener = New TcpListener(IPAddress.Parse("127.0.0.1"), 22667) rchMessageBox.Text += "Starting IP Listener..." & vbCrLf

    m_terminate = False

    Listener.Start()

    Listener.BeginAcceptTcpClient(
    AddressOf ConnectionHandler, Nothing)

    rchMessageBox.Text += "Server Awaiting Connections. Press Cancel to stop server." & vbCrLf

    Catch ex As Exception rchMessageBox.Text += "Could not start IP Listener" & vbCrLf

    End Try

     

    CLIENT CODE:-

    Using client As New TcpClient Me.rchMessageBox.Text += "Attempting to connect to server on port 22666..." & vbCrLf

    Try

    client.Connect(IPAddress.Parse("82.10.xxx.xxx"), 22667)

    Catch ex As Exception

    Me.rchMessageBox.Text += "Error Connecting..." & vbCrLf

    Exit Sub

    End Try

    Using writer As New BinaryWriter(client.GetStream)

    writer.Write("ConNecT")

    Using reader As New BinaryReader(client.GetStream)

    If reader.ReadString = "OK" Then

    Me.rchMessageBox.Text += "Connection Established..." & vbCrLf writer.Write("ReqUesT")

    If reader.ReadString = "FILE?" Then

    Me.rchMessageBox.Text += "Connection Established..." & vbCrLf

    writer.Write(txtFile.Text)

    Dim filesize As Integer = Integer.Parse(reader.ReadString())

    For i As Integer = 1 To filesize

    Me.rchMessageBox.Text += Chr(client.GetStream.ReadByte)

    Next

    Me.rchMessageBox.Text += vbCrLf & "Disconnecting..." & vbCrLf writer.Write("DisConNecT")

    End If

    Else

    Me.rchMessageBox.Text += "Connection not complete" & vbCrLf

    End If

    End Using

    End Using

    End Using

     

    I have left out all the threaded code because there is no problem with this, only the connecting...

    This is in vb.net 2005

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer Science is no more about computers than astronomy is about telescopes.” - E. W. Dijkstra