Q:

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.  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
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. 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?

A:

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.

