Tym
10/19/2008 6:33:00 PM
I must admint - half of what you said went straight over my head! So I hit
google and still none the wiser - yet!
I've created a new app for this - a windows app rather than console as
before.
My main form is now:
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Public frmTCP As Form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Const portNumber As Integer = 24601
Dim PortListener As New TcpListener(portNumber)
Dim tcpClient As New TcpClient
Me.Show()
'Call ReverseGEOData(53.1, -2.1)
Application.DoEvents()
Try
PortListener.Start()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Do
Application.DoEvents()
txtCONNLog.Text &= "Waiting for connection: " & Now() & vbCrLf
Application.DoEvents()
tcpClient = PortListener.AcceptTcpClient()
Dim frmDATA As New frmPORTdata(tcpClient)
tcpClient = Nothing
Loop
End Sub
End Class
Which as you can see sends the tcpClient off to another form each time the
connection is made for the forn to deal with it.
My form2 routine works fine in taking the connection and getting te data
from it using more or less the same routine as before - just a basuic
question I guess, is this sufficient to handle things the way I want them
handled, with the possibility of being bombarded with connections all on the
same port?
Form2:
Public Sub New(ByVal newParameter As TcpClient)
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
tcpClient = newParameter
Dim sWORDS() As String
Dim sIMEI As String
Dim sMSG As String
Dim I As Integer
Dim dblLAT As Double
Dim dblLONG As Double
Dim dblBEARING As Double
Dim singSPEED As Single
Me.Show()
Application.DoEvents()
Try
' Get the stream
Dim networkStream As NetworkStream = tcpClient.GetStream()
' Read the stream into a byte array
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
Debug.Print("Receive buffer size = " & tcpClient.ReceiveBufferSize)
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Return the data received from the client to the console.
Dim clientData As String = Encoding.ASCII.GetString(bytes)
AddToFile (MyFile,clientData)
Catch ex As Exception
Console.WriteLine(ex.ToString)
txtCONdata.Text &= ex.ToString & vbCrLf
End Try
Me.Dispose()
End Sub
Public Sub AddToFile(ByVal sFILENAME As String, ByVal sTEXT As String)
Using sw As StreamWriter = New StreamWriter(sFILENAME, True)
sw.WriteLine(sTEXT)
sw.Close()
End Using
End Sub
End Class
"Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote in message
news:op.ui6lm2jn8jd0ej@petes-computer.local...
> On Fri, 17 Oct 2008 08:55:58 -0700, Tym <spamtrap@ictis.net> wrote:
>
>> After programming in VB.Net for a couple of years now, I'm taking my
>> first
>> steps in TCP transfers
>>
>> I have a device which reports back to me via GRPS and thanks to Google
>> I've
>> managed to adapt a suitable port listener to I can grab the data.
>>
>> My quesiton is, whct if I've got 1000 of these things reporting back all
>> on
>> the same port number... how do I ensure that all the data gets captured
>> from
>> all units?
>
> That depends on whether you want to handle 1000 at a time, or 1000 in
> sequence one at a time.
>
> If you just want to handle them one at a time, the main thing you need to
> change is to loop back to the call to AcceptTcpClient() after you're done
> reading from a client.
>
> If you want to handle all of the clients at the same time, you need to
> separate the client connection accepting logic from the client receiving
> logic. In the code you posted, you accept and receive data in the same
> method. To handle multiple clients at once, you need to have one part of
> the code that only deals with accepting clients. In that code, for each
> client you'll need to hand off the created client connection to another
> part of the code, which will deal with the actual communications with the
> client.
>
> The part of the code that deals with the actual communications can be
> written in a variety of ways, but the most appropriate approach in .NET
> would be to use the asynchronous programming model. If you want to
> continue using TcpClient, you can use the BeginRead() method on the stream
> you get from the TcpClient. Alternatively, you can call AcceptSocket()
> instead of AcceptTcpClient(), and on the Socket instance call
> BeginReceive() or ReceiveAsync() (the latter has somewhat better
> performance characteristics under certain conditions, but for only a 1000
> clients, either is probably fine).
>
> By using the asynchronous programming model, you allow the framework to
> manage the i/o threads, and you only have to code the callbacks to handle
> the completion of i/o. You can start arbitrarily many i/o operations, one
> at a time after each call to Accept...() returns, and then go right back
> to calling the Accept...() method again. A different thread will be used
> when the i/o operation actually completes.
>
> You can even handle the accept logic using the asynchronous programming
> model, and in fact that would generally be my preference. But if you only
> have the one server socket listening, that may be overkill.
>
> Pete