Java implementation of network monitoring

Source: Internet
Author: User
Tags exit integer readline socket client port number server port
Network//Tcpserver.java by Fpont 3/2000





//Usage:java tcpserver <port number>.


//default port is 1500.


//connection to is closed by client.


//This server handles only 1 connection.





import java.net.*;


import java.io.*;





public 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 = 1500 (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);


}


}


}

















//Tcpclient.java by Fpont 3/2000





//Usage:java tcpclient <server> <port>


//default port is 1500





import java.net.*;


import java.io.*;





public 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 = 1000 (default)");


port = 1500;


}


}











//Connect to server


try {


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);


}


}


}














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.