java 中模擬TCP傳輸?shù)目蛻舳撕头?wù)端實例詳解
一、創(chuàng)建TCP傳輸?shù)目蛻舳?/strong>
1、建立TCP客戶端的Socket服務(wù),使用的是Socket對象,建議該對象一創(chuàng)建就明確目的地,即要連接的主機;
2、如果連接建立成功,說明數(shù)據(jù)傳輸通道已建立,該通道就是Socket流,是底層建立好的,既然是流,說著這里既有輸入流,又有輸出流,想要輸入流或者輸出流對象,可以通過Socket來獲取,可以通過getOutputStream()和getInputStream()來獲??;
3、使用輸出流,將數(shù)據(jù)寫出;
4、關(guān)閉Socket服務(wù)。
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
// 1、創(chuàng)建客戶端的Socket服務(wù)
Socket socket = new Socket("192.168.1.100", 10002);
// 2、獲取Socket流中輸入流
OutputStream out = socket.getOutputStream();
// 3、使用輸出流將指定的數(shù)據(jù)寫出去
out.write("TCP is coming !".getBytes());
// 4、關(guān)閉Socket服務(wù)
socket.close();
}
}
二、創(chuàng)建TCP傳輸?shù)姆?wù)端
1、建立TCP服務(wù)端的的Socket服務(wù),通過ServerSocket對象;
2、服務(wù)端必須對外提供一個端口,否則客戶端無法連接;
3、獲取連接過來的客戶端對象;
4、通過客戶端對象來獲取Socket流,讀取客戶端發(fā)來的數(shù)據(jù);
5、關(guān)閉資源,關(guān)客戶端,關(guān)服務(wù)端。
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
// 1、創(chuàng)建客戶端對象
ServerSocket ss = new ServerSocket(10002);
// 2、獲取連接過來的客戶端對象
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
// 3、通過Socket對象獲取輸入流,讀取客戶端發(fā)來的數(shù)據(jù)
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf, 0, len);
System.out.println(ip + ":" + text);
// 4、關(guān)閉資源
s.close();
ss.close();
}
}
三、優(yōu)化TCP傳輸?shù)目蛻舳撕头?wù)端
在本部分,我們對前兩部分的內(nèi)容進(jìn)行優(yōu)化,實現(xiàn)TCP傳輸模式下的客戶端和服務(wù)端的交互功能。
/**
*優(yōu)化TCP傳輸?shù)目蛻舳?
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ClientUpdate {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("192.168.1.100", 10002);
OutputStream out = socket.getOutputStream();
out.write("tcp!".getBytes());
// 讀取服務(wù)端返回的數(shù)據(jù),使用Socket讀取流
InputStream in = socket.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf, 0, len);
System.out.println(text);
socket.close();
}
}
/**
*優(yōu)化TCP傳輸?shù)姆?wù)端
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerUpdate {
public static void main(String[] args) throws IOException {
// 1、創(chuàng)建服務(wù)端對象
ServerSocket ss = new ServerSocket(10002);
// 2、獲取連接過來的客戶端對象
Socket s = ss.accept(); //accept方式為阻塞式方法
String ip = s.getInetAddress().getHostAddress();
// 3、通過Socket對象獲取輸入流,要讀取客戶端發(fā)來的數(shù)據(jù)
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
String text = new String(buf, 0, len);
System.out.println(ip + ":" + text);
// 使用客戶端的Socket對象的輸出流給客戶端返回數(shù)據(jù)
OutputStream out = s.getOutputStream();
out.write("收到".getBytes());
s.close();
ss.close();
}
}
四、創(chuàng)建英文大寫轉(zhuǎn)換服務(wù)器
應(yīng)用TCP(Transmission Control Protocol,傳輸控制協(xié)議)的相關(guān)性質(zhì),創(chuàng)建一個基于TCP傳輸下的英文大寫轉(zhuǎn)換服務(wù)器,要求:客戶端輸入字母數(shù)據(jù),發(fā)送給服務(wù)端;服務(wù)端收到數(shù)據(jù)后顯示在控制臺,并將該數(shù)據(jù)轉(zhuǎn)成大寫字母返回給客戶端;直到客戶端輸入“over”為止,轉(zhuǎn)換結(jié)束。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class TransClient {
public static void main(String[] args) throws IOException {
/**
* 思路:創(chuàng)建客戶端
* 1、創(chuàng)建Socket客戶端對象
* 2、獲取鍵盤錄入的數(shù)據(jù)
* 3、將錄入的信息發(fā)送給Socket輸出流
* 4、讀取服務(wù)端的數(shù)據(jù)并返回的大寫數(shù)據(jù)
*/
// 1、創(chuàng)建Socket客戶端對象
Socket s = new Socket("192.168.1.100", 10004);
// 2、獲取鍵盤錄入
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
// 3、Socket輸出流
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
// 4、Socket輸入流,讀取服務(wù)端的數(shù)據(jù)并返回的大寫數(shù)據(jù)
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while ((line = bufr.readLine()) != null) {
if ("over".equals(line))
break;
out.println(line);
// 讀取服務(wù)端返回的一行大寫數(shù)據(jù)
String upperStr = bufIn.readLine();
System.out.println(upperStr);
}
s.close();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TransServer {
public static void main(String[] args) throws IOException {
/**
* 思路:創(chuàng)建服務(wù)端
* 1、創(chuàng)建SeverSocket客戶端對象
* 2、獲取Socket流
* 3、通過Socket, 讀取客戶端發(fā)過來的需要轉(zhuǎn)換的數(shù)據(jù)
* 4、顯示在控制臺上
* 5、將數(shù)據(jù)轉(zhuǎn)換成大寫返回給客戶端
*/
// 1、創(chuàng)建SeverSocket對象
ServerSocket ss = new ServerSocket(10004);
// 2、獲取Socket對象
Socket s = ss.accept();
// 獲取IP地址
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip + "......connected");
// 3、獲取Socket讀取流,并裝飾
BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
// 4、獲取Socket的輸出流,并裝飾
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
String line = null;
while ((line = bufIn.readLine()) != null) {
System.out.println(line);
out.println(line.toUpperCase());
}
s.close();
ss.close();
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
MyBatis入門實例教程之創(chuàng)建一個簡單的程序
這篇文章主要介紹了MyBatis入門創(chuàng)建一個簡單的程序,在?MySQL?中創(chuàng)建數(shù)據(jù)庫?mybatisdemo,編碼為?utf8,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
SpringCLoud搭建Zuul網(wǎng)關(guān)集群過程解析
這篇文章主要介紹了SpringCLoud搭建Zuul網(wǎng)關(guān)集群過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03
Sentinel?Gateway自定義限流返回結(jié)果方式
這篇文章主要介紹了Sentinel?Gateway自定義限流返回結(jié)果方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
Springboot之@Async不執(zhí)行原因及分析
這篇文章主要介紹了Springboot之@Async不執(zhí)行原因及分析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
Java根據(jù)坐標(biāo)經(jīng)緯度計算兩點距離5種方法及校驗經(jīng)緯度是否在圓/多邊形區(qū)域內(nèi)的算法推薦
在項目開發(fā)過程中需要根據(jù)兩地經(jīng)緯度坐標(biāo)計算兩地間距離,下面這篇文章主要給大家介紹了關(guān)于Java根據(jù)坐標(biāo)經(jīng)緯度計算兩點距離5種方法以及校驗經(jīng)緯度是否在圓/多邊形區(qū)域內(nèi)的算法推薦,需要的朋友可以參考下2023-12-12
關(guān)于elasticsearch的match_phrase_prefix查詢詳解
這篇文章主要介紹了關(guān)于elasticsearch的match_phrase_prefix查詢問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
SpringBoot整合Shiro兩種方式(總結(jié))
這篇文章主要介紹了SpringBoot整合Shiro兩種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06

