从 gprs 连接接收数据的延迟

发布于 2024-11-15 23:46:04 字数 985 浏览 3 评论 0原文

我正在用 C# 编写一个解析器。它接收并发送一个最大长度= 100的数据包。然而,每个数据包之间(接收和发送)的时间间隔超过 2 秒。不知道是我的实现问题还是gprs方面的问题。

服务器监听:

tcpListener = new TcpListener(IPAddress.Any, Names.comPort);
tcpListener.Server.NoDelay = true;

while (true)
{
    TcpClient client = tcpListener.AcceptTcpClient();

    Thread th = new Thread(new ParameterizedThreadStart(HandleComm));
    th.IsBackground = true;
    th.start(client);
}

开始接收数据:

HandleComm(object client)
{
 NetworkStream clientStream = ((TcpClient)client).GetStream();

 while (true)
 {
     try { bytesRead = clientStream.Read(buffer, 0, 200);}
     catch(Exception ex) {break;}        
 }
}

这是一个简单的实现,我收到第一个数据包的bytesRead的那一刻与第二个数据包相比大约是2s。我认为这太慢了。如何增加接收时间?我已经检查过收到数据并回复后,只花了1毫秒。

添加发送部分:

socket.NoDelay = true;
socket.SendTimeout = 200; // minimum time out
socket.SendBufferSize = bytes.Length;

socket.Send(bytes, SocketFlags.DontRoute);

I am writing a parser in C#. It receives and sends a packet with max length = 100. However, the time between (receiving and sending) after each packet is more than 2 seconds. I don't know whether it is a problem of my implementation or the gprs side.

Server listening:

tcpListener = new TcpListener(IPAddress.Any, Names.comPort);
tcpListener.Server.NoDelay = true;

while (true)
{
    TcpClient client = tcpListener.AcceptTcpClient();

    Thread th = new Thread(new ParameterizedThreadStart(HandleComm));
    th.IsBackground = true;
    th.start(client);
}

Begin receiving data:

HandleComm(object client)
{
 NetworkStream clientStream = ((TcpClient)client).GetStream();

 while (true)
 {
     try { bytesRead = clientStream.Read(buffer, 0, 200);}
     catch(Exception ex) {break;}        
 }
}

It is a simple implementation, the moment I receive the bytesRead of the first data packet compared to the second one is about 2s. It is too slow I think. How could I increase receiving time? I already checked after receiving data and replying, it took 1 ms only.

Adding sending part:

socket.NoDelay = true;
socket.SendTimeout = 200; // minimum time out
socket.SendBufferSize = bytes.Length;

socket.Send(bytes, SocketFlags.DontRoute);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文