Link to home
Start Free TrialLog in
Avatar of smhouston
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.IndexOutOfBoundsException: end
        at java.util.regex.Matcher.region(Matcher.java:882)
        at java.util.Scanner.findPatternInBuffer(Scanner.java:964)
        at java.util.Scanner.findWithinHorizon(Scanner.java:1594)
        at java.util.Scanner.findWithinHorizon(Scanner.java:1551)
        at java.util.Scanner.nextLine(Scanner.java:1457)
        at AssignmentServer$ClientHandler.run(AssignmentServer.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
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi.

Could we see some code please?
I'm mainly after lines 60 to 70 in AssignmentServer.java please?

Cheers,
>> IM
Avatar of smhouston
smhouston

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.clientSocket.size()-1; i++)
                        {
                              try
                              {
                                    client2 = AssignmentServer.clientSocket.elementAt(i);
                                    System.out.println("#1 " + client2);
                                    PrintWriter output = new PrintWriter(client2.getOutputStream(),true);
                                    //output = new PrintWriter(client2.getOutputStream(),true);
                                    output.println(received);
                                    System.out.println(received);
                              }
                              catch(IOException e)
                              {
                                    System.err.println(e);
                              }
                        }

                  }while (!received.equals("QUIT"));[/CODE]
> 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
sure...

private static class ClientHandler extends Thread
      {
            
            public void run()
            {
                  String received;
                  Socket client2;


                  do
                  {
                        received = input.nextLine();

                        for(int i = 0; i <= AssignmentServer.clientSocket.size()-1; i++)
                        {
                              try
                              {
                                    client2 = AssignmentServer.clientSocket.elementAt(i);
                                    System.out.println("#1 " + client2);
                                    PrintWriter output = new PrintWriter(client2.getOutputStream(),true);
                                    //output = new PrintWriter(client2.getOutputStream(),true);
                                    output.println(received);
                                    System.out.println(received);
                              }
                              catch(IOException e)
                              {
                                    System.err.println(e);
                              }
                        }

                  }while (!received.equals("QUIT"));

                  try
                  {
                        System.out.println("Closing down connection...");
                        client.close();
                        for (int x = 0; x <= AssignmentServer.clientSocket.size()-1; x++)
                        {
                              Socket socketIP = AssignmentServer.clientSocket.get(x);

                              if (socketIP.equals(client))
                              {
                                    AssignmentServer.clientSocket.removeElementAt(x);
                                    System.out.println("Client deleted");
                              }

                              System.out.println(AssignmentServer.clientSocket.elementAt(x));
                        }
                  }
                  catch(IOException e)
                  {
                        e.printStackTrace();

                  }

            }
      }
ASKER CERTIFIED SOLUTION
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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'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("\nUnable 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.getInputStream());
                  clientUsername = input.nextLine();
                  System.out.println(clientUsername);
            }

            public void run()
            {
                  String received;
                  Socket client2;

                  do
                  {
                        received = input.nextLine();
                        System.out.println(received);

                        for(int i = 0; i <= AssignmentServerNew.clientSocket.size()-1; i++)
                        {
                              try
                              {
                                    client2 = AssignmentServerNew.clientSocket.elementAt(i);
                                    PrintWriter output = new PrintWriter(client2.getOutputStream(),true);
                                    output.println(received);

                              }
                              catch(IOException e)
                              {
                                    System.err.println(e);
                              }
                        }

                  }while (!received.equals("QUIT"));

                  try
                  {
                        System.out.println("Closing down connection...");
                        client.close();
                        for (int x = 0; x <= AssignmentServerNew.clientSocket.size()-1; x++)
                        {
                              Socket socketIP = AssignmentServerNew.clientSocket.get(x);

                              if (socketIP.equals(client))
                              {
                                    AssignmentServerNew.clientSocket.removeElementAt(x);
                                    System.out.println("Client deleted");
                              }

                              System.out.println(AssignmentServerNew.clientSocket.elementAt(x));
                        }
                  }
                  catch(IOException e)
                  {
                        e.printStackTrace();

                  }

            }

      }
}