您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關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中怎么利用TCP套接字實現多人聊天功能有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。