简体   繁体   中英

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? I already tried this solution below but myReader1, after reading from Socket, is with invalid characters how can I fix? 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?

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. Ditto the Scanners themselves if they have internal buffering.

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. It is impossible.

I already tried this solution below but myReader1, after reading from Socket, is with invalid characters how can I fix? 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. If you must have two threads you will need two sockets, with a BufferedReader and Scanner each.

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. Second, all the code that depends on the success of this code should be logically inside this try block. It should be impossible for any code anywhere to access the mySocket variable unless this code has succeeded.

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

The isConnected() test is pointless here. It will never return false. It doesn't magically become false when the peer disconnects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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