简体   繁体   English

具有两个不同的扫描程序和两个不同的BufferedReader的相同TCP套接字(客户端)

[英]Same TCP Socket (client-side) with two different scanners and two different BufferedReader

how can I download strings with two different objects BufferedReader and two different objects Scanner from a single socket with two different threads? 如何从具有两个不同线程的单个套接字中下载包含两个不同对象BufferedReader和两个不同对象Scanner的字符串? I already tried this solution below but myReader1, after reading from Socket, is with invalid characters how can I fix? 我已经在下面尝试过此解决方案,但是从套接​​字读取后,myReader1包含无效字符,该如何解决? You might suggest a workaround? 您可能会建议解决方法? Thanks a lot in advance 提前谢谢

    //Socket TCP declaration
   InetAddress serverAddr = InetAddress.getByName(IP);

   try
   {
       Socket mySocket  = new Socket(serverAddr, PORT);
   }
   catch(Exception e)
   {
      e.printStackTrace();
   }


    //Thread 1
   Thread t1 = new Thread(new Runnable()
   {
     @Override
     public void run() 
     {
        BufferedReader myReader1 = new BufferedReader(new InputStreamReader(mySocket.getInputStream(), "UTF-8"));

        Scanner myScanner1 = new Scanner(myReader1).useDelimiter("\0");

       synchronized(mySocket)
       {
         while(mySocket.isConnected() && myScanner1.hasNext())
         {
            String s = myScanner1.next();
         }
       }
    }
 });

  Thread t2 = new Thread(new Runnable()
  {
    @Override
    public void run() 
    {
      BufferedReader myReader2 = new BufferedReader(new InputStreamReader(mySocket.getInputStream(), "UTF-8"));

      Scanner myScanner2 = new Scanner(myReader2).useDelimiter("\0");

      synchronized(mySocket)
      {
         while(mySocket.isConnected() && myScanner2.hasNext())
         {
            String s = myScanner2.next();
         }
      }
  }
});

 t1.start();
 t2.start();

how can I download strings with two different objects BufferedReader and two different objects Scanner from a single socket with two different threads? 如何从具有两个不同线程的单个套接字中下载包含两个不同对象BufferedReader和两个不同对象Scanner的字符串?

You can't . 你不能。

You will never get this to work. 您将永远无法使用它。 Because of aspects of TCP you can't control, the data will arrive in unpredictable quanta, and the BufferedReaders will steal it from each other in unpredictable ways. 由于您无法控制TCP的某些方面,数据将以不可预测的数量到达,而BufferedReaders将以不可预测的方式相互窃取数据。 Ditto the Scanners themselves if they have internal buffering. 如果Scanners具有内部缓冲,则同上。

In any case what you are trying to do has no actual meaning. 无论如何,您试图做的事情没有实际意义。 What are you expecting the two threads to do that a single thread won't? 您期望两个线程执行单个线程不会执行的操作? If for example you're expecting them to read alternately, they won't. 例如,如果您期望他们交替阅读,则不会。 If you're expecting to be able to confidently send one string intended for thread 1 and another string intended for thread 2, you can't. 如果您希望能够自信地发送一个用于线程1的字符串和另一个用于线程2的字符串,那么您就不能这样做。 It is impossible. 是不可能的。

I already tried this solution below but myReader1, after reading from Socket, is with invalid characters how can I fix? 我已经在下面尝试过此解决方案,但是从套接​​字读取后,myReader1包含无效字符,该如何解决? You might suggest a workaround? 您可能会建议解决方法?

You won't succeed in getting anything working at all along the lines you've started on, but the basis of any solution must be a single Scanner and a single BufferedReader per socket. 您一开始就无法成功完成任何工作,但是任何解决方案的基础都必须是每个套接字一个Scanner和一个BufferedReader If you must have two threads you will need two sockets, with a BufferedReader and Scanner each. 如果必须有两个线程,则将需要两个套接字,每个套接字都有一个BufferedReaderScanner

try
{
    Socket mySocket  = new Socket(serverAddr, PORT);
}
catch(Exception e)
{
      e.printStackTrace();
}

Don't write code like this. 不要写这样的代码。 First, you should only be catching IOException. 首先,您应该只捕获IOException. Second, all the code that depends on the success of this code should be logically inside this try block. 其次,所有依赖于此代码成功的代码都应在逻辑上放在该try块内。 It should be impossible for any code anywhere to access the mySocket variable unless this code has succeeded. 除非该代码成功,否则任何地方的任何代码都不可能访问mySocket变量。

while(mySocket.isConnected() && myScanner1.hasNext())

The isConnected() test is pointless here. isConnected()测试在这里毫无意义。 It will never return false. 它永远不会返回false。 It doesn't magically become false when the peer disconnects. 当对等方断开连接时,它不会神奇地变为假。

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

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