Home java a simple client-server application

a simple client-server application

Author

Date

Category

Java is generally considered the language sharpened for working with internet / online, but after reading a few books on the Java, I have not found an example of a simple client-server application.

Please show me a simple example of a client application sends an object (string String , for example) to the server, and the server application, accept it.


Answer 1, Authority 100%

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 ( "Could not 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 not 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 as an example Echo server (Echo Server). The client gets back a string sent to the server.

The client side uses a command line parameter to specify the hostname. For example, if you run a server and client on the same computer, it is necessary to run the client so:

java client localhost

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions