The simplest client-server application [closed]

Closed. It is impossible to give an objective answer to this question . Answers to it are not accepted at the moment.

Want to improve this question? Reformulate the question so that it can be answered based on facts and quotations.

Closed 4 years ago.

Improve the question

Java is usually considered a language that is sharpened for working with the Internet/on the Internet, but after reading several books on Java, I never found an example the simplest client-server application.

Please show an example of a simple client application sending an object (the string String, for example) to the server and the server application that accepts it.

Author: arachnoden, 2016-07-19

1 answers

Server

import java.io.*;
import java.net.*;

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,output;

    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();
  }
}

Client

import java.io.*;
import java.net.*;

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();
  }
}

The client-server is organized on the example of Echo server (Echo server). The client gets back the string passed to the server.

The client side uses a command-line parameter to specify the host name. For example, if you run the server and the client on the same computer, then the client should be run like this:

java client localhost
 6
Author: Senior Pomidor, 2016-07-19 06:39:08