Advertisement
lizabaranovskaya

Untitled

Mar 22nd, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. package LR1.server;
  2.  
  3. import javax.xml.crypto.Data;
  4. import java.io.DataInputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.IOException;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9. import java.util.Map;
  10.  
  11. public class Server implements Runnable {
  12.     private int port;
  13.  
  14.     public Server(int port) {
  15.         this.port = port;
  16.     }
  17.  
  18.     public void handleClient(Socket client) {
  19.         String message = null;
  20.         try {
  21.             DataInputStream input = new DataInputStream(client.getInputStream());
  22.             message = input.readUTF();
  23.             System.out.println("Received client's message: " + message);
  24.         } catch (IOException e) {
  25.             System.out.println("Failed to receive client's message");
  26.             e.printStackTrace();
  27.             return;
  28.         }
  29.  
  30.         String reversed = new StringBuilder(message).reverse().toString();
  31.  
  32.         try {
  33.             DataOutputStream outputStream = new DataOutputStream(client.getOutputStream());
  34.             outputStream.writeUTF(reversed);
  35.  
  36.             System.out.println("Sent response: " + reversed);
  37.             System.out.println("Client disconnected.");
  38.             System.out.println();
  39.         } catch (IOException ex) {
  40.             System.out.println("Couldn't send response: ");
  41.             ex.printStackTrace();
  42.             System.out.println();
  43.         }
  44.     }
  45.  
  46.     @Override
  47.     public void run() {
  48.         ServerSocket server;
  49.         try {
  50.             server = new ServerSocket(this.port);
  51.             System.out.println("Server is running...");
  52.             System.out.println();
  53.         } catch (IOException e) {
  54.             System.out.println("Couldn't start server: ");
  55.             e.printStackTrace();
  56.             return;
  57.         }
  58.  
  59.         while (true) {
  60.             Socket client = null;
  61.             try {
  62.                 client = server.accept();
  63.                 handleClient(client);
  64.                 client.close();
  65.             } catch (IOException e) {
  66.                 System.out.println("Couldn't wait for client's connection: ");
  67.                 e.printStackTrace();
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73.  
  74. package LR1.server;
  75.  
  76. public class ServerApp {
  77.     private static final int port = 8080;
  78.  
  79.     public static void main(String[] args) {
  80.         Server server = new Server(port);
  81.         server.run();
  82.     }
  83. }
  84.  
  85. package LR1.client;
  86.  
  87. import java.io.*;
  88. import java.net.ConnectException;
  89. import java.net.Socket;
  90.  
  91. public class Client {
  92.     private static final int port = 8080;
  93.  
  94.     private static Socket client;
  95.     private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  96.  
  97.     public static void handleMessage(String message) throws IOException {
  98.         DataOutputStream output = new DataOutputStream(client.getOutputStream());
  99.         DataInputStream input = new DataInputStream(client.getInputStream());
  100.  
  101.         output.writeUTF(message);
  102.  
  103.         String result = input.readUTF();
  104.         System.out.println("Server response: " + result);
  105.     }
  106.  
  107.     public static void main(String[] args) throws IOException {
  108.         try {
  109.             client = new Socket("localhost", port);
  110.             System.out.println("Connected");
  111.         } catch (ConnectException ex) {
  112.             System.out.println("Couldn't connect to the server");
  113.             return;
  114.         }
  115.  
  116.         System.out.print("Enter word: ");
  117.         String message = br.readLine();
  118.  
  119.         try {
  120.             handleMessage(message);
  121.         } catch (IOException ex) {
  122.             System.out.println("Couldn't send message to server.");
  123.         }
  124.  
  125.         client.close();
  126.         System.out.println("Connection closed.");
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement