Results 1 to 4 of 4
  1. #1
    kappilrinesh is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default 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
    Java 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
    Java 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();
    		}
    	
    	}
    }
    Last edited by kappilrinesh; 10-02-2011 at 04:36 PM.

  2. #2
    kappilrinesh is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default Re: JAVA Socket Program

    hey i wud b grateful , if eny can tell me soon, why this error is occuring....Please..!!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  4. #4
    kappilrinesh is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default 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.....!!!! :)

Similar Threads

  1. java Socket Load balancing
    By rajeshsun69 in forum Networking
    Replies: 0
    Last Post: 12-17-2010, 11:25 AM
  2. Simple Socket program: Java Client- C server
    By pimmling in forum New To Java
    Replies: 1
    Last Post: 11-08-2010, 01:27 PM
  3. Replies: 1
    Last Post: 07-02-2010, 01:18 PM
  4. Extending java.net.Socket
    By DC200 in forum Networking
    Replies: 3
    Last Post: 06-10-2010, 04:23 PM
  5. append response to the request from Socket and write to another socket
    By vaibhav_singh_vs@yahoo.co in forum Networking
    Replies: 3
    Last Post: 04-17-2009, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •