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<MyChannel> all = new ArrayList<MyChannel>();
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();
}
}
}
}
网友评论