-
JAVA Socket Program
I am trying to run the following socket programs, both programs are compiled, but when i type "java GServer" on the cmd, the screen shows "Exception in thread "main" java.lang.ArrayIndexOutOfBounds: 0 at GClient.main(GClient.java:9)"
And when i run the server program it shows a similar error as "Exception in thread "main" java.lang.ArrayIndexOutOfBounds: 0 at GServer.main(GServer.java:43)" on executing "java GServer" on the cmd
I want to know how to run it properly, as i am new to JAVA,and naive too.
the codes are as follows:
Server Socket
Code:
//GServer.java
import java.io.*;
import java.net.*;
public class GServer{
private ServerSocket serverSocket;
public GServer(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();
}
}
}
public static void main(String args[])
{
int port = Integer.parseInt(args[0]);
try
{
GServer t = new GServer(port);
t.run();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
Client
Code:
//GClient.java
import java.net.*;
import java.io.*;
public class GClient
{
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();
}
}
}
-
Re: JAVA Socket Program
hey i wud b grateful , if eny can tell me soon, why this error is occuring....Please..!!
-
Re: JAVA Socket Program
Your code is unreadable. Consider editing your original post and adding the tag [code] above your code block and the tag [/code] below your code block so that your code retains its formatting (it must be already formatted for this to work). Note that the tags use square brackets and that the top and bottom tags are different. Also you'll want to indicate which line is throwing the exception.
-
Re: JAVA Socket Program
I understood my mistake....!!
After compilation, the execution has to be done as follows:
For server side:
"java GServer 20201" Here 20201 is the port number
For client side:
"java GClient 127.0.0.1 20201" Here 127.0.0.1 is the servername which is usually specified in terms of IP Address and 20201 is the port number.
THNX.....!!!! :)