473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 1584

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

67
by: Mike MacSween | last post by:
I've got a SQL Server database. Nearly finished. It's going to go on a single non networked machine. One day somebody might get access to it over ADSL (probably TS), but for now it's a single user...
0
by: Ken Allen | last post by:
Most of .Net seems to be focused on the concept of multiple clients and a single server that provides the control. All of the examples that I have seen presume that the client code knows the...
2
by: Naveen Mukkelli | last post by:
Hi, I'm writing a client/server application using C#. The server accepts connecitons asynchronously, using BeginAccept/EndAccept. Is there any way we can write some unit tests(NUnit) to test...
5
by: wrytat | last post by:
I'm not sure if I'm posting the correct place. I posted it somewhere else, but someone told me to post it at another place. Anyway, some background first. I am currently building a web...
5
by: B1ackwater | last post by:
We've fooled around with Access a bit, but only using the single-user store-bought version. It seems to be a good database - versatile and infinitely programmable - and can apparently be used as a...
2
by: WhatHappend | last post by:
I have converted a .Net 1.0 application to .Net 2.0 and the web service invocations have delay of around 10seconds on each intial access. After the first access subsequent access are fast (After a...
2
by: Wimpie van Lingen | last post by:
Hey I have some more questions with regards to Remoting in .NET 2. I'm using TCP with the Binary formatter. My solution consists of 4 projects: - Class Library containing the server classes...
22
by: Dan Rumney | last post by:
Hi all, I've been writing Javascript for quite a while now and have, of late, been writing quite a lot of AJAX and AJAX-related code. In the main, my dynamically generated pages are created...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.