Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Client server program in java on 2 different machines connected on internet (http://www.go4expert.com/forums/client-server-program-java-2-machines-t23157/)

ROHIT C. 31Aug2010 23:58

Client server program in java on 2 different machines connected on internet
 
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!!


All times are GMT +5.5. The time now is 12:20.