1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Client server program in java on 2 different machines connected on internet

Discussion in 'Java' started by ROHIT C., Aug 31, 2010.

  1. ROHIT C.

    ROHIT C. New Member

    Joined:
    Aug 31, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Below is the code for Client & Server
    I m trying 2 run it on 21 different machines connected to internet(1 machine runs Client & other Server).

    Code:
    //Client.Java
    import java.net.*;
    import java.io.*;
    class Client 
    {
     public static void main(String[] args) 
    {
     Socket client;
     String host="home";//home is Server's computer name
     
     try
      {InetAddress adressen = InetAddress.getByName(host);
       client = new Socket(adressen,4444);
       BufferedReader scanf = new BufferedReader(new
      InputStreamReader(client.getInputStream()));
       String someString = scanf.readLine();
      System.out.println("From Server: "+someString);
      client.close();
     }
     catch(Exception e)
      {
         System.out.println(e);  
      }
     }
    }
    
    Server
    Code:
    // Server.Java
    import java.net.*;
    import java.io.*;
    class Server {
    public static void main(String[] args) {
    boolean finished = false;
    try{
    ServerSocket listener = new ServerSocket(4444);
    while(!finished)
    {Socket to_client = listener.accept();
    OutputStream out = to_client.getOutputStream();
    PrintWriter pout = new PrintWriter(out, true);
    pout.println("Hello! this is server talking to you.");
    to_client.close();
    }
    listener.close();
    }// end of try
    catch(Exception ie) {
    System.out.println(ie);
    }
    }
    }
    
    But its not working & giving error:
    java.net.UnknownHostException: home: home

    PLEASE GUIDE ME!!
     
    Last edited by a moderator: Sep 1, 2010

Share This Page