1

I'm trying to run a client-server program in java on Netbeans.

Here's the code for the server:

// File Name GreetingServer.java

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

public class GreetingServer extends Thread
{
   private ServerSocket serverSocket;

   public GreetingServer(int port) throws IOException
   {
      serverSocket = new ServerSocket(port);
      serverSocket.setSoTimeout(10000);
   }

   public void run()
   {
      while(true)
      {
         try
         {
            System.out.println("Waiting for client on port " +
            serverSocket.getLocalPort() + "...");
            Socket server = serverSocket.accept();
            System.out.println("Just connected to "
                  + server.getRemoteSocketAddress());
            DataInputStream in =
                  new DataInputStream(server.getInputStream());
            System.out.println(in.readUTF());
            DataOutputStream out =
                 new DataOutputStream(server.getOutputStream());
            out.writeUTF("Thank you for connecting to "
              + server.getLocalSocketAddress() + "\nGoodbye!");
            server.close();
         }catch(SocketTimeoutException s)
         {
            System.out.println("Socket timed out!");
            break;
         }catch(IOException e)
         {
            e.printStackTrace();
            break;
         }
      }
   }
   public static void main(String [] args)
   {
      int port = Integer.parseInt(args[0]); #line 48, error arises here. 
      try
      {
         Thread t = new GreetingServer(port);
         t.start();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}

Here's the code for the client:

// File Name GreetingClient.java

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

public class GreetingClient
{
   public static void main(String [] args)
   {
      String serverName = args[0]; #line 10, error arises here 
      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();
      }
   }
}

So when I want to simulate the client and the server running together I first run the server. Once the server is running I need to run the client.

However when I try to run the server file, I get the following error:

run: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at GreetingServer.main(GreetingServer.java:48) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)

Similarly, when I try to run the client program, I get the following error:

run: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at GreetingClient.main(GreetingClient.java:10) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)

Why is this happening? Please help.

UPDATE:

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

public class GreetingServer extends Thread
{
    private static int port;
   private ServerSocket serverSocket;

   public GreetingServer(int port) throws IOException
   {
      serverSocket = new ServerSocket(port);
      serverSocket.setSoTimeout(10000);
   }

   public void run()
   {
      while(true)
      {
         try
         {
            System.out.println("Waiting for client on port " +
            serverSocket.getLocalPort() + "...");
            Socket server = serverSocket.accept();
            System.out.println("Just connected to "
                  + server.getRemoteSocketAddress());
            DataInputStream in =
                  new DataInputStream(server.getInputStream());
            System.out.println(in.readUTF());
            DataOutputStream out =
                 new DataOutputStream(server.getOutputStream());
            out.writeUTF("Thank you for connecting to "
              + server.getLocalSocketAddress() + "\nGoodbye!");
            server.close();
         }catch(SocketTimeoutException s)
         {
            System.out.println("Socket timed out!");
            break;
         }catch(IOException e)
         {
            e.printStackTrace();
            break;
         }
      }
   }
   public static void main(String [] args)
   {
      port=9000;
      try
      {
         Thread t = new GreetingServer(port);
         t.start();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}


// File Name GreetingClient.java

import java.io.*;
import java.net.*;
public class GreetingClient
{
    private static String serverName;
   public static void main(String [] args)
   {
      String sName = "MyServerName";
      int port = 9000;
      try
      {
         System.out.println("Connecting to " + sName
                             + " on port " + port);
         Socket client = new Socket(sName, 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)
      {
      }
   }
}

There is no error on the server side now. But I am still getting the following error on the client side:

run: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at GreetingClient.main(GreetingClient.java:10) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)

5
  • when tou running your program, are you passing any arguments to it? Commented Apr 24, 2013 at 8:34
  • How do you run the server? Commented Apr 24, 2013 at 8:35
  • no i'm not. if I should, then where and how do I pass them? Commented Apr 24, 2013 at 8:36
  • by right clicking on the .java file and selecting Run Commented Apr 24, 2013 at 8:36
  • please have a look at the update Commented Apr 24, 2013 at 9:18

2 Answers 2

2

Your code supposes that you pass the port you want to use as an argument so :

  • Either you run your application with the line Patryk Roszczyniała posted

  • If you don't want to use it, just delete the lines :

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

    and replace them by hardcoded values like :

    String serverName = "MyServerName";
    int port = 3000;
    

Update :

I just tested your code, in fact, the serverName is the address you use to connect to the server, so replace it by "localhost" if you are running both client and server on the same machine.

Tested code :

Here is the updated code I tested and it should work perfectly if you run both on the same machine.

Client :

import java.io.*;
import java.net.*;
public class GreetingClient
{
    private static String serverName;
   public static void main(String [] args)
   {
      String sName = "localhost";
      int port = 9000;
      try
      {
         System.out.println("Connecting to " + sName
                             + " on port " + port);
         Socket client = new Socket(sName, 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)
      {
      }
   }
}

Server :

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

public class GreetingServer extends Thread
{
    private static int port;
   private ServerSocket serverSocket;

   public GreetingServer(int port) throws IOException
   {
      serverSocket = new ServerSocket(port);
      serverSocket.setSoTimeout(20000);
   }

   public void run()
   {
      while(true)
      {
         try
         {
            System.out.println("Waiting for client on port " +
            serverSocket.getLocalPort() + "...");
            Socket server = serverSocket.accept();
            System.out.println("Just connected to "
                  + server.getRemoteSocketAddress());
            DataInputStream in =
                  new DataInputStream(server.getInputStream());
            System.out.println(in.readUTF());
            DataOutputStream out =
                 new DataOutputStream(server.getOutputStream());
            out.writeUTF("Thank you for connecting to "
              + server.getLocalSocketAddress() + "\nGoodbye!");
            server.close();
         }catch(SocketTimeoutException s)
         {
            System.out.println("Socket timed out!");
            break;
         }catch(IOException e)
         {
            e.printStackTrace();
            break;
         }
      }
   }
   public static void main(String [] args)
   {
      port=9000;
      try
      {
         Thread t = new GreetingServer(port);
         t.start();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}
Sign up to request clarification or add additional context in comments.

11 Comments

I compiled and ran your code, I don't get an exception but you ahve to set the serverName at "localhost" to get it to connect. Check my edit
I posted you the updated code I tested, check it, it runs perfectly on my computer just by running both of them in Eclipse.
i copied and pasted your code into Netbeans and it still dosen't work
I'm getting the same error: run: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at GreetingClient.main(GreetingClient.java:10) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)
If you still get an exception, tell me how do you run it now? If you run it by launching a .jar, make sure you regenerated the executable.jar after making the corrections.
|
1

To run SocketServer you need to pass port on which server will work. For example:

SocketServer server = new SocketServer(1234);

The problem is that you want to pass port by java argument and you aren't doing that. First of all before use of any variable you should first validate it.

if (args.length>0) {
    int port = Integer.parseInt(args[0]); #line 48, error arises here. 
}

Your program requires params but I think that you probably run your program without params. You should run program in this way: java -jar app.jar 1234 But you do it in this way: java -jar app.jar

In this article you will find how to pass parameters to java program using netbeans http://netbeanside61.blogspot.com/2009/02/using-command-line-arguments-in.html.

If you are new in java you should first read this tutorial http://docs.oracle.com/javase/tutorial/networking/sockets/. It's very helpful.

8 Comments

You've got the same problem with client. You try to pass arg[0] as port but arg array is empty. You should validate arg array before use it and pass port for client using command line or just hardcode port for client.
have another look, I've hardcoded the port number. It's now: String sName = "MyServerName"; int port = 9000;
And you've got still exception?
moreover why am I still getting the array index out of bounds as an exception when I am no longer using args[]?
I think that you should read this article docs.oracle.com/javase/tutorial/networking/sockets first.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.