Java使用TCP套接字實現多人聊天功能詳解
阿新 • • 發佈:2020-11-26
本文例項講述了Java使用TCP套接字實現多人聊天功能。分享給大家供大家參考,具體如下:
廢話不多說,直接開搞:
先建立一個服務端:
package interWebWriterEven2; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; /** * 服務端 * @author * */ public class ServiceT { static ArrayList<Socket> list = new ArrayList<Socket>(); public static void main(String[] args) { ServerSocket server; try { server = new ServerSocket(9999); while(true){ Socket socket = server.accept(); System.out.println(socket.getInetAddress().getHostName()+"連線成功。IP:"+socket.getInetAddress().getHostAddress()); list.add(socket); ServiceThreadT t =new ServiceThreadT(socket); t.start(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
接著是服務端的執行緒:
package interWebWriterEven2; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; /** * 服務端執行緒 * @author * */ public class ServiceThreadT extends Thread{ private Socket socket; public Socket getSocket() { return socket; } public void setSocket(Socket socket) { this.socket = socket; } public ServiceThreadT(Socket socket) { super(); this.socket = socket; } @Override public void run() { try { @SuppressWarnings("resource") BufferedWriter wFile = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D://messagesT.txt",true))); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String strOut = ""; String strIn = ""; while(true){ strIn = socket.getInetAddress().getHostName()+":"+br.readLine(); strOut = strIn; wFile.write(strIn); wFile.newLine(); wFile.flush(); for (Socket soc : ServiceT.list) { if(soc != socket){ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(soc.getOutputStream())); bw.write(strOut); System.out.println(strOut); bw.newLine(); bw.flush(); } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
然後是客戶端(應用端):
package interWebWriterEven2; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.Socket; /** * 客戶端 * @author * */ public class ClientT { public static void main(String[] args) { try { Socket socket = new Socket("127.0.0.1",9999); ClientThreadT t = new ClientThreadT(socket); t.start(); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); String strIn = ""; while(true){ strIn = br.readLine(); System.out.println(strIn); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
最後是客戶端的執行緒:
package interWebWriterEven2; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.Socket; import java.util.Scanner; /** * 客戶端的執行緒 * @author * */ public class ClientThreadT extends Thread{ private Socket socket; public Socket getSocket() { return socket; } public void setSocket(Socket socket) { this.socket = socket; } public ClientThreadT(Socket socket) { super(); this.socket = socket; } @Override public void run() { @SuppressWarnings("resource") Scanner sc = new Scanner(System.in); try { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); while(true){ System.out.println(); bw.write(sc.nextLine().trim()); bw.newLine(); bw.flush(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
先執行服務端哦,否則將會報空指標異常的。
更多關於java相關內容感興趣的讀者可檢視本站專題:《Java Socket程式設計技巧總結》、《Java檔案與目錄操作技巧彙總》、《Java資料結構與演算法教程》、《Java操作DOM節點技巧總結》和《Java快取操作技巧彙總》
希望本文所述對大家java程式設計有所幫助。