GPRS Web服务请求 [英] GPRS Web service Request

查看:77
本文介绍了GPRS Web服务请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我正在开发车队管理Web应用程序,我有很多GPS,它们正在将数据包发送到我的网站.

因此,我应该有一个侦听器来获取此数据包.

我写了下面的课,但是太弱了.有时听见,有时听不到.

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.

推荐答案

数据包?您确定不应该使用UDP来解决此问题吗?在我看来,定位器希望将不频繁的即发即弃"消息发送到您的服务器-正是UDP的目的.立即关闭创建的TCP连接这一事实强烈表明,您需要一个非持久连接协议.

至于您的代码实际上出了什么问题,如果您想坚持使用TCP:

  • 您正在阻止流.在接受连接的同一线程上进行读取.这意味着,如果在您正在等待来自另一个服务器的数据时定位器尝试连接到服务器,则它将失败(此时您不接受线程).您应该使用异步方法,或者至少将数据读取与接受分开放入单独的线程中.请参阅我的套接字库中的服务器,以获取使用异步方法适当处理接收和接收的方法(但请注意,接收是在不同的类中完成的).现在,您的服务器一次只能处理一个连接 ...根本无法扩展.
  • 您假设对Read的单个调用返回了将从该源发送的所有数据.即使您在一次呼叫中发送它,也不一定是这种情况(我认为,即使数据大小在数据包传输限制之内,也可以保证,即使数据大小超过了数据包传输限制,也不能保证.)我建议关闭连接从发件人的角度来看,如果您想要一劳永逸的操作,则服务器可以假定已关闭的连接已发送了所有数据.(如果客户端需要确认,则不起作用,但是您可以这样做)现在不这样做.)
  • 如果收到SocketException,则关闭整个服务器,最有可能在单个客户端的Read中发生.您需要两个陷阱,一个陷阱位于Accept位置,一个陷阱位于Receive/Close位置,第一个陷阱可以关闭服务器,但是第二个陷阱只能关闭该客户端.
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.


这篇关于GPRS Web服务请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆