java–網路編程(一)

來源:互聯網
上載者:User

一般涉及到網路的應用,都會提到兩個‘端’,一個提供服務叫服務端;一個申請服務叫用戶端。一台伺服器電腦可以通過服務程式向外提供多個服務(如http/ftp)。作為一台外網伺服器,也就是一個服務端,它必須有一個固定的IP,我們就是通過這個IP訪問到這台電腦,要想訪問一個特定的服務,就得指定連接埠號碼,這是區分不同服務的關鍵,1-1024為系統自留連接埠。

ServerSocket:偵聽所有的用戶端向服務端發送的建立通訊端請求,然後返回通訊端對象Socket,用戶端和服務端的串連建立。

構建一個ServerSocket對象,需要指定一個連接埠號碼,偵聽到串連請求,獲得一個Socket對象,建立Socket---Socket的串連

 

import java.io.*;

import java.net.*;

public class JabberServer{

  public static fianl int port=8080;

  public static void main(String[] args){

    ServerSocket ss=new ServerSocket(port);

    System.out.println(ss);

    try{

      Socket s=ss.accept();

      System.out.println(s);

      try{

        BufferedReader br=new BufferedReader(

                    new InputStreamReader(

                      s.getInputStream()

                    )

                  );

        //建立串連後,向伺服器寫入輸出內容,只有當輸入內容緩衝每次被重新整理,才能通過網路發送下一批訊息

        PrintWriter pw=new PrintWriter(

                  new BufferedWriter(

                    new OutputStreamWriter(

                      s.getOutputStream();

                    )

                  )

                ,true);

        String str=new String();

        int i=0;

        while(true){

          str=br.readLine();

          System.out.println(str);

          pw.println(++i+"content from server");

          if(str.equals("END")

            break;

        }

      }finally{

        System.out.println("Socket close");

        s.close();

      }

    }finally{

      System.out.println("ServerSocket close");

      ss.close();

    }

  }

}

 

 

import java.io.*;

import java.net.*;

 

public class JabberClient{

 

  public static void main(String[] args) thrwos Exception{

    InetAddress add=InetAddress.getByName(null);

    //InetAddress add=InetAddress.getByName("localhost");

    //InetAddress add=InetAddress.getByName("127.0.0.1");

    System.out.println(add);

    Socket s=new Socket(add,JabberServer.port);

    System.out.println(s);

    try{

      //建立串連後,向伺服器寫入輸出內容,只有當輸入內容緩衝每次被重新整理,才能通過網路發送下一批訊息

      PrintWriter pw=new PrintWriter(

                new BufferedWriter(

                  new OutputStreamWriter(

                    s.getOutputStream()

                  )

                )

              ,true);

      //獲得伺服器返回的訊息

 

      BufferedReader br=new BufferedReader(

                  new InputStreamReader(

                    s.getInputStream()

                  )

                );

 

      String str=new String();

      for(int i=0;i<10;i++){

        pw.println(i+"content from client");

        str=br.readLine();

        System.out.println(str);

      }

      //告訴服務端,用戶端內容寫入完畢

      pw.println("END");

    }finally{

      System.out.println("Socket close");

      s.close();

    }

  }

}

 

/**--注意--**/

以上代碼都是在文字編輯器中寫的,可能會有些許紕漏

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.