Results 1 to 5 of 5
  1. #1
    travist120 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default Problem with Vectors

    Hello everyone! I'm having trouble with my chat server. My server takes incoming sockets and puts them into a vector and separate thread. I can connect to this server with my client program, but if I try to input any text, I get this error:

    Java Code:
    Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
    	at java.util.Vector.get(Vector.java:694)
    	at ChatServer.broadcastMessage(ChatServer.java:19)
    	at ChatServer$1ClientConn.run(ChatServer.java:63)
    	at java.lang.Thread.run(Thread.java:680)
    From what I can tell... I'm doing something wrong with Vector.

    This is my program:
    ***EDIT*** - Now Current.
    Java Code:
    /* ChatServer.java */
    import java.net.*;
    import java.io.*;
    import java.util.*;
    import java.nio.*;
    
    public class ChatServer {
    	private static int port = 4444; /* port to listen on */
    	private Socket client;
    	public final Vector<Socket> connections = new Vector<Socket>();
    
    	public void broadcastMessage(String string) {
    		String clientMessage;
    		clientMessage = string;
    		PrintWriter out = null;
    		try
    		{
    			for(int i=0; i<connections.size(); i++) { 
    				client = connections.get(i);
    				out = new PrintWriter(client.getOutputStream(), true);
    				out.print(clientMessage);
    				out.close();
    			}
    		} catch (IOException ioEx) { 
    			System.out.println("broadcast message failed.");
    			ioEx.printStackTrace(); 
    		}
    	}
    
    	public static void main (String[] args) throws IOException {
    		ServerSocket server = null;
    		ChatServer s = new ChatServer();
    		try
    		{
    			server = new ServerSocket(port); /* start listening on the port */
    			System.out.println("Opening port:" + port);
    		} catch (IOException e) {
    			System.out.println("Could not listen on port: " + port);
    			e.printStackTrace();
     		}	
    
    		Socket client = null;
    		while(true) {
    			try 
    			{
    				client = server.accept();
    				s.connections.add(client);					
    			} catch (IOException e) {
    				System.out.println("Accept failed.");
    				e.printStackTrace();
    				System.exit(1);
    			}
    			/* start a new thread to handle this client */
    			Thread t = new Thread(new ClientConn(client));
    			t.start();
    		}	
    	}
    }
    
    
    class ClientConn implements Runnable {
    	private Socket client;
    	
    	ClientConn(Socket client) {
    		this.client = client;
    		System.out.println("Connection from " + client);
    	}
    	
    	public void run() {
    		ChatServer s = new ChatServer();
    		BufferedReader in = null;
    		PrintWriter out = null;
    		String msg = null;
    		ChatServer c = new ChatServer();
    		c.connections.add(client);
    		try 
    		{
    			/* obtain an input stream to this client ... */
    			in = new BufferedReader(new InputStreamReader(client.getInputStream()));	
    		} catch (IOException e) {
    			System.out.println("Something in thread failed.");
    			e.printStackTrace();
    		}
    		//Grab messages from the clients then broadCast them to all connected clients.
    		while(true) { 
    			msg = null;
    			try 
    			{ 
    				msg = in.readLine(); 
    				System.out.println("Message Recieved.");
    			}catch (IOException ioEx) { 
    				System.out.println("problem with msg in.readLine();");
    				ioEx.printStackTrace(); 
    			}
    			if(msg != null) {
    				s.broadcastMessage(msg);
    				msg = null;
    			}
    		}		
    	}
    }
    Last edited by travist120; 04-04-2011 at 11:06 AM.

  2. #2
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    You need to redesign your code.

    The object to which you are adding the connections is different than the object from which you are trying to access the connections.

    public void run() {
    ChatServer s = new ChatServer();
    BufferedReader in = null;
    PrintWriter out = null;
    String msg = null;
    ChatServer c = new ChatServer();
    The scope of object c is local to ClientConn object in main method. You can not get this object in broadcastMessage method.

    As per my understanding you should create ClientConn as an independent class not an inner class.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Here:

    Java Code:
    for(int i = 0; i <= connections.size(); i++) {
    looks dangerous as your connections Vector will contain size() number of items going from item 0 to item connections.size() - 1. Your i <= connections.size() means that it will look for an item at connections.size() which doesn't exist. Much safer is to change the loop to look like so:


    Java Code:
    for(int i = 0; i < connections.size(); i++) {

  4. #4
    travist120 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the advice lovelesh! I redesigned my code so that now ClientConn is it's own class. Now, the only problem I'm having is with my vector.

    I'm getting
    Java Code:
    non-static variable connections cannot be referenced from a static context
    				connections.add(client);
    Yet I still have the public final Vector<Socket> connections = new Vector<Socket>(); at under public class ChatServer.

    @Fubarable - Thanks for catching that!

  5. #5
    travist120 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Actually, I fixed the Vector problem myself.
    Java Code:
    public static void main (String[] args) throws IOException {
    ChatServer s = new ChatServer();
    ...
    ...
    s.connections.add(client);
    I think that is the correct way to solve it, right?
    It works now, but now I have another hurdle. I can send exactly 1 message... and I don't get any data in return. I think it has something to do with broadCastMessage(); but I'm not sure.

Similar Threads

  1. Vectors Problem
    By dashwall in forum New To Java
    Replies: 9
    Last Post: 01-05-2010, 12:16 AM
  2. Sorting JTable (Vectors) Problem
    By ramapple in forum AWT / Swing
    Replies: 6
    Last Post: 07-07-2009, 12:15 AM
  3. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 03:57 PM
  4. problem with Vectors and getTotal() function
    By java_fun2007 in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 02:55 PM
  5. Problem with vectors in java
    By toby in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:56 AM

Posting Permissions

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