Below is the java code for client & server program (tcp) using socket.
I am trying to run client & server program on 2 different machines connected 2 internet
(1 runs server.java & other client.java).
// Server.Java
import java.net.;
import java.io.;
class Server {
public static void main(String[] args) {
boolean finished = false;
try{
ServerSocket listener = new ServerSocket(n);//n any no.
while(!finished)
{OutputStream out = listener.getOutputStream();
PrintWriter pout = new PrintWriter(out, true);
pout.println("");
to_client.close();
}
listener.close();
}// end of try
catch(Exception ie) {
System.out.println(ie);
}
}
}
//Client.Java
import java.net.;
import java.io.;
class Client
{
public static void main(String[] args)
{
Socket client;
String host="serverpcname";//host is assigned name of the server
try
{InetAddress adressen = InetAddress.getByName(host);
client = new Socket(adressen,4444);
BufferedReader scanf = new BufferedReader();
String someString = scanf.readLine();
System.out.println("From Server: "+someString);
client.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
When the code is executed (CLIENT.java ON 1 PC & SERVER.java on other) it gives following exceptions:java.net.UnknownHostException: serverpcname: serverpcname
I even tried replacing host in Client.java with IP address of server
String host="192.168.1.35";
It gives exception:
java.net.ConnectException: Connection timed out: connect
Could there be any firewall problem?l
PLZ. GUIDE ME.
THANKS IN ADVANCE.