Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Client Server Program Not Working

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Client Server Program Not Working

    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.
    Last edited by ROHIT C.; September 12th, 2010 at 11:16 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Client Server Program Not Working

    Does the code work when the server and client are on the same machine and you use the localhost(127.0.0.1) address?

  3. The Following User Says Thank You to Norm For This Useful Post:

    ROHIT C. (September 3rd, 2010)

  4. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Client Server Program Not Working

    Thanks Norm for your reply.
    Yes it is working when client & server are executed on the same machine.
    That is it is working when following change is made to Client.java
    String host="127.0.0.1";

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Client Server Program Not Working

    That validates the code at one level. Next test should be to see if your IP addresses are correct and if there is a firewall or router or some other network problem.

Similar Threads

  1. [Java] Client - server, example
    By Grabar in forum Java Networking
    Replies: 6
    Last Post: January 22nd, 2011, 01:56 PM
  2. Client/Server
    By Dillz in forum Paid Java Projects
    Replies: 2
    Last Post: June 2nd, 2010, 05:19 AM
  3. Client-Server program
    By Reztem in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 12th, 2010, 05:36 PM
  4. [SOLVED] The concept of Server and Client
    By rendy.dev in forum Java Theory & Questions
    Replies: 3
    Last Post: January 18th, 2010, 04:13 AM
  5. client server program on a single machine
    By subhopam in forum Java Networking
    Replies: 1
    Last Post: December 16th, 2009, 02:02 PM