博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java socket来实现私聊和群聊-简易版
阅读量:4613 次
发布时间:2019-06-09

本文共 5192 字,大约阅读时间需要 17 分钟。

client:

import java.io.BufferedReader;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class Send implements Runnable {

private DataOutputStream dos = null;
private boolean flag = true;
private String name;
public Send(Socket client,String name) {
try {
dos = new DataOutputStream(client.getOutputStream());
this.name=name;
send(this.name);
} catch (IOException e) {
this.flag = false;
try {
dos.close();
} catch (IOException e1) {
}
}
}
public void send(String msg) {
try {
dos.writeUTF(msg);
dos.flush();
/* String readLine = br.readLine();
dos.writeUTF(readLine);*/
} catch (Exception e) {
this.flag = false;
try {
dos.close();
} catch (IOException e1) {
}
}
}
public String a() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
return br.readLine();
}
@Override
public void run() {
while (flag) {
try {
send(a());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

server:

import java.io.DataInputStream;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class MyServer {

List<MyChannel> list=new ArrayList<MyChannel>();
public static void main(String[] args) {
try {
//定义服务端
//获取一个连接
new MyServer(). start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void start() throws IOException{
ServerSocket server=new ServerSocket(8888);
while(true){
Socket accept = server.accept();
MyChannel myChannel = new MyChannel(accept) ;
list.add(myChannel);
new Thread(myChannel).start();
}
}
class MyChannel implements Runnable{
private DataInputStream dis=null;
private DataOutputStream dos=null;
private boolean flag=true;
private String name=null;
private boolean sys=true;
public MyChannel(Socket server) {
try {
dis=new DataInputStream(server.getInputStream());
dos=new DataOutputStream(server.getOutputStream());
this.name=dis.readUTF();
this.send("欢迎进入聊天室");
sendOthers(this.name+"进入了聊天室",true);
} catch (IOException e) {
e.printStackTrace();
try {
dis.close();
dos.close();
this.flag=false;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public String recive() throws IOException{
String readUTF = dis.readUTF();
return readUTF;
}
public void send(String msg) throws IOException{
dos.writeUTF(msg);
dos.flush();
}
public void sendOthers(String msg,boolean sys) throws IOException{
if(msg.startsWith("@")&&msg.contains(":")){
String name1 = msg.substring(msg.indexOf("@")+1,msg.indexOf(":"));
String content =msg.substring(msg.indexOf(":")+1);
for(MyChannel m: list){
if(m.name.equals(name1)){
m.send(this.name+"对您悄悄地说"+content);
return;
}
}
}
for(MyChannel m: list){
if(m==this){
continue;
}
if(sys){
m.send("系统消息:"+msg);
}
else{
m.send(this.name+"对所有人说:"+msg);
}
}
}
@Override
public void run() {
while(flag){
try {
sendOthers(recive(),false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
dos.close();
dis.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
}

发送消息的线程:

import java.io.BufferedReader;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class Send implements Runnable {

private DataOutputStream dos = null;
private boolean flag = true;
private String name;
public Send(Socket client,String name) {
try {
dos = new DataOutputStream(client.getOutputStream());
this.name=name;
send(this.name);
} catch (IOException e) {
this.flag = false;
try {
dos.close();
} catch (IOException e1) {
}
}
}
public void send(String msg) {
try {
dos.writeUTF(msg);
dos.flush();
/* String readLine = br.readLine();
dos.writeUTF(readLine);*/
} catch (Exception e) {
this.flag = false;
try {
dos.close();
} catch (IOException e1) {
}
}
}
public String a() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
return br.readLine();
}
@Override
public void run() {
while (flag) {
try {
send(a());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

接收消息的线程:

import java.io.DataInputStream;

import java.io.IOException;
import java.net.Socket;

public class Recive implements Runnable {

private DataInputStream dis;
private boolean flag=true;
public Recive(Socket client) {
try {
dis=new DataInputStream(client.getInputStream());
} catch (IOException e) {
try {
dis.close();
this.flag=false;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public String recive() throws IOException{
String msg = dis.readUTF();
return msg;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(flag){
try {
String recive = recive();
if(recive==null){
this.flag=false;
return;
}
System.out.println(recive);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

转载于:https://www.cnblogs.com/luoliang-gaoyu/p/8315982.html

你可能感兴趣的文章
0302 关于IT行业的就业感想
查看>>
3、流程语句相关练习
查看>>
30、git 使用
查看>>
iOS网络-02-数据解析(JSON与XML)
查看>>
python列表求和的几种等效电路
查看>>
Luogu P3393 逃离僵尸岛
查看>>
Flatten Binary Tree to Linked List
查看>>
Edit Distance
查看>>
软件工程第一次作业补充
查看>>
N76E003---输入捕获
查看>>
poj 1094 Sorting It All Out(拓扑排序)
查看>>
acdream B - 郭式树 (水题 卡cin,cout, 卡LL)
查看>>
BMP图像格式
查看>>
python的匿名函数lambda解释及用法
查看>>
c#遍历Dictionary使用KeyValuePair
查看>>
defineProperties属性的运用==数据绑定
查看>>
关于 IOS 发布的点点滴滴记录(一)
查看>>
《EMCAScript6入门》读书笔记——14.Promise对象
查看>>
CSS——水平/垂直居中
查看>>
Eclipse连接mysql数据库jdbc下载(图文)
查看>>