-1

I am learning to write Java network programming codes for both client and server system by using Glassfish Server.

I have created a localhost server through GS. Below is the code that I copied from github.

import java.net.*;
import java.io.*;

public class GreetingClient
{
   public static void main(String [] args)
   {
      String serverName = args[0];
      int port = Integer.parseInt(args[1]);
      try
      {
         System.out.println("Connecting to " + serverName +
         " on port " + port);
         Socket client = new Socket(serverName, port);
         System.out.println("Just connected to " 
         + client.getRemoteSocketAddress());
         OutputStream outToServer = client.getOutputStream();
         DataOutputStream out = new DataOutputStream(outToServer);
         out.writeUTF("Hello from "
                      + client.getLocalSocketAddress());
         InputStream inFromServer = client.getInputStream();
         DataInputStream in =
                        new DataInputStream(inFromServer);
         System.out.println("Server says " + in.readUTF());
         client.close();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}

Every time I run the code, it shows the error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0

How do I fix it please? This is really confusing...

Heng
  • 1
  • What line causes the error? How are you launching the app? – Johnny Mopp Jul 01 '19 at 18:38
  • String[] args is an array of program arguments, are you sure you are passing any arguments at all? – EDToaster Jul 01 '19 at 18:38
  • That is very likely from your argument list. Make sure to add a parameter when launching. – Tad Harrison Jul 01 '19 at 18:39
  • @JClassic Thank you for the reminder. I found what I've missed here and have made some changes. The code seems to be working now but the connection was refused. – Heng Jul 01 '19 at 19:06
  • @JohnnyMopp I did not pass any arguments here that's why it is being like this. Now the ArrayIndexOutOfBoundsException has been resolved but the connection was refused after I ran the code. – Heng Jul 01 '19 at 19:08

4 Answers4

2

Your application is expecting two command line arguments for the servername and the port.

   String serverName = args[0];
   int port = Integer.parseInt(args[1]);

For example if the GlassFish server is running on your local host on port 8080 you would run your client application with the following parameters

   java GreetingClient localhost 8080
Marc G. Smith
  • 836
  • 5
  • 8
  • Edit: New error here. After I changed it to String serverName = "localhost"; int port = Integer.parseInt("8080"); It showed that it had connected to localhost/127.0.0.1:8080 but after a while, the error java.io.EOFException at java.base/java.io.DataInputStream.readUnsignedShort (DataInputeStream.java:345) and other 3 lines showed up... – Heng Jul 01 '19 at 19:09
1

args contains command line parameters. If you run application without any parameter this array is empty, and when you are trying to get an element args[0] you get this exception.

To fix this you should either check whether array has elements before accessing it, or pass parameters in a command line: java MyApp firstParam secondParam

Max Farsikov
  • 1,868
  • 13
  • 19
0

You have to launch the application passing command line parameters, they will fill the args array. Usually good applications check if the parameters are present before using them, typically reposting some guide to the user on available and mandatory arguments.

Felice Pollano
  • 31,141
  • 8
  • 67
  • 108
0

As the others have said your program is expecting arguments to be passed to it.

If you are using an IDE such as Eclipse, you can configure your arguments by:

  1. Navigate to Run on the top menu.
  2. Select Run Configurations.
  3. Select Java Application on the left, and choose the correct application.
  4. Click the second tab named Arguments on the top.
  5. Write into the Program arguments box the parameters you want to pass every time this program is run.
  6. Click Apply. Now every time this program is ran inside of Eclipse, it will use those parameters.
Nexevis
  • 4,024
  • 3
  • 11
  • 20