java socket 多线程 tcp_TCP多用户多线程聊天室java实现

Client.java:

package TCP.multiThread;

import java.io.*;

import java.net.Socket;

/**

*客户端程序

*在客户端中,发送数据和接收数据应该是同步的,不应该存在先后顺序,所以要用线程来实现同步。

*/

public class Client {

public static void main(String[] args) throws IOException {

Socket client = new Socket("127.0.0.1",8888);

//输出数据

new Thread(new Send(client)).start();//一条发送线程

new Thread(new Receive(client)).start();

}

}

CloseUtil.java:

package TCP.multiThread;

import java.io.Closeable;

import java.io.IOException;

/**

* 关闭流的方法

*/

public class CloseUtil {

public static void closeAll(Closeable... io){

for(Closeable temp:io){

if(null != temp){

try {

temp.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

Receive.java:

package TCP.multiThread;

import java.io.DataInputStream;

import java.io.IOException;

import java.net.Socket;

/**

* 接收线程

*/

public class Receive implements Runnable{

private DataInputStream dis;

private boolean isRunning = true;

public Receive(){

}

public Receive(Socket client){

this();

try {

dis = new DataInputStream(client.getInputStream());

} catch (IOException e) {

e.printStackTrace();

isRunning = false;

CloseUtil.closeAll(dis);

}

}

public String receive(){

String msg = "";

try {

msg = dis.readUTF();

} catch (IOException e) {

e.printStackTrace();

isRunning = false;

CloseUtil.closeAll(dis);

}

return msg;

}

@Override

public void run() {

while (isRunning){

System.out.println(receive());

}

}

}

Send.java:

package TCP.multiThread;

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.Socket;

/**

* 发送线程

*/

public class Send implements Runnable{

private BufferedReader console;

private DataOutputStream dos;

private boolean isRunning=true;

Send(){

console = new BufferedReader(new InputStreamReader(System.in));

}

Send(Socket client){

this();

try{

dos = new DataOutputStream(client.getOutputStream());

} catch (IOException e) {

e.printStackTrace();

isRunning = false;

CloseUtil.closeAll(dos,console);

}

}

/**

* 从控制台接收数据

* 发送数据

*/

private String getMsgFromConsole(){

try {

return console.readLine();

} catch (IOException e) {

e.printStackTrace();

}

return "";

}

public void send(){

String msg = getMsgFromConsole();

if(null != msg && !msg.equals("")){

try {

dos.writeUTF(msg);

dos.flush();

} catch (IOException e) {

e.printStackTrace();

isRunning = false;

CloseUtil.closeAll(dos,console);

}

}

}

@Override

public void run() {

while (isRunning){

send();

}

}

}

Server.java:

package TCP.multiThread;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.ArrayList;

import java.util.List;

/*

*创建服务器

*/

public class Server {

List all = new ArrayList();

public static void main(String[] args) throws IOException {

new Server().start();

}

public void start() throws IOException {

ServerSocket server = new ServerSocket(8888);

Socket client;

while (true){

//一个客户端对应一个线程,同时需要把其他用户发送的信息搬运给其他的用户,所以需要维护一个线程数组,接收到一个用户发送的信息之后,把该信息发送给数组内其他用户:"other.send(msg);"

client = server.accept();

MyChannel channel = new MyChannel(client);

all.add(channel);

new Thread(channel).start();

}

}

private class MyChannel implements Runnable{

/**

* 一个客户端一条道路

*/

private DataInputStream dis;

private DataOutputStream dos;

private boolean isRunning = true;

MyChannel(Socket client){

try {

dis = new DataInputStream(client.getInputStream());

dos = new DataOutputStream(client.getOutputStream());

} catch (IOException e) {

e.printStackTrace();

isRunning = false;

CloseUtil.closeAll(dis,dos);

}

}

private String receive(){

String msg = "";

try {

msg = dis.readUTF();

} catch (IOException e) {

e.printStackTrace();

isRunning = false;

CloseUtil.closeAll(dis,dos);

all.remove(this);

}

return msg;

}

private String send(String msg){

if(null != msg && !msg.equals("")){

try {

dos.writeUTF(msg);

dos.flush();

} catch (IOException e) {

e.printStackTrace();

isRunning = false;

CloseUtil.closeAll(dos,dis);

all.remove(this);

}

}

return "";

}

private void sendOthers(){

String msg = receive();

for(MyChannel other:all){

if(other == this){

continue;

}

other.send(msg);

}

}

@Override

public void run() {

while (isRunning){

sendOthers();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值