Java for network monitoring

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.