Results 1 to 5 of 5
Thread: Problem with Vectors
- 04-04-2011, 02:53 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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:
From what I can tell... I'm doing something wrong with Vector.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)
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 10:06 AM.
- 04-04-2011, 04:25 AM #2
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
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.
The scope of object c is local to ClientConn object in main method. You can not get this object in broadcastMessage method.public void run() {
ChatServer s = new ChatServer();
BufferedReader in = null;
PrintWriter out = null;
String msg = null;
ChatServer c = new ChatServer();
As per my understanding you should create ClientConn as an independent class not an inner class.
-
Here:
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++) {
Java Code:for(int i = 0; i < connections.size(); i++) {
- 04-04-2011, 09:43 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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
Yet I still have the public final Vector<Socket> connections = new Vector<Socket>(); at under public class ChatServer.Java Code:non-static variable connections cannot be referenced from a static context connections.add(client);
@Fubarable - Thanks for catching that!
- 04-04-2011, 10:02 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
Actually, I fixed the Vector problem myself.
I think that is the correct way to solve it, right?Java Code:public static void main (String[] args) throws IOException { ChatServer s = new ChatServer(); ... ... s.connections.add(client);
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
-
Vectors Problem
By dashwall in forum New To JavaReplies: 9Last Post: 01-04-2010, 11:16 PM -
Sorting JTable (Vectors) Problem
By ramapple in forum AWT / SwingReplies: 6Last Post: 07-06-2009, 11:15 PM -
Vectors of Vectors or hash-somethings?
By mindwarp in forum New To JavaReplies: 3Last Post: 03-10-2008, 02:57 PM -
problem with Vectors and getTotal() function
By java_fun2007 in forum New To JavaReplies: 2Last Post: 11-23-2007, 01:55 PM -
Problem with vectors in java
By toby in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks