java Socket编程
生活随笔
收集整理的這篇文章主要介紹了
java Socket编程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 一、socket簡單客戶端與服務端
- 二、socket客戶端多次發送消息到服務端
- 三、socket多客戶端多次發送消息到服務端
一、socket簡單客戶端與服務端
客戶端發送消息給服務端,服務端接收到消息后回應客戶端。
客戶端:
import java.io.*; import java.net.Socket;public class MainClient1 {public static void main(String[] args) {try {//設置發送到的ip和端口Socket socket = new Socket("127.0.0.1", 9999);//從socket獲取輸出流到緩沖寫區,用來發送數據到服務端BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));//獲取Socket的輸入流,用來接收從服務端發送過來的數據DataInputStream in = new DataInputStream(socket.getInputStream());//緩沖讀區的內容寫入緩沖寫區,發送到服務端bufferedWriter.write("你好,我的第一個socket!");bufferedWriter.flush();socket.shutdownOutput();//獲取服務端返回的數據System.out.println(in.readUTF());} catch (IOException e) {e.printStackTrace();}} }服務端:
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket;public class MainServer {public static void main(String[] args) {try {//服務端綁定端口ServerSocket serverSocket = new ServerSocket(9999);//獲取客戶端連接(accept是阻塞的,如果有多個客戶端而服務端不用多線程會只獲取第一個連接的客戶端)Socket socket = serverSocket.accept();//獲取Socket的輸出流,用來向客戶端發送數據DataOutputStream out = new DataOutputStream (socket.getOutputStream());//獲取Socket的輸入流,用來接收從客戶端發送過來的數據BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));String str = reader.readLine();System.out.println("客戶端發來的消息:"+str);//發送消息到對應的客戶端out.writeUTF("服務端回復:已收到你的消息\"" + str+"\"");} catch (IOException e) {e.printStackTrace();}} }測試:
先啟動服務端,再啟動客戶端。
二、socket客戶端多次發送消息到服務端
客戶端從鍵盤可多次發送消息給服務端,服務端接每次收到消息后回應客戶端。
客戶端:
import java.io.*; import java.net.Socket;public class MainClient1 {public static void main(String[] args) {try {//設置發送到的ip和端口Socket socket = new Socket("127.0.0.1", 9999);//從socket獲取輸出流到緩沖寫區BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));//獲取Socket的輸入流,用來接收從服務端發送過來的數據DataInputStream in = new DataInputStream(socket.getInputStream());while (true) {//獲取鍵盤輸入的內容到緩沖讀區bufferedWriter.write(bufferedReader.readLine());bufferedWriter.write("\n");bufferedWriter.flush();//獲取服務端返回的數據System.out.println(in.readUTF());}} catch (IOException e) {e.printStackTrace();}} }服務端:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket;public class MainServer {public static void main(String[] args) {try {//服務端綁定端口ServerSocket serverSocket = new ServerSocket(9999);//獲取客戶端連接Socket socket = serverSocket.accept();//獲取Socket的輸出流,用來向客戶端發送數據DataOutputStream out = new DataOutputStream (socket.getOutputStream());//獲取Socket的輸入流,用來接收從客戶端發送過來的數據BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));String str ;while ((str = reader.readLine()) != null) {//輸出打印System.out.println("客戶端發來的消息:"+str);//發送消息到對應的客戶端out.writeUTF("服務端回復:已收到你的消息\"" + str+"\"");}} catch (IOException e) {e.printStackTrace();}} }測試:
先啟動服務端,再啟動客戶端,客戶端可多次輸入消息。
三、socket多客戶端多次發送消息到服務端
上面的例子是一個客戶端對應一個服務端,下面采用多線程對多個客戶端對應一個服務端進行示例說明。
客戶端1:
import java.io.*; import java.net.Socket;public class MainClient1 {public static void main(String[] args) {try {//設置發送到的ip和端口Socket socket = new Socket("127.0.0.1", 9999);//從socket獲取輸出流到緩沖寫區BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));//獲取Socket的輸入流,用來接收從服務端發送過來的數據DataInputStream in = new DataInputStream(socket.getInputStream());while (true) {//獲取鍵盤輸入的內容到緩沖讀區bufferedWriter.write(bufferedReader.readLine());bufferedWriter.write("\n");bufferedWriter.flush();//獲取服務端返回的數據System.out.println(in.readUTF());}} catch (IOException e) {e.printStackTrace();}} }客戶端2:
import java.io.*; import java.net.Socket;public class MainClient2 {public static void main(String[] args) {try {//設置發送到的ip和端口Socket socket = new Socket("127.0.0.1", 9999);//從socket獲取輸出流到緩沖寫區BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));//獲取Socket的輸入流,用來接收從服務端發送過來的數據DataInputStream in = new DataInputStream(socket.getInputStream());while (true) {//獲取鍵盤輸入的內容到緩沖讀區bufferedWriter.write(bufferedReader.readLine());bufferedWriter.write("\n");bufferedWriter.flush();//獲取服務端返回的數據System.out.println(in.readUTF());}} catch (IOException e) {e.printStackTrace();}} }服務端:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;public class MainServer {public static void main(String[] args) {try {//服務端綁定端口ServerSocket serverSocket = new ServerSocket(9999);//定長線程池ExecutorService executorService = Executors.newFixedThreadPool(100);while (true) {Socket socket = serverSocket.accept();//每當有一個服務端連接到服務器就新開一個線程處理executorService.execute(() -> {try {//socket中獲取輸入流到緩沖區BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));//獲取Socket的輸出流,用來向客戶端發送數據DataOutputStream out = new DataOutputStream (socket.getOutputStream());String str;//讀取緩沖區信息到str并輸出while (((str = reader.readLine()) != null)) {System.out.println("客戶端發來的消息:"+str);//發送消息到對應的客戶端out.writeUTF("服務端回復:已收到你的消息\"" + str+"\"");}} catch (IOException e) {e.printStackTrace();}});}} catch (IOException e) {e.printStackTrace();}} }測試:
先啟動服務端,再啟動兩個客戶端,兩個客戶端可多次輸入消息接受服務端的響應。
在idea中啟動多個main的方法技巧:
要啟動哪個就右上角切換哪個。
參考:
Java socket詳解,看這一篇就夠了
Java Socket 服務端發送數據 客戶端接收數據
Java TCP Socket 編程
總結
以上是生活随笔為你收集整理的java Socket编程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [包计划] node-fs-extra
- 下一篇: oracle instant clien