How do I compile this example network socket in Java using Eclipse?
Ok, I found this example in the book, so I know this code is error free. The program below contains two classes, both of which are main classes. One for the client and one for the server.
According to this book, I have to compile them like this:
Compile the client and server, and then start the server as follows:
$ java GreetingServer 6066 Waiting for client on port 6066 ...
Test the client program as follows:
$ java Greetings Client localhost 6066 Connect to localhost on port 6066 Just connected to localhost / 127.0.0.1: 6066 Server says Thank you for connecting to /127.0.0.1:6066 Goodbye!
I want to be able to run them on eclipse but every time I do it it gives me this error: Exception on thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 on GreetingServer.main (GreetingServer.java:47).
HOW CAN I CHECK THIS PROGRAM IN THE EKLIPSE? Thank.
// GreetingClient.java filename
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();
}
}
}
// 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]);
try
{
Thread t = new GreetingServer(port);
t.start();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
source to share
See these lines
public static void main(String [] args)
{
String serverName = args[0]; //<-- Expecting a value
int port = Integer.parseInt(args[1]); //<-- Expecting a value
Argi [] is a string array containing the arguments that you pass through the command line. And when you try to run it directly from eclipse you arent specifying those values, which causes args [] to be an empty array and hence args [0] gives ArrayIndexOutOfBoundsException
To solve this issue create a launch config from eclipse (see screenshot)
and provide the arguments you want eclipse to pass when this class is run. You can do this with the help right-clicking the project, select run, then run-configurations --> double click on java_application
and convey the required information. You may need to provide the name of the main class when you supply the arguments so that eclipse can recognize which main class to pass args.
OR you can just hard-code these values ββdirectly in the class itself (for testing)
source to share