在學習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();}}}
這僅僅是設計思路,有點簡陋,只能從控制台發送接收訊息