Re: Testing client server application from single computer

by Joh on 10/31/2007 5:32:00 PM On 1 Nov, 00:15, Tom Shelton <tom_shel...@comcast.net> wrote:
> On Oct 31, 4:14 pm, Joh <meanmachine...@hotmail.com> wrote:
>
>
>
>
>
> > I'd like to test a server application and a client application from
> > the same computer. Currently if I let the client try to connect to my
> > IPv4 adress provided by my router in this case 192.168.1.2 or my
> > "external" IP provided by my ISP the tcplistener in the Server class
> > wont accept the connection. Can I do this in another way and make it
> > work?
>
> > Server application source:
>
> > Imports System.Net.Sockets
> > Imports System.Text
> > Public Class CServer
> > Const portNumber As Integer =3D 8000
> > Public tcpListener As New TcpListener(portNumber)
> > Public tcpClient As New System.Net.Sockets.TcpClient()
> > Public networkStream As NetworkStream
> > Public Function Connect() As Boolean
> > ' Must listen on correct port- must be same as port client
> > wants to connect on.
> > Try
> > tcpListener.Start()
>
> > ' Console.WriteLine("Waiting for connection...")
> > Dim tcpClient As TcpClient =3D tcpListener.AcceptTcpClient()
> > Catch e As Exception
> > Debug.Print("Failed to connect. " + e.Message)
> > Return False
> > End Try
> > Return True
> > End Function
> > Public Function Write(ByVal Message As String) As Boolean
> > Try
> > networkStream =3D tcpClient.GetStream()
>
> > Dim responseString As String =3D "Connected to server."
> > Dim sendBytes As [Byte]() =3D
> > Encoding.ASCII.GetBytes(responseString)
> > networkStream.Write(sendBytes, 0, sendBytes.Length)
> > Console.WriteLine(("Message Sent /> : " + responseString))
> > 'Any communication with the remote client using the
> > TcpClient can go here.
> > 'Close TcpListener and TcpClient.
> > tcpClient.Close()
> > tcpListener.Stop()
> > Console.WriteLine("exit")
> > Console.ReadLine()
> > Catch ex As Exception
>
> > End Try
> > End Function
>
> > End Class
> > ' Calling Server class
> > Public Class Form1
>
> > Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> > System.EventArgs) Handles MyBase.Load
> > Dim server As New CServer
> > server.Connect()
> > server.Write("Test")
>
> > End Sub
> > End Class
>
> > Client application source:
>
> > Imports System.Net.Sockets
> > Imports System.Text
> > Public Class CClient
>
> > Public tcpClient As System.Net.Sockets.TcpClient
> > Public networkStream As NetworkStream
>
> > Public Function Connect(ByVal HostName As String) As Boolean
> > Try
> > tcpClient =3D New System.Net.Sockets.TcpClient
> > tcpClient.Connect(HostName, 8000)
> > networkStream =3D tcpClient.GetStream()
> > Catch ex As Exception
> > Return False
> > End Try
> > Return True
> > End Function
> > Public Function Read(ByRef ReadData As String) As Boolean
> > Try
> > If networkStream.CanRead Then
> > ' Read the NetworkStream into a byte buffer.
> > Dim bytes(tcpClient.ReceiveBufferSize) As Byte
> > networkStream.Read(bytes, 0,
> > CInt(tcpClient.ReceiveBufferSize))
> > ' Output the data received from the host to the
> > console.
> > ReadData =3D Encoding.ASCII.GetString(bytes)
> > End If
> > ' pause so user can view the console output
> > Console.ReadLine()
> > Catch ex As Exception
> > Return False
> > End Try
> > Return True
> > End Function
> > End Class
>
> > 'Calling client class
> > Public Class Form1
>
> > Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> > System.EventArgs) Handles MyBase.Load
> > Dim client As New CClient
> > client.Connect("192.168.1.2")
> > Dim data As String
> > client.Read(Data)
> > End Sub
> > End Class
>
> Do you have the firewall on?
>
> --
> Tom Shelton- D=F6lj citerad text -
>
> - Visa citerad text -

Hi Tom,
Yes, I have a firewall on my router and Windows firewall. I shut down
the W=EDndows firewall but still can't connect.