Testing client server application from single computer

J

Joh

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 = 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 = 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 = tcpClient.GetStream()

Dim responseString As String = "Connected to server."
Dim sendBytes As [Byte]() =
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 = New System.Net.Sockets.TcpClient
tcpClient.Connect(HostName, 8000)
networkStream = 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 = 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
 
M

Mr. Arnold

Joh said:
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?

You put the client on one machine on your LAN. You put the server on another
machine on the LAN. You set rules with the personal FW to open the ports
needed for the listening server machine or drop the PFW.
 
T

Tom Shelton

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 = 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 = 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 = tcpClient.GetStream()

Dim responseString As String = "Connected to server."
Dim sendBytes As [Byte]() =
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 = New System.Net.Sockets.TcpClient
tcpClient.Connect(HostName, 8000)
networkStream = 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 = 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?
 
J

Joh

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 = 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 = 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 = tcpClient.GetStream()
Dim responseString As String = "Connected to server."
Dim sendBytes As [Byte]() =
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 = New System.Net.Sockets.TcpClient
tcpClient.Connect(HostName, 8000)
networkStream = 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 = 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?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top