利用Java怎么樣實現(xiàn)網(wǎng)絡通信功能?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

創(chuàng)新互聯(lián)建站主營城廂網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,成都app開發(fā),城廂h5成都微信小程序搭建,城廂網(wǎng)站營銷推廣歡迎城廂等地區(qū)企業(yè)咨詢
方式一:同步阻塞方式(BIO):
服務器端(Server):
package com.ietree.basicskill.socket.mode1;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服務端
*/
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í)行客戶端的任務
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("服務器端回送響應的數(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);
// 向服務器端發(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: 服務器端回送響應的數(shù)據(jù)。
同步非阻塞(NIO)
異步非阻塞(AIO)
看完上述內容,你們掌握利用Java怎么樣實現(xiàn)網(wǎng)絡通信功能的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
當前標題:利用Java怎么樣實現(xiàn)網(wǎng)絡通信功能
分享網(wǎng)址:http://www.js-pz168.com/article32/jeodpc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、靜態(tài)網(wǎng)站、全網(wǎng)營銷推廣、自適應網(wǎng)站、網(wǎng)站收錄、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)