java實現的多人聊天(控制台輸入)

來源:互聯網
上載者:User

在學習android時,想做一個多人聊天室,不知如何動手

想先做一個純java的簡單多人聊天室,理理思路

通過Socket通訊端進行資料轉送

用到的知識點是:Socket,ServerSocket,  多線程

 

伺服器程式:

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;import java.net.URLDecoder;import java.net.UnknownHostException;import java.util.ArrayList;//這個程式是被串連的程式,俗稱伺服器程式//在程式界中,網路是指2個程式產生資訊互動,那麼構成網路//所以網路必須要有2個程式以上,一端叫伺服器,一端叫用戶端,通常伺服器和用戶端的對應關係是1-Npublic class SimpleServer {//用於儲存當前串連的使用者public static ArrayList<Socket> socketList=new ArrayList<Socket>();public static void main(String[] args)throws Exception {//建立一個serverSocket,用於監聽用戶端Socket的串連請求ServerSocket serverSocket = new ServerSocket(10002);//10002為此伺服器的連接埠號碼System.out.println("服務啟動");//採用迴圈不斷接收來自用戶端的請求while (true) {//每當接收到用戶端的請求時,伺服器端也對應產生一個SocketSocket socket = serverSocket.accept();System.out.println("服務串連");// 把串連的用戶端加入到list列表中socketList.add(socket);//啟動一個新的線程//任務線程,每個串連用都有一個屬於自己專用的任務線程//這個線程內會處理資訊的發送和響應new MyThread(socket,socketList).start();}/*BufferedReader br = new BufferedReader(new InputStreamReader(System.in));PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);// 把輸入的內容輸出到clientwhile (true) {String msg = br.readLine();pw.println("伺服器說:" + msg);}*/}}class MyThread extends Thread {Socket client;ArrayList<Socket> clients;BufferedReader br;public MyThread(Socket client,ArrayList<Socket> clients)throws Exception {super();this.client = client;this.clients = clients;br = new BufferedReader(new InputStreamReader(this.client.getInputStream()));}//把接收到的訊息發送給各用戶端public void run() {try {String content=null;while(true){//從某個用戶端讀取資訊if((content=br.readLine())!=null) {for(Socket socket:clients){if(client!=socket){//把讀取到的訊息發送給各個用戶端PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);pw.println(content);}}content = URLDecoder.decode(content, "UTF-8");System.out.println(content);}}}catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

用戶端程式:

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;import java.net.URLDecoder;import java.net.URLEncoder;public class Client {/** * @param args */public static void main(String[] args) throws Exception {// TODO Auto-generated method stub//建立串連到本機,連接埠為10002的socketSocket client = new Socket("192.168.3.26", 10002);new MyThread(client).start();InputStream is = client.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is));while (true) {String msg = br.readLine();//對收到的資訊進行解碼msg=URLDecoder.decode(msg, "UTF-8");System.out.println(msg);}}}class MyThread extends Thread {Socket client;public MyThread(Socket client) {super();this.client = client;}public void run() {// 發出訊息try {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));PrintWriter pw = new PrintWriter(client.getOutputStream(), true);// 把輸入的內容輸出到socketwhile (true) {String msg = br.readLine();//對發出的訊息進行編碼msg = URLEncoder.encode("張三說:" + msg, "UTF-8");pw.println(msg);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

這僅僅是設計思路,有點簡陋,只能從控制台發送接收訊息

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.