472,101 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,101 software developers and data experts.

Testing client server application from single computer

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

Oct 31 '07 #1
3 1483

"Joh" <me************@hotmail.comwrote in message
news:11**********************@v3g2000hsg.googlegro ups.com...
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.

Oct 31 '07 #2
On Oct 31, 4:14 pm, Joh <meanmachine...@hotmail.comwrote:
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?

--
Tom Shelton

Oct 31 '07 #3
Joh
On 1 Nov, 00:15, Tom Shelton <tom_shel...@comcast.netwrote:
On Oct 31, 4:14 pm, Joh <meanmachine...@hotmail.comwrote:


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?

--
Tom Shelton- Dölj citerad text -

- Visa citerad text -
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.

Nov 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

67 posts views Thread by Mike MacSween | last post: by
reply views Thread by Ken Allen | last post: by
2 posts views Thread by Naveen Mukkelli | last post: by
5 posts views Thread by wrytat | last post: by
2 posts views Thread by WhatHappend | last post: by
2 posts views Thread by Wimpie van Lingen | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.