[Socket] Connect locally = True Connect over network = false?

Andi03

Freshman
Joined
Sep 5, 2003
Messages
43
[Socket] Client Connect to Local server but not to remote server...

Hi.

Im making a little server application just to get to know a little about TCPListener and TCP Client.

I found some working code on som page on the net and I have one tiny little problem (Zip attached). I made to application On Server and one Client. The problem, i think, is in the server application. I can connect to the server with my client app locally on my computer but when I moved the server application to another computer (home network) and now I cant connect with my client. It works locally but not over my network. Any ideas what can cause this? I've checked our firewalls and all i OK there (Full Rights).

Any ideas?
 

Attachments

Last edited:
Any chance you could give a bit more information?
When you say it doesn't work what actually happens?
If you step through the code (server and client client) is ther ea particular line it fails or hangs on?
What code does the client application use to connect to the server? Are you using the correct address / port?
 
Ok...Here I go. I get no error messages and im connecting to ther right adress. I tried to connect to another remote adress and requested a webpage and that worked.

Here is the code that I use to connect:
Code:
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports System.Text
Imports System.Drawing
Imports System.Drawing.Imaging

    Private ClientSocket As Socket
    Private ASCII As New System.Text.ASCIIEncoding
    Private DevMode As Boolean = True

*** Skiped: Windowsorm Designer generated code ***

    Private Sub btnCoonnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCoonnect.Click
        Dim Addr As IPAddress = Dns.Resolve(txtIP.Text).AddressList(0)
        If Not Addr Is Nothing Then
            'Create an ip endpoint
            Dim EP As New IPEndPoint(Addr, CInt(txtPort.Text))
            ClientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            'Connect
            ClientSocket.BeginConnect(EP, AddressOf ConnectCallback, Nothing)
            SendTimeRequest
        End If
    End Sub

    Private Sub SendTimeRequest()
        Try
            Dim bytes() As Byte = ASCII.GetBytes("GETTIME" & vbCrLf)
        Catch ex As Exception
            If DevMode Then MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ConnectCallback(ByVal ar As IAsyncResult)
        Try
            ClientSocket.EndConnect(ar)
            Dim dlg As New NoParamsDelegate(AddressOf ConnectedUI)
            Me.Invoke(dlg)

            'Begin recieving data
            Dim bytes(8192) As Byte
            ClientSocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, AddressOf RecieveCallback, bytes)
        Catch ex As Exception
            If DevMode Then MsgBox(ex.Message)
        End Try

    End Sub

    Private Delegate Sub NoParamsDelegate()

    Private Sub ConnectedUI()
        Try
            Me.btnCoonnect.Enabled = False
            Me.txtIP.Enabled = False
            Me.txtPort.Enabled = False
        Catch ex As Exception
            If DevMode Then MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub DisconnectedUI()
        Try
            Me.btnCoonnect.Enabled = True
            Me.txtIP.Enabled = True
            Me.txtPort.Enabled = True
        Catch ex As Exception
            If DevMode Then MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub RecieveCallback(ByVal ar As IAsyncResult)
        Try
            Dim bytes() As Byte = CType(ar.AsyncState, Byte())
            Dim numbytes As Int64 = ClientSocket.EndReceive(ar)

            If numbytes = 0 Then
                ClientSocket.Shutdown(SocketShutdown.Both)
                ClientSocket.Close()
                Dim dlg As New NoParamsDelegate(AddressOf DisconnectedUI)
                Me.Invoke(dlg)
            Else
                Dim Recv As String = ASCII.GetString(bytes, 0, numbytes)
                'clear array
                Array.Clear(bytes, 0, bytes.Length)
                'show UI
                Dim dlg As New OneStringDelegate(AddressOf DisplayRevievedData)
                Dim args() As Object = {Recv}
                Me.Invoke(dlg, args)
                'begin recieving again
                ClientSocket.BeginReceive(bytes, 0, bytes.Length, SocketFlags.None, AddressOf RecieveCallback, bytes)
            End If
        Catch ex As Exception
            If DevMode Then MsgBox(ex.Message)
        End Try

    End Sub

    Private Delegate Sub OneStringDelegate(ByVal data As String)

    Private Sub DisplayRevievedData(ByVal data As String)
        Try

	    MsgBox(Data)

        Catch ex As Exception
            If DevMode Then MsgBox(ex.Message)
        End Try

    End Sub
I havent commented any code because I found it on the net. This is the imports, Variables and the subs/function I use to connect. (Left out the windows design generated code). It just wont connect, I get now error message.

//Andi
 
Back
Top