简体   繁体   English

两个线程可以同时使用同一套接字,这可能有问题吗?

[英]can two threads use the same socket at the same time, and are there possible problems with this?

Can two threads use the same socket at the same time? 两个线程可以同时使用同一套接字吗?

for example, first i open a socket that represents a connection between Android client and windows 7 server. 例如,首先我打开一个套接字,该套接字代表Android客户端与Windows 7服务器之间的连接。 i want to send an integer that is the size of the file by using DataOutputStream, then after this i will use BufferedOutputStream to send the binary file, such as a pdf 我想使用DataOutputStream发送一个等于文件大小的整数,然后在此之后,我将使用BufferedOutputStream发送二进制文件,例如pdf

the server will get the size sent by the client and then check if the size of the file received is the correct size. 服务器将获取客户端发送的大小,然后检查所接收文件的大小是否正确。 it compares the send integer, file size number with the actual size of the file received. 它将发送整数,文件大小编号与接收到的文件的实际大小进行比较。 if there is any difference than the server knows that the file is not complete and then it will send a message back to the client to request that the file be sent again. 如果存在任何差异,但服务器知道文件不完整,则它将向客户端发送一条消息,要求再次发送该文件。

i have an app where the file is received as incomplete about 5% of the time. 我有一个应用程序,其中大约5%的时间收到的文件不完整。 it arrives smaller than the actual size. 它到达小于实际大小。 the only way to fix this problem is to resend the file again if it is not complete 解决此问题的唯一方法是,如果文件不完整,请重新发送该文件

if i send a file, it is required to have a port open and waiting to receive a message back from the server telling the client if the file was received complete or has to be resent. 如果我发送文件,则需要打开端口,并等待从服务器接收回消息,告知客户端文件是否已完整接收或必须重新发送。

so is it possible to have one thread using the socket and listening for any messages and on another thread sending a message. 这样就有可能使一个线程使用套接字并侦听任何消息,而使另一个线程发送消息。 so that if a message is being received back from the server at the same time one is being sent. 因此,如果同时从服务器接收到一条消息,则正在发送一条消息。

it is clear that using only one thread will block and that two threads are needed but to use the same socket are there possible problems with this? 很明显,仅使用一个线程将阻塞,并且需要两个线程,但是使用同一套接字是否有可能出现此问题?

It is a very common pattern to have a dedicated reader thread and allow other threads to write to the same socket. 具有专用的读取器线程并允许其他线程写入同一套接字是一种非常常见的模式。

the problem for multiple readers or writers is fairly obvious. 对于多个读者或作家来说,这个问题是显而易见的。 What you read or write needs to make sense and so the actions on the socket need to be co-ordinated. 您读或写的内容必须有意义,因此套接字上的操作需要进行协调。 Often using a lock 经常使用锁

btw don't unwrap streams unless you like confusion. 顺便说一句,除非您喜欢混淆,否则请不要打开流。 If you want a DataOutputStream to wrap a BufferedOutputStream then only use the DOS for everything. 如果要让DataOutputStream包装BufferedOutputStream,则仅将DOS用于所有内容。

You can send data using one thread to a server and make another thread wait and listen for the response and do some stuff when the response is received. 您可以使用一个线程将数据发送到服务器,然后让另一个线程等待并侦听响应,并在收到响应时做一些事情。

If multiple threads are using the same socket for sending some data, there is a fair chance of data interleaving. 如果多个线程使用同一套接字发送某些数据,则很有可能发生数据交织。

You can use a single socket for sending data with multiple threads using thread locks (eg. Lock in Android & mutex in linux). 您可以使用单个套接字通过线程锁(例如,Android中的锁和Linux中的互斥锁)使用多个线程发送数据。

Hope this helps 希望这可以帮助

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

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