本程序虽然不是很完善,但是基本要求达到了。
功能:实现多个人同时聊天。类似于群聊天室
- 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();
- }
- }
- }
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 ,可以启动用来和用户一对一的对话。
- 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();
- }
- }
- }
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循环时,判断线程中和那个用户绑定了,也不需要得到线程里面的具体信息了。这样一对一的聊天也很容易就实现了,这些只要有了这个主干下面的程序就好延伸了。
还有需要注意的就是线程存在的时间,我们可能需要写一个定时器,不断唤醒程序,以及让其睡眠。