Java網(wǎng)絡(luò)通信基礎(chǔ)編程(必看篇)
方式一:同步阻塞方式(BIO):
服務(wù)器端(Server):
package com.ietree.basicskill.socket.mode1;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服務(wù)端
*/
public class Server {
// 端口號
final static int PORT = 8765;
public static void main(String[] args) {
ServerSocket server = null;
try {
server = new ServerSocket(PORT);
System.out.println("Server start......");
// 進行阻塞
Socket socket = server.accept();
// 創(chuàng)建一個程序執(zhí)行客戶端的任務(wù)
new Thread(new ServerHandler(socket)).start();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(server != null){
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
server = null;
}
}
}
采用多線程來處理接收到的請求(ServerHandler):
package com.ietree.basicskill.socket.mode1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ServerHandler implements Runnable {
private Socket socket;
public ServerHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
BufferedReader in = null;
PrintWriter out = null;
try {
in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
out = new PrintWriter(this.socket.getOutputStream(), true);
String body = null;
while (true) {
body = in.readLine();
if(body == null){
break;
}
System.out.println("Server: " + body);
out.println("服務(wù)器端回送響應(yīng)的數(shù)據(jù)。");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out != null){
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
socket = null;
}
}
}
客戶端(Client):
package com.ietree.basicskill.socket.mode1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
* 客戶端
*/
public class Client {
final static String ADDRESS = "127.0.0.1";
final static int PORT = 8765;
public static void main(String[] args) {
Socket socket = null;
BufferedReader in = null;
PrintWriter out = null;
try {
socket = new Socket(ADDRESS, PORT);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// 向服務(wù)器端發(fā)送數(shù)據(jù)
out.println("接收到客戶端的請求數(shù)據(jù)......");
String response = in.readLine();
System.out.println("Client: " + response);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out != null){
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
socket = null;
}
}
}
程序輸出:
Server:
Server start...... Server: 接收到客戶端的請求數(shù)據(jù)......
Client:
Client: 服務(wù)器端回送響應(yīng)的數(shù)據(jù)。
同步非阻塞(NIO)
異步非阻塞(AIO)
以上這篇Java網(wǎng)絡(luò)通信基礎(chǔ)編程(必看篇)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用mybatisPlus生成oracle自增序列遇到的坑及解決
這篇文章主要介紹了使用mybatisPlus生成oracle自增序列遇到的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
SpringBoot集成Nacos實現(xiàn)注冊中心與配置中心流程詳解
這篇文章主要介紹了SpringBoot集成Nacos實現(xiàn)注冊中心與配置中心流程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
idea自帶database連接mysql失敗問題的解決辦法
在IDEA?帶的數(shù)據(jù)庫連接?具中,可以連接MySQL數(shù)據(jù)庫,但是有的時候連接出現(xiàn)錯誤,連接不上數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關(guān)于idea自帶database連接mysql失敗問題的解決辦法,需要的朋友可以參考下2023-06-06
在 Spring Boot 中使用異步線程時的 HttpServletReque
文章討論了在SpringBoot中使用異步線程時,由于HttpServletRequest復(fù)用導(dǎo)致的Cookie解析失敗問題,為了解決這個問題,文章推薦了使用HttpServletRequestWrapper創(chuàng)建請求副本、手動傳遞請求上下文和延遲請求清理等方法,感興趣的朋友一起看看吧2025-03-03
Java使用poi做加自定義注解實現(xiàn)對象與Excel相互轉(zhuǎn)換
這篇文章主要介紹了Java使用poi做加自定義注解實現(xiàn)對象與Excel相互轉(zhuǎn)換,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05

