By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,760 Members | 1,644 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,760 IT Pros & Developers. It's quick & easy.

Testing client server application from single computer

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a

"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

P: n/a
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

P: n/a
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.