MCPcopy Create free account
hub / github.com/0linlin0/CyberBox / NetcatUDPClient

Class NetcatUDPClient

Skay_Exp/src/tools/nc/NetcatUDPClient.java:15–88  ·  view source on GitHub ↗

Binds to a specified port number on the local host and waits for a connection request from a client. Once a connection is established, it operates in one of two modes: download and upload. @author tlmader.dev@gmail.com @since 2017-02-27

Source from the content-addressed store, hash-verified

13 * @since 2017-02-27
14 */
15public class NetcatUDPClient {
16
17 private static DatagramSocket clientSocket;
18 private static InetAddress ipAddress;
19 private static boolean uploadMode;
20 private static boolean pinged;
21
22 /**
23 * Creates client socket makes request to the server.
24 *
25 * @throws Exception
26 */
27 @SuppressWarnings("InfiniteLoopStatement")
28 private static void start(String host, int port) throws Exception {
29 clientSocket = new DatagramSocket();
30 ipAddress = InetAddress.getByName(host);
31 if (System.in.available() > 0) {
32 uploadMode = true;
33 }
34 while (true) {
35 if (uploadMode) {
36 upload(port);
37 } else {
38 download(port);
39 }
40 }
41 }
42
43 /**
44 * In download mode, client reads data from the socket and writes it to standard output.
45 *
46 * @throws Exception
47 */
48 private static void download(int port) throws Exception {
49 if (!pinged) {
50 byte[] sendData = "".getBytes();
51 DatagramPacket pingPacket = new DatagramPacket(sendData, sendData.length, ipAddress, port);
52 clientSocket.send(pingPacket);
53 pinged = true;
54 }
55 byte[] receiveData = new byte[4096];
56 DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
57 clientSocket.receive(receivePacket);
58 System.out.println(new String(receivePacket.getData()).trim());
59 }
60
61 /**
62 * In upload mode, client reads data from its standard input device and writes it to the socket.
63 *
64 * @throws Exception
65 */
66 @SuppressWarnings("Duplicates")
67 private static void upload(int port) throws Exception {
68 Scanner input = new Scanner(System.in);
69 while (input.hasNextLine()) {
70 byte[] sendData = input.nextLine().getBytes();
71 clientSocket.send(new DatagramPacket(sendData, sendData.length, ipAddress, port));
72 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected