黑马程序员技术交流社区
标题:
关于Socket多线程的学习-多线程聊天室
[打印本页]
作者:
backin
时间:
2015-8-17 10:43
标题:
关于Socket多线程的学习-多线程聊天室
今天在学socket,写了一个简单聊天室的代码实现服务器的广播
首先是一个ServerSocket启动类
package com.mycode.server;
import java.net.ServerSocket;
import java.net.Socket;
复制代码
然后就是一个线程处理类ClientThread
package com.mycode.server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ClientThread implements Runnable {
private Socket socket = null;
private ChatRoom chatroom = null;
private BufferedReader br = null;
private PrintWriter pw = null;
private String name = "";
public String getName() {
return name;
}
public Socket getSocket() {
return socket;
}
/***
* 构造一个客户端线程获得服务器的socket以及当前的Chatroom对象
*
* @param socket
* 服务器的socket
* @param chatroom
* 当前的Chatroom
*/
public ClientThread(Socket socket, ChatRoom chatroom) {
this.socket = socket;
this.chatroom = chatroom;
this.name = socket.getInetAddress().getHostName();
try {
br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
pw = new PrintWriter(socket.getOutputStream());
chatroom.in(this);
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
/***
* 说。。。
* @param content 内容
* */
public void clientSay(String content) {
pw.println(content);
pw.flush();
}
long lastTime = 0;
int count=0;
@Override
public void run() {
try {
while (true) {
String readLine = br.readLine();
if (System.currentTimeMillis() - lastTime < 3000) {
clientSay("兄弟禁言1分钟--测试3秒");
count++;
} else {
chatroom.say(this.name, readLine);
}
lastTime = System.currentTimeMillis();
if(count>=3)
{
chatroom.say("系统提示"+this.name, "恶意刷屏,ByeBye!");
br.close();
//socket.close();
}
if (readLine.equals("exit")) {
br.close();
}
}
} catch (Exception e) {
chatroom.out(this);
pw.close();
}
}
}
复制代码
最后就是个ChatRoom类作为聊天室的场地,服务器就搭建完毕咯
package com.mycode.server;
import java.util.ArrayList;
import java.util.List;
public class ChatRoom {
List<ClientThread> clients = new ArrayList<ClientThread>();//存储进入聊天室的客户端
String msg = "";
/***
* 进入聊天室提示信息
* * @param clientThread 获得一个客户端对象
**/
public void in(ClientThread clientThread) {
boolean add = clients.add(clientThread);
if (add) {
msg = clientThread.getName() + "进入聊天室!";
NoteALL(msg);
if (clients.size() > 0) {
System.out.println("当前在线:"+clients.size()+"人");
showAllClient();
}
}
}
public void out(ClientThread ct) {
boolean remove = clients.remove(ct);
if (remove) {
msg = ct.getName() + "退出聊天室!";
NoteALL(msg);
if (clients.size() > 0) {
System.out.println("当前在线:"+clients.size()+"人");
showAllClient();
}
}
}
private void showAllClient() {
for (ClientThread clientThread : clients) {
System.out.println(clientThread.getName());
}
}
private synchronized void NoteALL(String msg) {
for (ClientThread clientThread : clients) {
clientThread.clientSay(msg);
}
System.out.println(msg);
}
public synchronized void say(String name, String content) {
msg = name + " Say:" + content;
NoteALL(msg);
}
}
复制代码
启动MainFrame开启服务器。服务端搞定下面就是客户端了
package com.mycode.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
/***
* 客户端
* * @author backin
* */
public class Client {
static PrintWriter pw = null;
static BufferedReader br = null;
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
String IPADDRESS = "127.0.0.1";
int PORT = 9990;
try {
Socket socket = new Socket(IPADDRESS, PORT);
pw = new PrintWriter(socket.getOutputStream());
br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
System.out.print(socket.getLocalPort());//获取当前与服务器通信的端口,非连接端口
new Thread() {
public void run() {//输入到程序端线程,避免影响主线程
try {
System.out.println(br.readLine());
} catch (IOException e) {
System.out.println("Server is missing...");
return;
}
};
}.start();
new Thread() {//输出到服务器线程避免影响主线程
public void run() {
while (true) {
String str = input.next();
pw.println(str);
pw.flush();
if (str.equals("exit")) {
pw.close();
}
}
}
}.start();
} catch (UnknownHostException e) {
System.out.println("Server is not found!"+e.getMessage());
} catch (IOException e) {
System.out.println("IO error!"+e.getMessage());
}
}
}
复制代码
OK程序有点ugly,大伙来帮忙完善下,交流交流
作者:
backin
时间:
2015-8-17 10:44
有板凳自己做,格式怎么个这样子,哎真心很...
作者:
boboyuwu
时间:
2015-8-17 18:18
你可以写成图形界面 弄个客户端和服务器端相互发信息相信会有趣很多
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2