前言
本篇博客主要是分享,socket網絡編程進階與實踐☞socket網絡編程快速入門
一、聊一聊Socket
學習目標與收穫
案例
網絡編程
- 歸納爲:
- 對應關係
二、Socket與TCP、UDP
TCP
UDP
Client-Server Application
三、Socket TCP牛刀小試-客戶端實現
- 構建TCP客戶端,服務端
- 客戶端發送數據
- 服務器讀取數據並打印
client
package com.zcw.demo;
import java.io.*;
import java.net.*;
/**
* @ClassName : Client
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-07-07 10:09
*/
public class Client {
public static void main(String[] args) throws IOException {
//初始化socket
Socket socket = new Socket();
//設置超時時間
socket.setSoTimeout(3000);
//設置連接的超時時間與連接的端口號
socket.connect(new InetSocketAddress(Inet4Address.getLocalHost(),2000),3000);
System.out.println("已發起服務器連接,並進入後續流程");
System.out.println("客戶端信息:"+socket.getLocalAddress()+"P:"+socket.getLocalPort());
System.out.println("服務器信息:"+socket.getInetAddress()+"P:"+socket.getPort());
try{
//發送接收數據
todo(socket);
}catch(Exception e){
System.out.println("異常關閉");
}
//釋放資源
socket.close();
System.out.println("客戶端已退出");
}
//發送數據
private static void todo(Socket client) throws IOException{
//構建鍵盤輸入流
InputStream in = System.in;
BufferedReader input = new BufferedReader(new InputStreamReader(in));
//客戶端輸出流(得到Socket輸出流,並轉換爲打印流)
OutputStream outputStream = client.getOutputStream();
PrintStream socketPrintStream = new PrintStream(outputStream);
//得到Socket輸入流,並轉換爲BufferedReader
InputStream inputStream = client.getInputStream();
BufferedReader socketBufferedReader = new BufferedReader(new InputStreamReader(inputStream));
boolean flag = true;
do{
//從鍵盤讀取一行
String str= input.readLine();
//然後把讀取的這一行,發送出去,到服務器
socketPrintStream.println(str);
//從服務器讀取一行
String echo = socketBufferedReader.readLine();
if("bye".equalsIgnoreCase(echo)){
flag = false;
}else{
System.out.println(echo);
}
}while (flag);
//資源釋放
socketPrintStream.close();
socketBufferedReader.close();
}
}
server
package com.zcw.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @ClassName : Server
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-07-07 10:08
*/
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(2000);
System.out.println("服務器準備就緒");
System.out.println("服務器信息:"+server.getInetAddress()+"p:"+server.getLocalPort());
//等待客戶端連接
for(;;){
//得到客戶端
Socket client = server.accept();
//客戶端構建異步線程
ClientHandler clientHandler = new ClientHandler(client);
//啓動線程
clientHandler.start();
}
}
//多個客戶端時,需要進行異步操作處理
private static class ClientHandler extends Thread{
private Socket socket;
private boolean flag =true;
ClientHandler(Socket socket){
this.socket = socket;
}
@Override
public void run(){
super.run();
System.out.println("新客戶端連接:"+socket.getInetAddress()+"p:"+socket.getPort());
try{
//得到打印流,用戶數據輸出,服務器會送數據使用
PrintStream socketOutput = new PrintStream(socket.getOutputStream());
//得到輸入流,用於接收數據
BufferedReader socketInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));
do{
//客戶端,拿到一條數據
String str = socketInput.readLine();
if("bye".equalsIgnoreCase(str)){
flag = false;
//回送
socketOutput.println("bye");
}else{
//打印到屏幕,並回送數據長度
System.out.println(str);
socketOutput.println("回送:"+str.length());
}
}while(flag);
socketInput.close();
socketOutput.close();
}catch(Exception e){
System.out.println("連接異常斷開");
}finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("客戶端已關閉:"+socket.getInetAddress()+"P:"+socket.getPort());
}
}
}
- 測試
- 輸入數據
四、報文、協議、Mac地址
五、IP、端口及遠程服務器