java中TCP傳輸協議

來源:互聯網
上載者:User

標籤:java   tcp   

class TcpClient {    public static void main(String[] args) throws Exception {        //建立用戶端的socket服務,指定目的主機和連接埠        Socket s = new Socket("192.168.1.10",10003);        //為了發送資料,擷取socket流中的輸出資料流        java.io.OutputStream out = s.getOutputStream();        out.write("hello tcp".getBytes());        s.close();    }}class TcpServer {    public static void main(String[] args) throws Exception {        //建立服務端socket服務,並監聽一個連接埠        ServerSocket ss = new ServerSocket(10003);        //通過accept方法擷取串連過來的用戶端對象        Socket s = ss.accept();        String ip = s.getInetAddress().getHostAddress();        //擷取用戶端發送過來的資料,使用用戶端對象的讀取流來讀取資料        InputStream in = s.getInputStream();        byte[] buf = new byte[1024];        int len = in.read(buf);        System.out.println(new String(buf,0,len));        //關閉用戶端        s.close();    }}class TcpClient2 {    public static void main(String[] args) throws Exception {        //建立socket服務,指定串連的主機和連接埠        Socket s = new Socket("192.168.1.10",10004);        //擷取socket流中的輸出資料流。將資料寫入該流,通過網路傳送給服務端        OutputStream out = (OutputStream) s.getOutputStream();        out.write("hello Tcp".getBytes());        //擷取socket流中的輸入資料流,將服務端反饋的資料擷取到,並列印        InputStream in = s.getInputStream();        byte[] buf = new byte[1024];        int len = in.read(buf);        System.out.println(new String(buf,0,len));        s.close();    }}class TcpServer2 {    public static void main(String[] args) throws Exception {        ServerSocket ss = new ServerSocket(10004);        Socket s = ss.accept();        //服務端收資訊        InputStream in = s.getInputStream();        String ip = s.getInetAddress().getHostAddress();        byte[] buf = new byte[1024];        int len = in.read(buf);        System.out.println(new String(buf,0,len));        //服務端發資訊        OutputStream out = (OutputStream) s.getOutputStream();        out.write("have receive".getBytes());        s.close();        ss.close();    }}/*需求:建立一個文本轉換伺服器用戶端給服務端發送文本,服務端會將文本轉成大寫再返還給用戶端*/class TcpClient3 {    public static void main(String[] args) throws Exception {        Socket s = new Socket("192.168.1.4",10005);        //定義讀取鍵盤資料的流對象,讀取鍵盤錄入文本        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));        //擷取socket流中的輸出資料流,通過網路傳送到服務端        BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));        //定義一個socket讀取流,讀取服務端返回的大寫資訊        BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));        String line = null;        while((line=bufr.readLine())!=null) {            if("over".equals(line))                break;            bufOut.write(line);            bufOut.newLine();//結束標記            bufOut.flush();            String str = bufIn.readLine();            System.out.println("server:"+str);        }        bufr.close();        s.close();        }}class TcpServer3 {    public static void main(String[] args) throws Exception {        ServerSocket ss = new ServerSocket(10005);        Socket s = ss.accept();        //讀取socket讀取流中的資料        BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));        //socket輸出資料流,將大寫文本寫入到socket輸出資料流,並發送給用戶端         BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));        String line = null;        while((line=bufIn.readLine())!=null) {            bufOut.write(line.toUpperCase());            bufOut.newLine();//結束標記            bufOut.flush();        }        s.close();        ss.close();    }}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

java中TCP傳輸協議

聯繫我們

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