简体   繁体   English

当我们执行networkstream.write()时会发生什么?

[英]What happens when we do networkstream.write()?

I have 2 programs. 我有2个程序。 1 server and 1 client. 1个服务器和1个客户端。

In the client it goes something like this: 在客户端,它是这样的:

while(statement)
{
    networkstream.write(data);
}

And in the server it goes something like this: 在服务器中,它是这样的:

while(statement)
{
    while(statement)
    {
        ReceiveData;
    }

    Do other stuff;
}

So, while the client can write to the network stream really fast, the server still has to attend to the data before he can read some more. 因此,尽管客户端可以真正快速地写入网络流,但是服务器仍必须先处理数据,然后才能读取更多内容。

What happens when the client has already made 4 laps of the loop containing the write, while the server has still only read 1 time for example. 例如,当客户端已经进行了4圈包含写操作的循环,而服务器仍只读取1次时,会发生什么情况。

Is there a way of letting the client know when he can make another write? 有没有一种方法可以让客户知道他什么时候可以再写一次? And also what happens when the client make several '.write'? 当客户端进行多个“ .write”操作时,还会发生什么? does the server keep them all and reads them all or does the data that has been sent get overwriten? 服务器会保留所有内容并全部读取它们,还是已发送的数据被覆盖?

Hopefully you can understand my question. 希望你能理解我的问题。 Edit the question title if you desire. 如果需要,请编辑问题标题。

There is a whole stack of layers between your .write and the actual wires. .write和实际的导线之间有一整层堆栈。 The layers do all kinds of stuff, from error correction to making sure that your network traffics does not collide with others etc.; 这些层执行各种工作,从纠错到确保您的网络流量不与其他流量冲突等。 non the least, they provide you with buffering and blocking (if you sent too much or - on the receiving side - there is no data yet). 至少,它们为您提供缓冲和阻止功能(如果您发送的数据过多或-在接收方-尚无数据)。

To answer your question, somewhere along the line will be a buffer (a chunk of memory) where your bytes are written to. 为了回答您的问题,在线的某个地方将是一个缓冲区(一块内存),您的字节被写入该缓冲区。 Until they are sent along to the next stop along the way, it will block (wait/hang...). 直到它们被发送到途中的下一站,它都会阻塞(等待/挂起...)。 These calls will travel down the stack, up on the receiving side, and then down the other side and up yours again. 这些呼叫将沿着堆栈向下移动,在接收侧向上移动,然后在另一侧向下移动,然后再次向上移动。 So when your write returns, you can be reasonably sure that all is well, unless you get back an error code, exception or however your error handling works. 因此,当您的write返回时,可以合理地确定一切都很好,除非您返回错误代码,异常或执行错误处理。

If the server is slow when processing your requests, it will not get to read as quick as you'd like, so the buffers between you and them will fill up, at which point writing stops. 如果服务器在处理您的请求时速度很慢,那么read速度将达不到您的期望,因此您和它们之间的缓冲区将被填满,此时写入将停止。

There are plenty of different buffers on different layers as well, a full answer would be pretty complex. 在不同的层上也有许多不同的缓冲区,完整的答案将非常复杂。 https://en.wikipedia.org/wiki/OSI_model https://zh.wikipedia.org/wiki/OSI_model

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM