一個簡單的java聊天室

來源:互聯網
上載者:User

標籤:

 利用java Socket編寫的群聊室,可以自己拷過去試試


Server端:

package net3;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketException;import java.util.ArrayList;import java.util.List;public class Server {private List<ServerThread> clients = null;public static void main(String[] args) {new Server().startUp();}private void startUp() {ServerSocket ss = null;Socket s = null;try {ss = new ServerSocket(5858);clients = new ArrayList<ServerThread>();while (true) {s = ss.accept();ServerThread st = new ServerThread(s);new Thread(st).start();}} catch (IOException e) {e.printStackTrace();} finally {try {if (ss != null) ss.close();} catch (IOException e) {e.printStackTrace();}}}private class ServerThread implements Runnable {private Socket s = null;private BufferedReader br;private PrintWriter out;private String name;private boolean flag = true;public ServerThread(Socket socket) throws IOException {this.s = socket;br = new BufferedReader(new InputStreamReader(socket.getInputStream()));out = new PrintWriter(socket.getOutputStream(), true);String str = br.readLine();name = str+"["+socket.getInetAddress().getHostAddress()+":"+socket.getPort()+"]";clients.add(this);send(name+"上線了");}private void send(String msg) {for (ServerThread st : clients)st.out.println(msg);}private void receive() throws IOException {String str = null;while ((str=br.readLine()) != null) {if (str.equalsIgnoreCase("quit")) {stop();out.println("disconnect");break;}send(name+":"+str);}}private void stop() {clients.remove(this);flag = false;send(name+"已經下線了");}@Overridepublic void run() {try {while (true) {if (!flag) break;receive();}} catch (SocketException e) {stop();} catch (IOException e) {e.printStackTrace();} finally {try {if (s != null) s.close();} catch (IOException e) {e.printStackTrace();}}}}}


Client端

package net3;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;import java.net.UnknownHostException;public class Client {private Socket s;private BufferedReader br; private PrintWriter out;private boolean flag = true;public static void main(String[] args) {new Client().stratUp();}private void stratUp() {BufferedReader sbr = null;try {s = new Socket("127.0.0.1", 5858);out = new PrintWriter(s.getOutputStream(), true);br = new BufferedReader(new InputStreamReader(s.getInputStream()));out.println("老林");//out.println("老周");sbr = new BufferedReader(new InputStreamReader(System.in));new Thread(new ClientThread()).start();String str = null;while (flag && (str=sbr.readLine())!=null) {if (!flag) break;out.println(str);}} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (s != null) s.close();} catch (IOException e) {e.printStackTrace();}try {if (sbr != null) s.close();} catch (IOException e) {e.printStackTrace();}}}private void receive() {try {String rs = br.readLine();if (rs.equalsIgnoreCase("disconnect")) {flag = false;System.out.println("點擊斷行符號退出");}System.out.println(rs);} catch (IOException e) {e.printStackTrace();}}private class ClientThread implements Runnable {@Overridepublic void run() {while (true) {if (!flag) break;receive();}}}}


一個簡單的java聊天室

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.