Click here to Skip to main content
15,920,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello

I'm working on a fleet management web application, I have a lot of GPSs which they're sending their packets to my website.

So, I should have a listener to get this packets.

I wrote the following class, but it's so weak. Sometimes It hears, sometimes not..

Imports System.Threading
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic

Public Class cls_GPRS_Business
    Private trd As Thread
    Private Mo_Positions As New cls_Positions_Business

    Public Sub ListenGPRS()
        Dim TCPserver As TcpListener
        TCPserver = Nothing
        Try
            ' Set the TcpListener on port 13000.
            TCPserver = New TcpListener(1688)

            ' Start listening for client requests.
            TCPserver.Start()

            ' Buffer for reading data
            Dim bytes(1024) As Byte
            Dim data As String = Nothing

            ' Enter the listening loop.
            Do
                ' Perform a blocking call to accept requests.
                ' You could also user server.AcceptSocket() here.
                Dim client As TcpClient = TCPserver.AcceptTcpClient()

                data = Nothing

                ' Get a stream object for reading and writing
                Dim stream As NetworkStream = client.GetStream()

                Dim i As Int32

                ' Loop to receive all the data sent by the client.
                i = stream.Read(bytes, 0, bytes.Length)

                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)

                Mo_Positions.InsertGPRS(data.Trim)

                client.Close()

                Thread.Sleep(100)
            Loop
        Catch e As SocketException
            'SaveTextFile(Server.MapPath("test.txt"), e.Message & vbCrLf)
        Finally
            TCPserver.Stop()
        End Try
    End Sub

    Public Sub StartGPRS()
        trd = New Thread(AddressOf ListenGPRS)
        trd.IsBackground = True
        trd.Start()
    End Sub

End Class



I need to have something Strong.

anyone can take me an idea??

with best regards.
Posted

1 solution

Packets? Are you sure you shouldn't be using UDP for this problem? It seems to me that the locators want to send infrequent 'fire and forget' messages to your server – exactly what UDP is designed for. The fact that you're immediately closing the TCP connection you create strongly implies that you want a non-persistent-connection protocol.

As for what's actually wrong with your code, if you want to persist with TCP:

  • You are blocking on stream.Read on the same thread as you accept connections. That means that if a locator attempts to connect to your server while you are waiting from data from another, it will fail (you are not accepting threads at that point). You should use the asynchronous methods, or at least put data reading into a separate thread from the acceptance. See the server in my sockets library for a way of handling accept and receive appropriately using asynchronous methods (but note that receive is done in a different class). Right now your server can only handle one connection at once ... not at all scalable.
  • You are assuming that a single call to Read returns all the data that will be sent from that source. Even if you send it in one call, this is not necessarily the case (even if the size of the data is under the packet transfer limit, I think – and if it is over, it is certainly not guaranteed. I recommend closing the connection from the sender's end, if you want a fire-and-forget – then the server can assume that a closed connection has sent all its data. (That doesn't work if a client requires an acknowledgement but you're not doing that now.)
  • You close the whole server if you get a SocketException, which is most likely to occur in Read for a single client. You want two traps, one around Accept and one around Receive/Close, the first can close the server but the second should only close that client.
 
Share this answer
 
Comments
vahid_gian 16-Aug-11 3:21am    
dear BobJanova,
firstly, thank u for your response,

I'm new to the network connections like this, and now working on UDP, any idea where to start?? have any samples?
BobJanova 16-Aug-11 16:38pm    
Try looking up UdpClient in the framework docs and starting from there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900