smhouston
asked on
IndexOutOfBoundsException Problem
I'm currently making a chat room where i have a server.java file and a client.java file.2 users can connect to the server successfully,and send a few messages between each other.the problem is that after a random amount of messages the following exception error occurs on the server window:
Exception in thread "Thread-0" java.lang.IndexOutOfBounds Exception: end
at java.util.regex.Matcher.re gion(Match er.java:88 2)
at java.util.Scanner.findPatt ernInBuffe r(Scanner. java:964)
at java.util.Scanner.findWith inHorizon( Scanner.ja va:1594)
at java.util.Scanner.findWith inHorizon( Scanner.ja va:1551)
at java.util.Scanner.nextLine (Scanner.j ava:1457)
at AssignmentServer$ClientHan dler.run(A ssignmentS erver.java :65)
after that error appears,the user that thread is associated with cannot send anymore messages,however,they can still received messages sent by other users.
i personally don't understand what it means and how to fix it.i've only beens tudying java for a few months so don't have the required knowledge to fix it so any help is appreciated!
if any code is required then just ask and i'll give you it
Exception in thread "Thread-0" java.lang.IndexOutOfBounds
at java.util.regex.Matcher.re
at java.util.Scanner.findPatt
at java.util.Scanner.findWith
at java.util.Scanner.findWith
at java.util.Scanner.nextLine
at AssignmentServer$ClientHan
after that error appears,the user that thread is associated with cannot send anymore messages,however,they can still received messages sent by other users.
i personally don't understand what it means and how to fix it.i've only beens tudying java for a few months so don't have the required knowledge to fix it so any help is appreciated!
if any code is required then just ask and i'll give you it
ASKER
i've been using textpad to do the code so i've given you lines 57 to 84 as i understand java and textpad numbering aren't the same?
[CODE]public void run()
{
String received;
Socket client2;
do
{
received = input.nextLine();
for(int i = 0; i <= AssignmentServer.clientSoc ket.size() -1; i++)
{
try
{
client2 = AssignmentServer.clientSoc ket.elemen tAt(i);
System.out.println("#1 " + client2);
PrintWriter output = new PrintWriter(client2.getOut putStream( ),true);
//output = new PrintWriter(client2.getOut putStream( ),true);
output.println(received);
System.out.println(receive d);
}
catch(IOException e)
{
System.err.println(e);
}
}
}while (!received.equals("QUIT")) ;[/CODE]
[CODE]public void run()
{
String received;
Socket client2;
do
{
received = input.nextLine();
for(int i = 0; i <= AssignmentServer.clientSoc
{
try
{
client2 = AssignmentServer.clientSoc
System.out.println("#1 " + client2);
PrintWriter output = new PrintWriter(client2.getOut
//output = new PrintWriter(client2.getOut
output.println(received);
System.out.println(receive
}
catch(IOException e)
{
System.err.println(e);
}
}
}while (!received.equals("QUIT"))
> as i understand java and textpad numbering aren't the same?
They should be.. I know for fact that Notepad, and just about every other IDE has correct line numbering..
Back to the problem now...
Hmm.. I could only make a wild guess there.. Could you post the full content of the class "ClientHandler" (from within the AssignmentServer), please?
Ta,
>> IM
They should be.. I know for fact that Notepad, and just about every other IDE has correct line numbering..
Back to the problem now...
Hmm.. I could only make a wild guess there.. Could you post the full content of the class "ClientHandler" (from within the AssignmentServer), please?
Ta,
>> IM
ASKER
sure...
private static class ClientHandler extends Thread
{
public void run()
{
String received;
Socket client2;
do
{
received = input.nextLine();
for(int i = 0; i <= AssignmentServer.clientSoc ket.size() -1; i++)
{
try
{
client2 = AssignmentServer.clientSoc ket.elemen tAt(i);
System.out.println("#1 " + client2);
PrintWriter output = new PrintWriter(client2.getOut putStream( ),true);
//output = new PrintWriter(client2.getOut putStream( ),true);
output.println(received);
System.out.println(receive d);
}
catch(IOException e)
{
System.err.println(e);
}
}
}while (!received.equals("QUIT")) ;
try
{
System.out.println("Closin g down connection...");
client.close();
for (int x = 0; x <= AssignmentServer.clientSoc ket.size() -1; x++)
{
Socket socketIP = AssignmentServer.clientSoc ket.get(x) ;
if (socketIP.equals(client))
{
AssignmentServer.clientSoc ket.remove ElementAt( x);
System.out.println("Client deleted");
}
System.out.println(Assignm entServer. clientSock et.element At(x));
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
private static class ClientHandler extends Thread
{
public void run()
{
String received;
Socket client2;
do
{
received = input.nextLine();
for(int i = 0; i <= AssignmentServer.clientSoc
{
try
{
client2 = AssignmentServer.clientSoc
System.out.println("#1 " + client2);
PrintWriter output = new PrintWriter(client2.getOut
//output = new PrintWriter(client2.getOut
output.println(received);
System.out.println(receive
}
catch(IOException e)
{
System.err.println(e);
}
}
}while (!received.equals("QUIT"))
try
{
System.out.println("Closin
client.close();
for (int x = 0; x <= AssignmentServer.clientSoc
{
Socket socketIP = AssignmentServer.clientSoc
if (socketIP.equals(client))
{
AssignmentServer.clientSoc
System.out.println("Client
}
System.out.println(Assignm
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i'm just doing what our lecturer tells us,and from what i'm finding out so far he's not teaching us the best possible methods so far!
i think i understand what you mean,but once i receive the message from a client,how do i pass it out to all the clients?as i did that using the vector last time and going through each client in the vector
i think i understand what you mean,but once i receive the message from a client,how do i pass it out to all the clients?as i did that using the vector last time and going through each client in the vector
ASKER
i've actually just managed to get it to work :-) when i was modifying the server a few weeks ago i moved some of the information from the clienthandler.i've just put it back in and it works so far *touch wood*.i've also took your advice on board,and even though i've still done the method different to what you recommend,i've implemented the never ending for loop.thanks alot for your help :-) the code now looks like the following:
import java.net.*;
import java.io.*;
import java.util.*;
public class AssignmentServerNew
{
private static InetAddress host;
private static ServerSocket servSocket;
private static final int PORT = 1234;
private static String text = "";
private static Vector<Socket> clientName = new Vector<Socket>();
private static Vector<Socket> clientSocket = new Vector<Socket>();
public static void main(String[] args) throws IOException
{
try
{
servSocket = new ServerSocket(PORT);
}
catch (IOException e)
{
System.out.println("\nUnab le to set up port!");
System.exit(1);
}
for(;;)
{
System.out.println("****** ********** SERVER RUNNING ****************");
Socket client = servSocket.accept(); //Wait for client.
host = client.getInetAddress();
System.out.println("\nNew client accepted with IP Address of" + host);
ClientHandler handler = new ClientHandler(client);
handler.start();
}
}
public static class ClientHandler extends Thread
{
private Socket client;
private Scanner input;
private PrintWriter output;
public ClientHandler(Socket socket) throws IOException
{
client = socket;
String clientUsername;
clientSocket.add(client);
input = new Scanner(client.getInputStr eam());
clientUsername = input.nextLine();
System.out.println(clientU sername);
}
public void run()
{
String received;
Socket client2;
do
{
received = input.nextLine();
System.out.println(receive d);
for(int i = 0; i <= AssignmentServerNew.client Socket.siz e()-1; i++)
{
try
{
client2 = AssignmentServerNew.client Socket.ele mentAt(i);
PrintWriter output = new PrintWriter(client2.getOut putStream( ),true);
output.println(received);
}
catch(IOException e)
{
System.err.println(e);
}
}
}while (!received.equals("QUIT")) ;
try
{
System.out.println("Closin g down connection...");
client.close();
for (int x = 0; x <= AssignmentServerNew.client Socket.siz e()-1; x++)
{
Socket socketIP = AssignmentServerNew.client Socket.get (x);
if (socketIP.equals(client))
{
AssignmentServerNew.client Socket.rem oveElement At(x);
System.out.println("Client deleted");
}
System.out.println(Assignm entServerN ew.clientS ocket.elem entAt(x));
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
import java.net.*;
import java.io.*;
import java.util.*;
public class AssignmentServerNew
{
private static InetAddress host;
private static ServerSocket servSocket;
private static final int PORT = 1234;
private static String text = "";
private static Vector<Socket> clientName = new Vector<Socket>();
private static Vector<Socket> clientSocket = new Vector<Socket>();
public static void main(String[] args) throws IOException
{
try
{
servSocket = new ServerSocket(PORT);
}
catch (IOException e)
{
System.out.println("\nUnab
System.exit(1);
}
for(;;)
{
System.out.println("******
Socket client = servSocket.accept(); //Wait for client.
host = client.getInetAddress();
System.out.println("\nNew client accepted with IP Address of" + host);
ClientHandler handler = new ClientHandler(client);
handler.start();
}
}
public static class ClientHandler extends Thread
{
private Socket client;
private Scanner input;
private PrintWriter output;
public ClientHandler(Socket socket) throws IOException
{
client = socket;
String clientUsername;
clientSocket.add(client);
input = new Scanner(client.getInputStr
clientUsername = input.nextLine();
System.out.println(clientU
}
public void run()
{
String received;
Socket client2;
do
{
received = input.nextLine();
System.out.println(receive
for(int i = 0; i <= AssignmentServerNew.client
{
try
{
client2 = AssignmentServerNew.client
PrintWriter output = new PrintWriter(client2.getOut
output.println(received);
}
catch(IOException e)
{
System.err.println(e);
}
}
}while (!received.equals("QUIT"))
try
{
System.out.println("Closin
client.close();
for (int x = 0; x <= AssignmentServerNew.client
{
Socket socketIP = AssignmentServerNew.client
if (socketIP.equals(client))
{
AssignmentServerNew.client
System.out.println("Client
}
System.out.println(Assignm
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
Could we see some code please?
I'm mainly after lines 60 to 70 in AssignmentServer.java please?
Cheers,
>> IM