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.

Results 1 to 5 of 5

Thread: My chat program's multi threaded server keeps on throwing exceptions when calling nextLine();

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    My Mood
    Dead
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My chat program's multi threaded server keeps on throwing exceptions when calling nextLine();

    package chatprogram;
     
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.util.Iterator;
    import java.util.Scanner;
    import java.util.Vector;
     
    public class SunChatServerChild extends Thread {
     
    	static Scanner in;
    	static PrintWriter out;
    	//static HashSet<PrintWriter> writerSet = new HashSet<PrintWriter>();
    	//static HashSet<String> names = new HashSet<String>();
    	static Vector<PrintWriter> wV = new Vector<PrintWriter>();
    	static Vector<Scanner> sV = new Vector<Scanner>();
    	static Socket sock;
    	static int n;
     
    	public SunChatServerChild(Socket s, int cN) {
    		sock = s;
    		n  = cN;
    	}
     
    	public void run() {
    		super.run();
    		try {
    				in = new Scanner(new InputStreamReader(sock.getInputStream()));
    				out = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()), true);
    				wV.add(out);
    				while (true) {
    					String fromClient = in.nextLine(); // <--- problem right here when multiple clients are connected and chatting
    					broadCastMsg(fromClient);
    				}
    		} catch (IOException e) {
    		} finally {
    			in.close();
    			out.close();
    		}
    	}
    	private void broadCastMsg(String fromClient) {
    		Iterator<PrintWriter> it = wV.iterator();
    		while (it.hasNext()) {
    			PrintWriter writer = it.next();
    			writer.println(fromClient);
    			writer.flush();
    		}
    	}
    }

    cant figure out whats wrong, the program usually throws OutOfBoundsIndex, or NoSuchElementException: No line found.
    Doesn't the the Server sit on the nextLine() until the client sends out a message. This only occurs when I have more than one client, and sometimes it works for a few messages back and forth then it throws the exceptions.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: My chat program's multi threaded server keeps on throwing exceptions when calling nextLine();

    Why is your catch block empty? You shouldn't do that, and should at least print a stack trace there.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    My Mood
    Dead
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My chat program's multi threaded server keeps on throwing exceptions when calling nextLine();

    Nvm error has been resolved from using magical logic. Somehow my teachers identical code manages to run it flawlessly... : /

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: My chat program's multi threaded server keeps on throwing exceptions when calling nextLine();

    That's great, but still I urge you not to use empty catch blocks which is the coding equivalent of driving a car with blindfolds on.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    My Mood
    Dead
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My chat program's multi threaded server keeps on throwing exceptions when calling nextLine();

    Will take that into account! I finally understood what the bug was... I made the Scanner static so I believe every thread was sharing the same Scanner.

Similar Threads

  1. How to show chat in threaded view
    By devashish in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: August 21st, 2012, 04:13 AM
  2. Simple client-server chat program
    By Saloni Patil in forum Java Networking
    Replies: 3
    Last Post: October 22nd, 2011, 09:29 AM
  3. Replies: 3
    Last Post: July 24th, 2011, 06:13 AM
  4. Multi-Threaded Chat Server
    By TopdeK in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 7th, 2011, 10:12 AM
  5. Replies: 1
    Last Post: October 19th, 2009, 11:53 PM