java實現網路監聽

來源:互聯網
上載者:User

標籤:java   網路監聽   電腦語言   socket   server   

                                                        Java實現網路監聽

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);    }  }}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 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實現網路監聽

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.