Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 38 of 38

Thread: Reply to all clients Multithreading

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reply to all clients Multithreading

    If the variable: name has a null value, the code should test its value before trying to use it where it will cause a NPE.
    If you don't understand my answer, don't ignore it, ask a question.

  2. The Following User Says Thank You to Norm For This Useful Post:

    ManInTheMiddle (May 16th, 2013)

  3. #27
    Member
    Join Date
    Nov 2012
    Posts
    49
    My Mood
    Amused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Reply to all clients Multithreading

    my private String name = null. I think this is causing it.

    I'v noticed that "name" should be reading in data just before the NPE error ( name = in.readInput), so, I think my client code needs to be edited as the
                       in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    doesn't seem to be receiving anything.

  4. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reply to all clients Multithreading

    If you do not know which variable is null when line 36 is executed, add a println just before line 36 that prints out the values of all the variables used in line 36.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    ManInTheMiddle (May 16th, 2013)

  6. #29
    Member
    Join Date
    Nov 2012
    Posts
    49
    My Mood
    Amused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Reply to all clients Multithreading

    when I print out name it prints data but the ArrayList "nameList" is null, so, I think that's causing it.

    I'v tried a few other things and the same error, it doesn't want to let me add data to the ArrayList nameList..

  7. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reply to all clients Multithreading

    the ArrayList "nameList" is null,
    It needs to be assigned a value so it is not null.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    ManInTheMiddle (May 16th, 2013)

  9. #31
    Member
    Join Date
    Nov 2012
    Posts
    49
    My Mood
    Amused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Reply to all clients Multithreading

    Got it i needed to change

     
    private ArrayList <ServerThread> nameList;


    to


     
    private ArrayList <ServerThread> nameList = new ArrayList<>();

  10. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Reply to all clients Multithreading

    That should be better.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    ManInTheMiddle (May 16th, 2013)

  12. #33
    Member
    Join Date
    Nov 2012
    Posts
    49
    My Mood
    Amused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Reply to all clients Multithreading

    Cheers, rooky mistake. Much appreciated..

  13. #34
    Member
    Join Date
    Nov 2012
    Posts
    49
    My Mood
    Amused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Reply to all clients Multithreading

    hi,

    I have this completed and working now except for an error when a client disconnects..

    import java.net.*;
    import java.io.*;
    import java.util.*;
     
    class ServerThread extends Thread
    {
    	private Socket socket = null;
    	private String name = null;
    	private String message = null;
        private ArrayList <ServerThread> clientList = new ArrayList<>();
    	private ArrayList <String> nameList = new ArrayList<>();;
    	private PrintWriter out = null; 
        private BufferedReader in = null;
     
    	ServerThread(Socket socket, ArrayList <ServerThread> clientList)
    	{
        	super("ServerThread");
        	this.socket = socket;
        	this.clientList = clientList;
        }	
     
     
    	public void sendToAll(String message)
    	{
        	for(ServerThread c: clientList)
    		{
    			c.out.println(message);				
    		}
        }	
     
    	public void run()
    	{
    		try
    		{
    			out = new PrintWriter(socket.getOutputStream(), true);	// be able to echo back message to client
    			in = new BufferedReader
    			(new InputStreamReader(socket.getInputStream()));
    			in.readLine();
    			name = in.readLine();
     
    			synchronized(this){
    				sendToAll( name + " has joined the chatroom ....\n");	
    			}
     
    			if(!nameList.contains(name))
    			{
    				nameList.add(name);
    			}	
     
     
     
    			while((message = in.readLine()) != null)
    				synchronized(this)
    				{
    					sendToAll(name+" says: "+ message);
    				}
     
    			synchronized(this){			
    				clientList.remove(currentThread());
    			}
     
    			System.out.println("Clients :" + clientList.size() + "\n");
    			out.close();
    			in.close();
    			socket.close();
    		}	
    			catch(IOException e)
    			{
    				 e.printStackTrace();
    			}
    	}
    }
    // The server
    public class ChatServer
    {
    	public static final int PORT = 7777;
    	public ArrayList <ServerThread> clientList = new ArrayList<>();
     
    	public static void main(String[] args) throws IOException {
    			new ChatServer().run();
    	}
     
    	public void run() throws IOException{
    		ServerSocket serverSocket = new ServerSocket(PORT); 			// wait for request from PORT
    		System.out.println("Server is listening for connections...");			
     
     
    		while(true){
    			try{
    				Socket socket = serverSocket.accept();						
    				ServerThread client = new ServerThread(socket,clientList);
    				//add client to clientList and start the thread
    				clientList.add(client);
    				System.out.println("Clients :" + clientList.size() + "\n");
    				client.start();
    			} 
    			catch (IOException e)
    			{
    				e.getMessage();
    			}
     
    			serverSocket.close();
    		}
    	}
    }

    here's the error messages

    java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream. java:189)
    at java.net.SocketInputStream.read(SocketInputStream. java:121)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.j ava:283)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.ja va:325)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:1 77)
    at java.io.InputStreamReader.read(InputStreamReader.j ava:184)
    at java.io.BufferedReader.fill(BufferedReader.java:15 4)
    at java.io.BufferedReader.readLine(BufferedReader.jav a:317)
    at java.io.BufferedReader.readLine(BufferedReader.jav a:382)
    at ServerThread.run(ChatServer.java:39)

  14. #35
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Reply to all clients Multithreading

    Quote Originally Posted by ManInTheMiddle View Post
    I have this completed and working now except for an error when a client disconnects..
    You will have to write your code to handle things like this that are almost guaranteed to happen.
    Catch the exception and clean up the mess

  15. The Following User Says Thank You to jps For This Useful Post:

    ManInTheMiddle (May 16th, 2013)

  16. #36
    Member
    Join Date
    Nov 2012
    Posts
    49
    My Mood
    Amused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Reply to all clients Multithreading

    Quote Originally Posted by jps View Post
    You will have to write your code to handle things like this that are almost guaranteed to happen.
    Catch the exception and clean up the mess
    I don't quite follow. I'm not too sure why this is happening..

  17. #37
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Reply to all clients Multithreading

    Think of it like a phone call. The phone cord got cut (breaking the connection) and the server lost communication to the client without hearing the client say goodbye. The server is trying to talk/listen to whats not there. This is not what is expected to happen, the server expects a polite goodbye before they disconnect.
    An exception of this type is in place to cause an alert to the fact that it has happened so your program can respond correctly.

    If you were sending routing information for some money for someone, and the connection was broken, it may be very important to reconnect and attempt to send again.
    On the other hand if it was the last line of text in a chat room, it would not be important to reconnect and send. (Some chat programs will send the message the next time the user connects)
    What ever you decide to do, the exception being thrown is your indication it is time to take care of the problem.
    One solution would be to just ignore it. You are not required to actually do anything about it when it happens, but the syntax requires you to specify you intend to do nothing.
    That is as simple as an empty catch block.

    My suggestion is to read about this exception to understand why it has happened, and decide what you think your program should do when this triggering event takes place.
    (that goes for all exceptions)

  18. The Following User Says Thank You to jps For This Useful Post:

    ManInTheMiddle (May 16th, 2013)

  19. #38
    Member
    Join Date
    Nov 2012
    Posts
    49
    My Mood
    Amused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Reply to all clients Multithreading

    Cheers thanks for that..

    Got it working

    Greatly appreciated...

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Multithreading
    By Miths in forum Java Theory & Questions
    Replies: 10
    Last Post: December 17th, 2012, 02:46 AM
  2. Its urgent please reply soon
    By vikas.sharma in forum Web Frameworks
    Replies: 3
    Last Post: September 11th, 2012, 09:12 AM
  3. reply plz
    By Tanmaysinha in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 16th, 2011, 09:24 AM
  4. Am I right (urgent, please reply)
    By Sunil Raghuvanshi in forum The Cafe
    Replies: 2
    Last Post: March 26th, 2010, 07:05 AM

Tags for this Thread