標籤:線程 next start ade arraylist rem nbsp style ip地址
1、用戶端MyClient.java
1 import java.io.*; 2 import java.net.*; 3 4 public class MyClient 5 { 6 public static void main(String[] args)throws Exception 7 { 8 Socket s = new Socket("192.168.1.1" , 30000); 9 // 用戶端啟動ClientThread線程不斷讀取來自伺服器的資料10 new Thread(new ClientThread(s)).start();11 // 擷取該Socket對應的輸出資料流12 PrintStream ps = new PrintStream(s.getOutputStream());13 String line = null;14 // 不斷讀取鍵盤輸入15 BufferedReader br = new BufferedReader(16 new InputStreamReader(System.in));17 while ((line = br.readLine()) != null)18 {19 // 將使用者的鍵盤輸入內容寫入Socket對應的輸出資料流20 ps.println(line);21 }22 }23 }
2、加入多線程,ClientThread.java
1 import java.io.*; 2 import java.net.*; 3 4 public class ClientThread implements Runnable 5 { 6 // 該線程負責處理的Socket 7 private Socket s; 8 // 該線程所處理的Socket所對應的輸入資料流 9 BufferedReader br = null;10 public ClientThread(Socket s)11 throws IOException12 {13 this.s = s;14 br = new BufferedReader(15 new InputStreamReader(s.getInputStream()));16 }17 public void run()18 {19 try20 {21 String content = null;22 // 不斷讀取Socket輸入資料流中的內容,並將這些內容列印輸出23 while ((content = br.readLine()) != null)24 {25 System.out.println(content);26 }27 }28 catch (Exception e)29 {30 e.printStackTrace();31 }32 }33 }
3、伺服器端MyServer.java
1 import java.net.*; 2 import java.io.*; 3 import java.util.*; 4 5 public class MyServer 6 { 7 // 定義儲存所有Socket的ArrayList 8 public static ArrayList<Socket> socketList 9 = new ArrayList<Socket>();10 public static void main(String[] args)11 throws IOException12 {13 System.out.println("222222……");14 ServerSocket ss = new ServerSocket15 16 (30000);17 while(true)18 {19 System.out.println("伺服器等待連結……");20 // 此行代碼會阻塞,將一直等待別人的串連21 Socket s = ss.accept();22 socketList.add(s);23 // 每當用戶端串連後啟動一條ServerThread線程為該用戶端服務24 new Thread(new ServerThread25 26 (s)).start();27 }28 }29 }
4、為伺服器端通訊加入多線程
1 import java.io.*; 2 import java.net.*; 3 import java.util.*; 4 5 // 負責處理每個線程通訊的線程類 6 public class ServerThread implements Runnable { 7 // 定義當前線程所處理的Socket 8 Socket s = null; 9 // 該線程所處理的Socket所對應的輸入資料流10 BufferedReader br = null;11 12 public ServerThread(Socket s) throws IOException {13 System.out.println("4444444……");14 this.s = s;15 // 初始化該Socket對應的輸入資料流16 String ip=s.getInetAddress().getHostAddress();17 System.out.println("ip地址為"+ip+"........connected!!!");18 br = new BufferedReader(new InputStreamReader(s.getInputStream(),19 "utf-8")); // 20 }21 22 public void run() {23 System.out.println("555555……");24 try {25 System.out.println("4444444……");26 String content = null;27 // 採用迴圈不斷從Socket中讀取用戶端發送過來的資料28 while ((content = readFromClient()) != null) {29 /*System.out.println("---"30 + Arrays.toString(content.getBytes("utf-8")));*/31 System.out.println("---" +"用戶端發來的資料為"+content);32 System.out.println("===============");33 // 遍曆socketList中的每個Socket,34 // 將讀到的內容向每個Socket發送一次35 for (Iterator<Socket> it = MyServer.socketList.iterator(); it36 .hasNext();) {37 Socket s = it.next();38 try {39 System.out.println("666666");40 OutputStream os = s.getOutputStream();41 os.write((content+"伺服器反饋給用戶端ok" + "\n").getBytes("utf-8"));42 System.out.println("777777");43 } catch (SocketException e) {44 e.printStackTrace();45 // 刪除該Socket。46 it.remove();47 System.out.println(MyServer.socketList);48 }49 }50 }51 } catch (IOException e) {52 e.printStackTrace();53 }54 }55 56 // 定義讀取用戶端資料的方法57 private String readFromClient() {58 try {59 return br.readLine();60 }61 // 如果捕捉到異常,表明該Socket對應的用戶端已經關閉62 catch (IOException e) {63 e.printStackTrace();64 System.out.println("用戶端已經關閉!!!");65 // 刪除該Socket。66 MyServer.socketList.remove(s); 67 }68 return null;69 }70 }
Java網路編程之tcp的socket通訊