package com.socket.server; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * @info java_socket入門Demo3 * @author kxw 客戶端與服務器端通訊 * @see 參考資料 wind聖騎士blog */ public class TcpSocketServer { public static void main(String[] args) { try { // 建立服務器端套接字 ServerSocket serverSocket = new ServerSocket(3000); // 作一個循環一直監聽 while (true) { // 多客戶端可作socket集合 noi的chanelSocket作的更好 Socket socket = serverSocket.accept(); // 建立讀取的輸入流線程 Thread serverInputThread = new Thread(new ServerInputRunnable(socket)); // 建立寫入的輸入流線程 Thread serverOutputThread = new Thread(new ServerOutputRunnable(socket)); // 啓動線程 不停的讀和寫 serverInputThread.start(); serverOutputThread.start(); } } catch (Exception e) { e.printStackTrace(); } } }
package com.socket.server; import java.io.IOException; import java.io.InputStream; import java.net.Socket; /** * @author kxw * 服務器端___讀取客戶端數據的線程 */ public class ServerInputRunnable implements Runnable { private Socket socket; public ServerInputRunnable(Socket socket) { this.socket = socket; } @Override public void run() { try { // 得到輸入流 InputStream is = socket.getInputStream(); while (true) { byte[] bytes = new byte[1024]; int length = is.read(bytes); System.out.println("客戶端:"+new String(bytes,0,length)); } } catch (IOException e) { e.printStackTrace(); } } }
package com.socket.server; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; /** * @author kxw * 服務器端____向客戶端寫入數據的線程 */ public class ServerOutputRunnable implements Runnable { private Socket socket; public ServerOutputRunnable(Socket socket) { super(); this.socket = socket; } @Override public void run() { try { OutputStream os = socket.getOutputStream(); while (true) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in,"UTF-8")); String line = reader.readLine(); os.write(line.getBytes()); } } catch (IOException e) { e.printStackTrace(); } } }
//客戶端eclipsejava
package com.cnse.socket.client; import java.net.Socket; public class SocketClient { public static void main(String[] args) throws Exception { // 建立客戶點socket對象 鏈接服務器端socket 端口一致 Socket socket = new Socket("127.0.0.1", 3000); // 建立客戶端 獨立的寫的線程和讀的線程 Thread ClientInputThread = new Thread(new ClientInputRunnable(socket)); Thread ClientOutputThread = new Thread(new ClientOutputRunnable(socket)); ClientInputThread.start(); ClientOutputThread.start(); } }
package com.cnse.socket.client; import java.io.IOException; import java.io.InputStream; import java.net.Socket; public class ClientInputRunnable implements Runnable { private Socket socket; public ClientInputRunnable(Socket socket) { super(); this.socket = socket; } @Override public void run() { try { // 得到輸入流 InputStream is = socket.getInputStream(); while (true) { byte[] bytes = new byte[1024]; int length = is.read(bytes); System.out.println("服務器:"+new String(bytes,0,length)); } } catch (IOException e) { e.printStackTrace(); } } }
package com.cnse.socket.client; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; public class ClientOutputRunnable implements Runnable { private Socket socket; public ClientOutputRunnable(Socket socket) { super(); this.socket = socket; } @Override public void run() { try { OutputStream os = socket.getOutputStream(); while (true) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); String line = reader.readLine(); os.write(line.getBytes()); } } catch (IOException e) { e.printStackTrace(); } } }