標籤:
先看一張圖,畫的很挫,將就看。
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總結