本程式雖然不是很完善,但是基本要求達到了。
功能:實作多個人同時聊天。類似于群聊天室
Java代碼
- package com.xiva.bean;
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.List;
- public class SocketServer {
- public static List<Socket> socketList = new ArrayList<Socket>();
- public static int number = 0;
- public static void main(String[] args) {
- try{
- //綁定服務端在8080端口
- ServerSocket server = new ServerSocket(8080);
- Socket socket;
- int count = 0;
- while (true) {
- //監聽用戶端程式
- socket = server.accept();
- count = count + 1;
- System.out.println(socket.getInetAddress().getHostAddress());
- //一個用戶端連接配接後啟動一個線程進行監聽用戶端消息
- ReadThread readThread = new ReadThread(socket);
- readThread.start();
- socketList.add(socket);
- }
- } catch(IOException e){
- e.printStackTrace();
- }
- }
- }
- class ThreadSocketServer extends Thread {
- Socket server;
- public ThreadSocketServer(){
- }
- public ThreadSocketServer(Socket s) {
- this.server = s;
- }
- public void run() {
- try {
- BufferedReader br = new BufferedReader(new InputStreamReader(
- System.in));
- PrintWriter pw = new PrintWriter(server.getOutputStream());
- //循環發送資訊給用戶端
- while (true) {
- String s = br.readLine();
- pw.println(s);
- pw.flush();
- System.out.println("Server:" + s);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- class ReadThread extends Thread {
- private Socket socket;
- public ReadThread(Socket socket){
- this.socket = socket;
- }
- public void run() {
- try {
- BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
- String read=new String();
- //循環接受用戶端資訊
- while(true)
- {
- read=br.readLine();
- System.out.println(socket.getInetAddress().getHostAddress() +"client說:"+read);
- ArrayList<Socket> socketList = SocketServer.socketList;
- for(int i = 0; i < socketList.size(); i ++){
- Socket socket = socketList.get(i);
- PrintWriter pw = new PrintWriter(socket.getOutputStream());
- pw.println(read);
- pw.flush();
- }
- if(read.equals("bye"))
- {
- break;
- }
- }
- br.close();
- socket.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
[java] view plain copy print ?
- package com.xiva.bean;
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.List;
- public class SocketServer {
- public static List<Socket> socketList = new ArrayList<Socket>();
- public static int number = 0;
- public static void main(String[] args) {
- try{
- //綁定服務端在8080端口
- ServerSocket server = new ServerSocket(8080);
- Socket socket;
- int count = 0;
- while (true) {
- //監聽用戶端程式
- socket = server.accept();
- count = count + 1;
- System.out.println(socket.getInetAddress().getHostAddress());
- //一個用戶端連接配接後啟動一個線程進行監聽用戶端消息
- ReadThread readThread = new ReadThread(socket);
- readThread.start();
- socketList.add(socket);
- }
- } catch(IOException e){
- e.printStackTrace();
- }
- }
- }
- class ThreadSocketServer extends Thread {
- Socket server;
- public ThreadSocketServer(){
- }
- public ThreadSocketServer(Socket s) {
- this.server = s;
- }
- public void run() {
- try {
- BufferedReader br = new BufferedReader(new InputStreamReader(
- System.in));
- PrintWriter pw = new PrintWriter(server.getOutputStream());
- //循環發送資訊給用戶端
- while (true) {
- String s = br.readLine();
- pw.println(s);
- pw.flush();
- System.out.println("Server:" + s);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- class ReadThread extends Thread {
- private Socket socket;
- public ReadThread(Socket socket){
- this.socket = socket;
- }
- public void run() {
- try {
- BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
- String read=new String();
- //循環接受用戶端資訊
- while(true)
- {
- read=br.readLine();
- System.out.println(socket.getInetAddress().getHostAddress() +"client說:"+read);
- ArrayList<Socket> socketList = SocketServer.socketList;
- for(int i = 0; i < socketList.size(); i ++){
- Socket socket = socketList.get(i);
- PrintWriter pw = new PrintWriter(socket.getOutputStream());
- pw.println(read);
- pw.flush();
- }
- if(read.equals("bye"))
- {
- break;
- }
- }
- br.close();
- socket.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
不用說了上面是socket的server端。
标題中提到了,全局靜态變量;就是上面程式中的socketList。用來存儲每一個連接配接到server端的socket.
在read線程中,同時向所有的client端發送資訊。
其中類ThreadSocketServer ,可以啟動用來和使用者一對一的對話。
Java代碼
- package com.xiva.bean;
- import java.io.*;
- import java.net.*;
- public class TalkClient {
- public static void main(String args[]) {
- try{
- Socket socket=new Socket("192.168.0.64",8080);
- ReadString read = new ReadString(socket);
- read.start();
- BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
- PrintWriter os=new PrintWriter(socket.getOutputStream());
- String readline=sin.readLine();
- while(!readline.equals("bye")){
- os.println(readline);
- os.flush();
- System.out.println("Client:"+readline);
- readline=sin.readLine();
- }
- os.close();
- socket.close();
- }catch(Exception e) {
- System.out.println("Error"+e);
- }
- }
- }
- class ReadString extends Thread {
- private Socket socket;
- public ReadString(Socket socket){
- this.socket = socket;
- }
- public void run() {
- try {
- BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
- //循環接受服務端資訊
- while(true)
- {
- String read = is.readLine();
- System.out.println("Server:"+read);
- if(read.equals("bye"))
- {
- break;
- }
- }
- is.close();
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
[java] view plain copy print ?
- package com.xiva.bean;
- import java.io.*;
- import java.net.*;
- public class TalkClient {
- public static void main(String args[]) {
- try{
- Socket socket=new Socket("192.168.0.64",8080);
- ReadString read = new ReadString(socket);
- read.start();
- BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
- PrintWriter os=new PrintWriter(socket.getOutputStream());
- String readline=sin.readLine();
- while(!readline.equals("bye")){
- os.println(readline);
- os.flush();
- System.out.println("Client:"+readline);
- readline=sin.readLine();
- }
- os.close();
- socket.close();
- }catch(Exception e) {
- System.out.println("Error"+e);
- }
- }
- }
- class ReadString extends Thread {
- private Socket socket;
- public ReadString(Socket socket){
- this.socket = socket;
- }
- public void run() {
- try {
- BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
- //循環接受服務端資訊
- while(true)
- {
- String read = is.readLine();
- System.out.println("Server:"+read);
- if(read.equals("bye"))
- {
- break;
- }
- }
- is.close();
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
用戶端程式,在于啟動一個線程監視server端傳過來的message,同時可以不斷的發送資訊給server端。
這樣多人聊天室,便實作了。
擴充方向,可以在list中綁定其他資訊,比如userInfo。當用戶端連接配接時,發送給用戶端有哪些其它客戶線上。同樣綁定了userInfo後,這樣就可以for循環時,判斷線程中和那個使用者綁定了,也不需要得到線程裡面的具體資訊了。這樣一對一的聊天也很容易就實作了,這些隻要有了這個主幹下面的程式就好延伸了。
還有需要注意的就是線程存在的時間,我們可能需要寫一個定時器,不斷喚醒程式,以及讓其睡眠