Java for network monitoring
Import Java.net.*;import java.io.*;p ublic class TCPServer {public static void main (String args[]) {int port; ServerSocket Server_socket; BufferedReader input; try {port = integer.parseint (Args[0]); } catch (Exception e) {System.out.println ("port = (default)"); Port = 1500;} try {server_socket = new ServerSocket (port); System.out.println ("Server Waiting for Client on port" + Server_socket.getlocalport ()); Server infinite Loop while (true) {Socket socket = server_socket.accept (); SYSTEM.OUT.PRINTLN ("New connection accepted" + socket.getinetaddress () + ":" + socket.getport ()); input = new BufferedReader (New InputStreamReader (Socket.getinputstream ())); Print received data try {while (true) {String message = Input.readline (); if (message==null) break; SYSTEM.OUT.PRINTLN (message); }} catch (IOException e) {System.out.println (e); } //Connection closed by client try {socket.close (); System.out.println ("Connection Closed by client"); } catch (IOException e) {System.out.println (e); }}} catch (IOException e) {System.out.println (e); }}}import java.net.*;import java.io.*;p ublic class TcpClient {public static void main (string[] args) {int port = 1500; String Server = "localhost"; Socket socket = NULL; String linetobesent; BufferedReader input; PrintWriter output; int ERROR = 1; Read arguments if (args.length = = 2) {server = Args[0]; try {port = integer.parseint (args[1]); } catch (Exception e) {System.out.println ("Server port = (default)"); Port = 1500; }}//Connect to Servertry {socket = new socket (server, port); System.out.println ("Connected with Server" + socket.getinetaddress () + ":" + socket.getport ());} catch (Unknownhostexception e) {System.out.println (e); System.exit (ERROR);} catch (IOEXception e) {System.out.println (e); System.exit (ERROR);} try {input = new BufferedReader (new InputStreamReader (system.in)); Output = new PrintWriter (Socket.getoutputstream (), true); Get user input and transmit it to server while (true) {linetobesent = Input.readline (); Stop if input line is "." if (Linetobesent.equals (".")) break; Output.println (linetobesent); }}catch (IOException e) {System.out.println (e);} try {socket.close ();} catch (IOException e) {System.out.println (e); } }}
Java for network monitoring