Results 1 to 2 of 2
  1. #1
    voipp is offline Member
    Join Date
    Oct 2013
    Posts
    51
    Rep Power
    0

    Default listening socket in a new thread

    Hi!
    I have min-chat and that is part of client code:
    Java Code:
        	final ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
            final ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
    
    
                
                // Создаем поток для чтения с клавиатуры.
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
    
                Thread t = new Thread(
                		new Thread()
                		{
                			@Override
                			public void run()
                			{
                				while(! socket.isClosed())
                				{
                					
                				}
                					
                			}
                		}
                		);
    
                        line = in.readUTF(); // In this part socket input data can be delayed and it can be not printed to console immediately
                        System.out.println(line);
    The reason why i have wrote particular thread is that program needs to listen servers socket and perform actions without delays in client code.
    So, why should i make Object streams final ?
    PS Feel free to offer another way to communicate with server data to print it to console immediately

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: listening socket in a new thread

    Because the thread will make a thread local copy of the reference and it NEEDS to make sure that those definitions WILL NOT CHANGE.

Similar Threads

  1. How to close this thread? (Socket listener)
    By zardos in forum New To Java
    Replies: 5
    Last Post: 03-02-2011, 11:27 PM
  2. Replies: 1
    Last Post: 07-02-2010, 02:18 PM
  3. Socket Listening problem in Netbeans
    By adeel109 in forum Networking
    Replies: 0
    Last Post: 05-06-2010, 03:21 PM
  4. NIO Server: listening on client's side socket
    By carek in forum Networking
    Replies: 1
    Last Post: 12-24-2009, 08:30 AM
  5. best way to signal a thread that's blocking on a socket receive
    By danieru in forum Threads and Synchronization
    Replies: 5
    Last Post: 09-08-2009, 05:47 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •