xickoh

SD socket server

Nov 22nd, 2020 (edited)
409
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package echoserver;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.io.PrintWriter;
  12. import java.net.ServerSocket;
  13. import java.net.Socket;
  14.  
  15. /**
  16.  *
  17.  * @author franc
  18.  */
  19. public class EchoServer {
  20.  
  21.     /**
  22.      * @param args the command line arguments
  23.      */
  24.     public static void main(String[] args) throws IOException {
  25.         ServerSocket serverSocket = null;
  26.         try {
  27.             serverSocket = new ServerSocket(7);
  28.         } catch (IOException e) {
  29.             System.err.println("Could not listen on port: 7");
  30.             System.exit(1);
  31.         }
  32.         Socket clientSocket = null;
  33.         try {
  34.             clientSocket = serverSocket.accept();
  35.         } catch (IOException e) {
  36.             System.err.println("Accept failed.");
  37.             System.exit(1);
  38.         }
  39.         PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
  40.         BufferedReader in = new BufferedReader(new InputStreamReader(
  41.                 clientSocket.getInputStream()));
  42.         String inputLine;
  43.         while ((inputLine = in.readLine()) != null) {
  44.             out.println(inputLine);
  45.         }
  46.         out.close();
  47.         in.close();
  48.         clientSocket.close();
  49.         serverSocket.close();
  50.     }
  51. }
  52.  
RAW Paste Data