在Socket.Send计时问题之后的Socket.Receive [英] Socket.Receive after Socket.Send timing issue

查看:121
本文介绍了在Socket.Send计时问题之后的Socket.Receive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个客户端 - 服务器应用程序,其中每个客户端都在一个单独的线程上提供服务,我在运行程序时遇到了一些问题,我观察到如果客户端在服务器调用Socket.Receive之前调用Socket.Send,那么服务器块在Socket.Receive永远。

在调试模式下慢慢运行或在Socket上放一个Thread.Sleep。发送一切正常。



我的问题是:

1.为什么会这样?

2.如何解决?

I am working on an client-server application, where each client is served on a separate thread, i met some issues when running the program, i observed that if client calls Socket.Send before server calls Socket.Receive then server blocks on Socket.Receive forever.
When running slowly in debug mode or putting a Thread.Sleep over Socket.Send everything is working normally.

My questions are:
1. Why that happens ?
2. How can i fix it ?

推荐答案

问题不是足够的信息,所以我只是展示一些非常基本的考虑因素。



在调用相应的<$之前调用发送 c $ c>收到完全没问题。如果你试图阻止它,你可能会滥用技术。在时序方面,发送和接收操作是完全对称的。在数据可用或准备接收之前,可以调用它们中的每一个。这些操作中的每一个都将调用线程置于<等待状态,并且处于此状态不会浪费任何CPU时间:线程被关闭并且在被唤醒之前不会被调度执行。唤醒线程的一个事件是来自网络的硬件中断,因此等待非常有效。 (其他唤醒事件是线程中止,中断,超时等。)



这是技术的全部基础:网络执行线程的同步。此外,历史上套接字已被提出作为IPC工具(甚至在引入线程之前,套接字同步进程并在它们之间传递数据)。只有后来的套接字用于网络编程。



-SA
The question is not informative enough, so I'll just show some very basic considerations.

Calling Send before calling corresponding Receive is perfectly fine. If you are trying to prevent it, you are probably abusing technology. In terms of timing, sending and receiving operations are perfectly symmetric. Each of them can be called before data is available or is ready to be received. Each of those operations will put a calling thread in a wait state, and sitting in this state don't waste any CPU time: a thread is switched off and not scheduled for execution until it is awaken. One of the event waking up a thread is the hardware interrupt from the network, so the waiting is perfectly efficient. (Other waking up events are thread abort, interrupt, timeout, and so on.)

This is the whole basis of technology: network does the synchronization of the thread. Moreover, historically sockets have been put forward as an IPC facility (even before threads have been introduced, sockets synchronized processes and delivered data between them). Only later sockets were used for network programming.

—SA


这篇关于在Socket.Send计时问题之后的Socket.Receive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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