java中TCP總結

來源:互聯網
上載者:User

標籤:

先看一張圖,畫的很挫,將就看。

TCP 用戶端與服務端通訊時,是服務端會拿到用戶端的socket進行通訊。

TCP就相當於以前的有線電話,有一個耳機和一個話筒,A用話筒說話,B用耳機聽。

下面講講java中TCP的使用以及步驟。

TCP用戶端步驟:

  1.建立Socket服務,並確定IP和Port

  2.通過socket服務擷取輸入資料流或輸出資料流。

  3.通輸入或輸出資料流操作資料。

TCP服務端的步驟:

  1.用ServerSocket來監聽Port

  2.用accept擷取用戶端的Socket

  3.通用戶端的Socket來擷取輸入和輸出資料流。

  4.通過資料流傳輸。

用一個簡單一實例示範:

package com.core.net;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;class TcpClient{public static void main(String[] args) throws Exception {Socket s = new Socket("localhost", 10004);OutputStream out = s.getOutputStream();out.write("tcp come ...".getBytes());s.close();}}class TcpServer{public static void main(String[] args) throws Exception {ServerSocket ss = new ServerSocket(10004);Socket s = ss.accept();String ip = s.getInetAddress().getHostAddress();System.out.println(ip+".....connect");InputStream in = s.getInputStream();byte[] buf = new byte[1024];int len = in.read(buf);System.out.println(new String(buf,0,len));s.close();ss.close();}}public class TcpDemo {public static void main(String[] args) {}}

這個執行個體只是客服端發送資料,伺服器接收資料。

下面是一個例子是客服端發送資料,伺服器發送資料。

package com.core.net;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;class Client2{public static void main(String[] args) throws Exception {Socket socket = new Socket("localhost", 10004);OutputStream out =  socket.getOutputStream();out.write("服務端,你好".getBytes());InputStream in = socket.getInputStream();byte[] buf = new byte[1024];int len = in.read(buf);System.out.println(new String(buf,0,len));socket.close();}}class Server2{public static void main(String[] args) throws Exception {ServerSocket ss = new ServerSocket(10004);Socket s = ss.accept();InputStream in = s.getInputStream();byte[] buf = new byte[1024];int len = in.read(buf);System.out.println(new String(buf,0,len));OutputStream out = s.getOutputStream();Thread.sleep(10000);out.write("客服端,你好".getBytes());s.close();ss.close();}}public class TcpDemo2 {public static void main(String[] args) {}}

最後一個例子是服務端為大寫轉換器,客服端輸入資料,服務端將輸入的資料轉換大寫。

package com.core.net;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.ServerSocket;import java.net.Socket;class TransClient{public static void main(String[] args) throws Exception {Socket socket = new Socket("localhost", 10005);//鍵盤輸入BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));//socket輸入資料流BufferedReader bufin = new BufferedReader(new InputStreamReader(socket.getInputStream()));//socket輸出資料流BufferedWriter bufout = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));String line = null;while((line = reader.readLine())!=null){if("over".equals(line))break;bufout.write(line);bufout.newLine();//要注意 "\r\n"bufout.flush();String str = bufin.readLine();System.out.println("server:"+str);}reader.close();socket.close();}}class TransServer{public static void main(String[] args) throws Exception {ServerSocket ss = new ServerSocket(10005);     Socket s = ss.accept();     String ip = s.getInetAddress().getHostAddress();     System.out.println(ip+"......connection"); BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream())); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String line = null; while((line = reader.readLine())!=null){  System.out.println(line); out.write(line.toUpperCase()); out.newLine(); out.flush(); } s.close(); ss.close();}}public class TransDemo {public static void main(String[] args) {}}

 

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.