局域网聊天系统代码
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
client.getOutputStream());
oos.writeObject(serverBean);
oos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
onlines.remove(bean.getName());
try {
ss = new ServerSocket(8520);
onlines = new HashMap<String, ClientBean>();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
cbean.setName(bean.getName());
cbean.setSocket(client);
//添加在线用户
onlines.put(bean.getName(), cbean);
//创建服务器的bean,并发送给客户端
Bean serverBean = new Bean();
serverBean.setType(0);
private String name;
private Socket socket;
public String getName() {
return name;
}
public void setName(String namecket getSocket() {
+ "向你传送文件,是否需要接受";
serverBean.setType(2);
serverBean.setClients(bean.getClients()); //这是发送的目的地
serverBean.setFileName(bean.getFileName()); //文件名称
serverBean.setSize(bean.getSize()); //文件大小
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
Clientbean
package Server;
import .Socket;
public class ClientBean {
set.addAll(onlines.keySet());
serverBean2.setClients(set);
sendAll(serverBean2);
return;
}
case 1: { //聊天
//创建服务器的catbean,并发送给客户端
Bean serverBean = new Bean();
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
Bean serverBean = new Bean();
serverBean.setType(3);
serverBean.setClients(bean.getClients()); //文件来源
serverBean.setTo(bean.getTo()); //文件目的地
serverBean.setFileName(bean.getFileName()); //文件名称
private int type; // 1私聊0上下线更新-1下线请求2请求发送文件3.确定接收文件
private HashSet<String> clients; //存放选中的客户
private HashSet<String> to;
public HashMap<String, Bean> onlines;
return onlines;
}
public void setOnlines(HashMap<String, Bean> onlines) {
this.onlines = onlines;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
serverBean.setInfo(bean.getTimer() + " "
+ bean.getName() + "上线了");
//通知所有客户有人上线
HashSet<String> set = new HashSet<String>();
//客户昵称
set.addAll(onlines.keySet());
}
public void setName(String name) {
= name;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
this.client = client;
}
@Override
public void run() {
try {
//不停的从客户端接收信息
while (true) {
//读取从客户端接收到的bean信息
ois = new ObjectInputStream(client.getInputStream());
serverBean.setType(1);
serverBean.setClients(bean.getClients());
serverBean.setInfo(bean.getInfo());
serverBean.setName(bean.getName());//接收方的名称
serverBean.setTimer(bean.getTimer());
this.clients = clients;
}
public HashSet<String> getTo() {
return to;
}
public void setTo(HashSet<String> to) {
this.to = to;
}
public HashMap<String, Bean> getOnlines() {
serverBean.setInfo(info);
serverBean.setName(bean.getName()); //来源
serverBean.setTimer(bean.getTimer());
//向选中的客户发送数据
sendMessage(serverBean);
break;
}
case 3: { //确定接收文件
serverBean.setFileName(bean.getFileName());
return type;
}
public void setType(int type) {
this.type = type;
}
public HashSet<String> getClients() {
return clients;
}
public void setClients(HashSet<String> clients) {
import Client.Bean;
import Client.ClientBean;
public class Server {
private static ServerSocket ss;
public static HashMap<String, ClientBean> onlines;
static {
= info;
}
public String getTimer() {
return timer;
}
public void setTimer(String timer) {
this.timer = timer;
}
public String getName() {
return name;
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
}
Server
package Server;
import java.io.*;
import .*;
import java.util.*;
bean = (Bean)ois.readObject();
//分析bean中,type是那样一种类型
switch (bean.getType()) {
//上下线更新
case 0: { //上线
//记录上线客户的用户名和端口在clientbean中
ClientBean cbean = new ClientBean();
Server
Bean
package Server;
import .Socket;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
public class Bean implements Serializable{
serverBean.setClients(set);
sendAll(serverBean);
break;
}
case -1: { //下线
//创建服务器的catbean,并发送给客户端
Bean serverBean = new Bean();
serverBean.setType(-1);
try {
oos = new ObjectOutputStream(
}
}
class CatClientThread extends Thread {
private Socket client;
private Bean bean;
private ObjectInputStream ois;
private ObjectOutputStream oos;
public CatClientThread(Socket client) {
//向选中的客户发送数据
sendMessage(serverBean);
break;
}
case 2: { //请求接受文件
//创建服务器的catbean,并发送给客户端
Bean serverBean = new Bean();
String info = bean.getTimer() + " " + bean.getName()
private String info;
private String timer;
private String name;
private String fileName;
private int size;
private String ip;
private int port;
public int getType() {
serverBean.setIp(bean.getIp());
serverBean.setPort(bean.getPort());
serverBean.setName(bean.getName()); //接收的客户名称
serverBean.setTimer(bean.getTimer());
//通知文件来源的客户,对方确定接收文件
sendMessage(serverBean);
break;
}
case 4: {
Bean serverBean = new Bean();
serverBean.setType(4);
serverBean.setClients(bean.getClients()); //文件来源
serverBean.setTo(bean.getTo()); //文件目的地
//向剩下的在线用户发送有人离开的通知
Bean serverBean2 = new Bean();
serverBean2.setInfo(bean.getTimer() + " "
+ bean.getName() + " " + "下线了");
serverBean2.setType(0);
HashSet<String> set = new HashSet<String>();
oos.writeObject(serverBean);
oos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
onlines.remove(bean.getName());
try {
ss = new ServerSocket(8520);
onlines = new HashMap<String, ClientBean>();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
cbean.setName(bean.getName());
cbean.setSocket(client);
//添加在线用户
onlines.put(bean.getName(), cbean);
//创建服务器的bean,并发送给客户端
Bean serverBean = new Bean();
serverBean.setType(0);
private String name;
private Socket socket;
public String getName() {
return name;
}
public void setName(String namecket getSocket() {
+ "向你传送文件,是否需要接受";
serverBean.setType(2);
serverBean.setClients(bean.getClients()); //这是发送的目的地
serverBean.setFileName(bean.getFileName()); //文件名称
serverBean.setSize(bean.getSize()); //文件大小
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
Clientbean
package Server;
import .Socket;
public class ClientBean {
set.addAll(onlines.keySet());
serverBean2.setClients(set);
sendAll(serverBean2);
return;
}
case 1: { //聊天
//创建服务器的catbean,并发送给客户端
Bean serverBean = new Bean();
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
Bean serverBean = new Bean();
serverBean.setType(3);
serverBean.setClients(bean.getClients()); //文件来源
serverBean.setTo(bean.getTo()); //文件目的地
serverBean.setFileName(bean.getFileName()); //文件名称
private int type; // 1私聊0上下线更新-1下线请求2请求发送文件3.确定接收文件
private HashSet<String> clients; //存放选中的客户
private HashSet<String> to;
public HashMap<String, Bean> onlines;
return onlines;
}
public void setOnlines(HashMap<String, Bean> onlines) {
this.onlines = onlines;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
serverBean.setInfo(bean.getTimer() + " "
+ bean.getName() + "上线了");
//通知所有客户有人上线
HashSet<String> set = new HashSet<String>();
//客户昵称
set.addAll(onlines.keySet());
}
public void setName(String name) {
= name;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
this.client = client;
}
@Override
public void run() {
try {
//不停的从客户端接收信息
while (true) {
//读取从客户端接收到的bean信息
ois = new ObjectInputStream(client.getInputStream());
serverBean.setType(1);
serverBean.setClients(bean.getClients());
serverBean.setInfo(bean.getInfo());
serverBean.setName(bean.getName());//接收方的名称
serverBean.setTimer(bean.getTimer());
this.clients = clients;
}
public HashSet<String> getTo() {
return to;
}
public void setTo(HashSet<String> to) {
this.to = to;
}
public HashMap<String, Bean> getOnlines() {
serverBean.setInfo(info);
serverBean.setName(bean.getName()); //来源
serverBean.setTimer(bean.getTimer());
//向选中的客户发送数据
sendMessage(serverBean);
break;
}
case 3: { //确定接收文件
serverBean.setFileName(bean.getFileName());
return type;
}
public void setType(int type) {
this.type = type;
}
public HashSet<String> getClients() {
return clients;
}
public void setClients(HashSet<String> clients) {
import Client.Bean;
import Client.ClientBean;
public class Server {
private static ServerSocket ss;
public static HashMap<String, ClientBean> onlines;
static {
= info;
}
public String getTimer() {
return timer;
}
public void setTimer(String timer) {
this.timer = timer;
}
public String getName() {
return name;
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
}
Server
package Server;
import java.io.*;
import .*;
import java.util.*;
bean = (Bean)ois.readObject();
//分析bean中,type是那样一种类型
switch (bean.getType()) {
//上下线更新
case 0: { //上线
//记录上线客户的用户名和端口在clientbean中
ClientBean cbean = new ClientBean();
Server
Bean
package Server;
import .Socket;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
public class Bean implements Serializable{
serverBean.setClients(set);
sendAll(serverBean);
break;
}
case -1: { //下线
//创建服务器的catbean,并发送给客户端
Bean serverBean = new Bean();
serverBean.setType(-1);
try {
oos = new ObjectOutputStream(
}
}
class CatClientThread extends Thread {
private Socket client;
private Bean bean;
private ObjectInputStream ois;
private ObjectOutputStream oos;
public CatClientThread(Socket client) {
//向选中的客户发送数据
sendMessage(serverBean);
break;
}
case 2: { //请求接受文件
//创建服务器的catbean,并发送给客户端
Bean serverBean = new Bean();
String info = bean.getTimer() + " " + bean.getName()
private String info;
private String timer;
private String name;
private String fileName;
private int size;
private String ip;
private int port;
public int getType() {
serverBean.setIp(bean.getIp());
serverBean.setPort(bean.getPort());
serverBean.setName(bean.getName()); //接收的客户名称
serverBean.setTimer(bean.getTimer());
//通知文件来源的客户,对方确定接收文件
sendMessage(serverBean);
break;
}
case 4: {
Bean serverBean = new Bean();
serverBean.setType(4);
serverBean.setClients(bean.getClients()); //文件来源
serverBean.setTo(bean.getTo()); //文件目的地
//向剩下的在线用户发送有人离开的通知
Bean serverBean2 = new Bean();
serverBean2.setInfo(bean.getTimer() + " "
+ bean.getName() + " " + "下线了");
serverBean2.setType(0);
HashSet<String> set = new HashSet<String>();