Search code examples
javasocketsclient-serverserversocket

How to use this client-server program?


I have a work code of client-server program. Please explain to me how to successfully use this program. As I understand at first I run Server.java

Server.java:

public class Server {

  public static void main(String[] args) throws IOException {
    System.out.println("Welcome to Server side");
    BufferedReader in = null;
    PrintWriter    out= null;

    ServerSocket servers = null;
    Socket       fromclient = null;

    // create server socket
    try {
      servers = new ServerSocket(4444);
    } catch (IOException e) {
      System.out.println("Couldn't listen to port 4444");
      System.exit(-1);
    }

    try {
      System.out.print("Waiting for a client...");
      fromclient= servers.accept();
      System.out.println("Client connected");
    } catch (IOException e) {
      System.out.println("Can't accept");
      System.exit(-1);
    }

    in  = new BufferedReader(new InputStreamReader(fromclient.getInputStream()));
    out = new PrintWriter(fromclient.getOutputStream(),true);
    String input;

    System.out.println("Wait for messages");
    while ((input = in.readLine()) != null) {
     if (input.equalsIgnoreCase("exit")) break;
     out.println("S ::: "+input);
     System.out.println(input);
    }
    out.close();
    in.close();
    fromclient.close();
    servers.close();
  }
}

After, I run client.java:

public class client {
  public static void main(String[] args) throws IOException {

    System.out.println("Welcome to Client side");

    Socket fromserver = null;

    if (args.length==0) {
      System.out.println("use: client hostname");
      System.exit(-1);
    }

    System.out.println("Connecting to... "+args[0]);

    fromserver = new Socket(args[0], 4444);
    BufferedReader in  = new BufferedReader(new InputStreamReader(fromserver.getInputStream()));
    PrintWriter    out = new PrintWriter(fromserver.getOutputStream(),true);
    BufferedReader inu = new BufferedReader(new InputStreamReader(System.in));

    String fuser,fserver;

    while ((fuser = inu.readLine())!=null) {
      out.println(fuser);
      fserver = in.readLine();
      System.out.println(fserver);
      if (fuser.equalsIgnoreCase("close")) break;
      if (fuser.equalsIgnoreCase("exit")) break;
    }

    out.close();
    in.close();
    inu.close();
    fromserver.close();
  }
}

What should I do in course? If you see the code, I should to get out.println("S ::: "+input). How can i get it?


Solution

  • Be sure to start the server at first. From eclipse right click on Server.java and choose run -> as Java Application. Check if there is already a Server thread running. You can see that when you look at the Console view. If an applicationn is already running then a red filled button is shown. Next to it there is an down arrow button which when you click on it shows all running applications.

    It is important to run the server just one time since it uses the fixed port 4444. If the message " can not listen on port 4444" than certainly anothrr server thread already works in background. If the server not starts at all the problably a server running as service on your system blockd that port. You can change the port of course within code. But you have to do it within Server and Client side to the same.

    UPDATE The server prints out "Waiting for clients ...!" if it started successfully.

    After that you can run the client by clicking right on the Client.java and run -> as java application.

    UPDATE

    The client must be started with an argiment the hostname. In order to do than you have to open the run dialog in eclipse and choose the Client application. Switch to the second register in this dialog there you can add arguments to the jvm and application. Type in the application argument textfield the value 127.0.0.1 for localhost