Chris.. I have a bit of egg on my face.
In the process of getting the code ready to post, I made a slight change.
In the code below I have a tr.sleep(1) in both the server and client. I would
have thought that this might limit the speed to around 500 transactions/sec
but in fact it is reduced to about 100 transactions per second. When I take
out the tr.sleep(1) statements, the transaction rate increases to about 1200
per second, much more acceptable.
However I wonder if having these threads working 100% will it slow down
other parts of the program.
Below are two programs. First is the TcpServer. This program waits for a
connection and merely echoes the message it receives.
The second program is the TcpClient. It makes a connection with the
TcpServer and then enters a loop that sends a message, waits for the response
and loops over and over. The message starts with a count so you can see how
fast the echoing is working.
Once everything is working, the key server code snippet is as follows..
Do While listening = True
Thread.Sleep(1)
receivedData = sr.ReadLine
If receivedData <> Nothing Then
returnedData = receivedData.ToUpper
sw.WriteLine(returnedData)
sw.Flush()
End If
Loop
The key client code snippit is.....
Do While Running = 1
'sw.WriteLine(txtSendData.Text)
sw.WriteLine(Format(cnt, "0") & txtSendData.Text)
cnt += 1
sw.Flush()
' Receive and display data.
result = sr.ReadLine()
txtReceiveData.Text = result
tr.Sleep(1)
Loop
****** Start of TcpServer *******
Imports System.IO
Imports System.net
Imports System.Net.Sockets
Imports System.Threading
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
'I did not include the windows generated code.. Hamil.
#End Region
Dim listening As Boolean
Dim localhostAddress As IPAddress
Dim port As Integer
Dim tcpList As TcpListener
Dim tcpCli As TcpClient
Dim ns As NetworkStream
Dim sr As StreamReader
Dim receivedData As String
Dim returnedData As String
Dim sw As StreamWriter
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles butStart.Click
butStart.Enabled = False
butStop.Enabled = True
Dim tr As New Thread(AddressOf ListenToClients)
tr.Start()
End Sub
Sub ListenToClients()
localhostAddress = IPAddress.Parse("192.168.1.20") '.Loopback
port = CInt(txtPort.Text)
tcpList = New TcpListener(localhostAddress, port)
tcpList.Start()
listening = True
Do While listening
Do While tcpList.Pending = False And listening = True
Thread.Sleep(10)
Loop
If Not listening Then Exit Do
Dim tcpCli As New TcpClient
tcpCli = tcpList.AcceptTcpClient()
ns = tcpCli.GetStream
sr = New StreamReader(ns)
sw = New StreamWriter(ns)
tbStatus.Text = "connected"
Do While listening = True
Thread.Sleep(1)
receivedData = sr.ReadLine
If receivedData <> Nothing Then
returnedData = receivedData.ToUpper
sw.WriteLine(returnedData)
sw.Flush()
End If
Loop
sr.Close()
sw.Close()
ns.Close()
tcpCli.Close()
tbStatus.Text = "closed"
Loop
tcpList.Stop()
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles butStop.Click
butStart.Enabled = True
butStop.Enabled = False
listening = False
End Sub
Private Sub butStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles butStart.Click
End Sub
Private Sub butExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles butExit.Click
End
End Sub
End Class
******* End of TcpServer ********
******* Start of TcpClient *******
Imports System.IO
Imports System.Net.Sockets
Imports System.Threading
#Region " Windows Form Designer generated code "
'I did not include this code. Hamil.
#End Region
Public Class TCPClientForm
Inherits System.Windows.Forms.Form
Dim host As String
Dim port As Integer
Dim tcpCli As TcpClient
Dim ns As NetworkStream
Dim sw As StreamWriter
Dim sr As StreamReader
Dim result As String
Dim cnt As Integer = 0
Dim tr As Thread
Dim Running As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
host = txtHost.Text
port = CInt(txtPort.Text)
tcpCli = New TcpClient(host, port)
ns = tcpCli.GetStream
' Send data to the server.
sw = New StreamWriter(ns)
sr = New StreamReader(ns)
sw.WriteLine(txtSendData.Text)
sw.Flush()
' Receive and display data.
result = sr.ReadLine()
txtReceiveData.Text = result
sr.Close()
sw.Close()
ns.Close()
End Sub
Private Sub butConnect_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles butConnect.Click
host = txtHost.Text
port = CInt(txtPort.Text)
tcpCli = New TcpClient(host, port)
ns = tcpCli.GetStream
' Send data to the server.
sw = New StreamWriter(ns)
sr = New StreamReader(ns)
End Sub
Private Sub butSend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles butSend.Click
'sw.WriteLine(txtSendData.Text)
sw.WriteLine(Format(cnt, "0") & txtSendData.Text)
cnt += 1
sw.Flush()
' Receive and display data.
result = sr.ReadLine()
txtReceiveData.Text = result
End Sub
Private Sub butClose_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles butClose.Click
sr.Close()
sw.Close()
ns.Close()
End Sub
Private Sub butStartFast_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles butStartFast.Click
Dim tr As New Thread(AddressOf SendFast)
tr.Start()
End Sub
Sub SendFast()
Running = 1
Do While Running = 1
'sw.WriteLine(txtSendData.Text)
sw.WriteLine(Format(cnt, "0") & txtSendData.Text)
cnt += 1
sw.Flush()
' Receive and display data.
result = sr.ReadLine()
txtReceiveData.Text = result
tr.Sleep(1)
Loop
End Sub
Private Sub butStopFast_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles butStopFast.Click
Running = 0
End Sub
Private Sub butExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles butExit.Click
End
End Sub
End Class
******** End of TcpClient ***********
"Chris, Master of All Things Insignifican" wrote:
I'm not sure about the speed difference "should" be but if the code is
simple enough, post it and we can see if you are doing something that could
be slowing it down.
Chris
"hamil" <ha***@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...I wrote a VB.net "echo" program using the Tcp client/listener class to
determine how fast the transactions would occur. I got a speed of about
160
echoes per second. A friend did the same test using Linux/C++ and
reported
3000 echoes/second. Is my number typical, or am I doing something wrong?
How
fast should I expect an echo test to run?
Thanks, Hamil.