网络编程:TCP简单实现群聊功能
生活随笔
收集整理的這篇文章主要介紹了
网络编程:TCP简单实现群聊功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用TCP同時加入多線程和容器實現群聊功能。
在服務端加入多線程,每當連接一個客戶端就相當于為這個客戶端開辟一個可以讀信息和發送信息的服務。要實現群聊需要借助容器,有客戶端連接時,把這個客戶端放入容器,發送信息時遍歷這個容器,向這個容器內的客戶端逐個發送消息。
在客戶端加入多線程,想要實現客戶端的隨時發消息和隨時收消息可以加入一個發送線程和接收線程。發送線程通過IO流向服務端發送線程,接收線程通過IO流接收服務端發送過來的線程。
package Net.TCP;import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.CopyOnWriteArrayList;/*** 服務端* * @author zhaoy**/ public class Test02_server{private static CopyOnWriteArrayList<Channel> all = new CopyOnWriteArrayList<>();public static void main(String[] args) throws Exception {System.out.println("======服務端========");//使用ServerSocket指定端口創建服務器ServerSocket server = new ServerSocket(9999);//阻塞式等待連接 while(true) {Socket client = server.accept();Channel c = new Channel(client);System.out.println("有一個客戶端連接");all.add(c);new Thread(c).start();}}static class Channel implements Runnable{private Socket client;private DataInputStream dis;private DataOutputStream dos;public Channel(Socket client) {this.client = client;try {dos = new DataOutputStream(client.getOutputStream());dis = new DataInputStream(client.getInputStream());} catch (IOException e) {e.printStackTrace();}}public String receive() {String msg = "";try {msg = dis.readUTF();} catch (IOException e) {e.printStackTrace();}return msg;}public void send(String msg) {System.out.println(msg);try {dos.writeUTF(msg);} catch (IOException e) {e.printStackTrace();}}private void sendOthers(String msg,boolean isSys) {for (Channel other : all) {if(other==this) {continue;}other.send(msg);}}@Overridepublic void run() {while(true) {String msg = receive();if(!msg.equals("")) {sendOthers(msg, false);}}} } } package Net.TCP;import java.net.Socket;/*** 客戶端* * @author zhaoy**/ public class Test02_client {public static void main(String[] args) throws Exception {System.out.println("------客戶端 -------");// 使用Socket 指定服務器的地址和端口Socket client = new Socket("localhost", 9999);// 發送消息new Thread(new Test02_send(client)).start();// 接收消息new Thread(new Test02_receive(client)).start();} } package Net.TCP;import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket;/*** 發送* * @author zhaoy**/ public class Test02_send implements Runnable {private BufferedReader console;private DataOutputStream dos;private Socket client;public Test02_send(Socket client) {this.client = client;try {console = new BufferedReader(new InputStreamReader(System.in));dos = new DataOutputStream(client.getOutputStream());} catch (IOException e) {e.printStackTrace();}}@Overridepublic void run() {while (true) {try {String msg = console.readLine();dos.writeUTF(msg);dos.flush();} catch (IOException e) {e.printStackTrace();}}} } package Net.TCP;import java.io.DataInputStream; import java.io.IOException; import java.net.Socket;/*** 接收* * @author zhaoy**/ public class Test02_receive implements Runnable{private DataInputStream dis;private Socket client;public Test02_receive(Socket client) {this.client = client;try {dis = new DataInputStream(client.getInputStream());} catch (IOException e) {e.printStackTrace();}}@Overridepublic void run() {while(true) {String msg = "";try {msg = dis.readUTF();System.out.println(msg);} catch (IOException e) {e.printStackTrace();}}} }?
總結
以上是生活随笔為你收集整理的网络编程:TCP简单实现群聊功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java中生成随机数的4种方式!
- 下一篇: JAVA 随机数学习