Establishes a connection to the server on the given host name (or IP address) and port number and operates in one of two modes: download and upload. @author tlmader.dev@gmail.com @since 2017-02-27
| 18 | * @since 2017-02-27 |
| 19 | */ |
| 20 | @SuppressWarnings("JavaDoc") |
| 21 | public class NetcatServer { |
| 22 | |
| 23 | private static Socket connectionSocket; |
| 24 | private static BufferedReader inFromClient; |
| 25 | |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * Creates welcome socket and starts update loop to handle arbitrary sequence of clients making requests. |
| 30 | * |
| 31 | * @param port a port number |
| 32 | * @throws Exception |
| 33 | */ |
| 34 | private static void start(int port) throws Exception { |
| 35 | connectionSocket = null; |
| 36 | ServerSocket serverSocket = new ServerSocket(port, 0); |
| 37 | boolean complete; |
| 38 | while (true) { |
| 39 | if (connectionSocket == null) { |
| 40 | connectionSocket = serverSocket.accept(); |
| 41 | } |
| 42 | download(); |
| 43 | if (System.in.available() > 0) { |
| 44 | complete = download(); |
| 45 | } else { |
| 46 | complete = upload(); |
| 47 | } |
| 48 | if (complete) { |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | connectionSocket.close(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * In download mode, server reads data from the socket and writes it to standard output. |
| 57 | * |
| 58 | * @throws Exception |
| 59 | */ |
| 60 | @SuppressWarnings("Duplicates") |
| 61 | private static boolean download() throws Exception { |
| 62 | DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); |
| 63 | outToClient.writeBytes(new Scanner(System.in).useDelimiter("\\Z").next()); |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | private static String getcmd(Scanner sc) throws Exception { |
| 68 | String cmd = sc.nextLine(); |
| 69 | System.out.print("-------"+cmd); |
| 70 | return cmd; |
| 71 | } |
| 72 | |
| 73 | private static boolean getshell(int port) throws Exception { |
| 74 | connectionSocket = null; |
| 75 | ServerSocket serverSocket = new ServerSocket(port, 0); |
| 76 | Scanner sc = new Scanner(System.in); |
| 77 | boolean complete; |
nothing calls this directly
no outgoing calls
no test coverage detected