A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

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();
                                }
                        }
                       
                }
        }

}

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

public class Client {

        public static void main(String[] args) throws UnknownHostException, IOException  {
                //1.建立客户端套接字
                Socket client = new Socket("127.0.0.1",8888);       
                new Thread(new Send(client)).start();
                new Thread(new Receive(client)).start();
               
        }

}

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

public class Receive implements Runnable {
        private DataInputStream dis = null;
        private boolean isRunning = true;
        public Receive(){
               
        }
         public Receive(Socket client) {
                 try {
                        dis = new DataInputStream(client.getInputStream());
                } catch (IOException e) {
                        isRunning = false;
                        CloseUtil.closeAll(dis);
                }
               
        }
         public String receive(){
                 String msg ="";
                 try {
                                 msg = dis.readUTF();
                        } catch (IOException e) {
                                isRunning = false;
                                CloseUtil.closeAll(dis);
                        }
                 return msg;
                 
         }

        @Override
        public void run() {
                //线程体
                while(isRunning){
                        receive();               
                }
                       
        }

}

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 br = null;//控制台输入流
        private DataOutputStream dos =null;//管道输出流
        private boolean isRunning = true;//线程标示
       
        public Send(){
                System.out.println("请输入:");
                br =new BufferedReader(new InputStreamReader(System.in));       
        }
        public Send(Socket client){
                this();
                try {
                        dos = new DataOutputStream(client.getOutputStream());
                } catch (IOException e) {
//                        e.printStackTrace();
                        isRunning = false;
                        CloseUtil.closeAll(dos);
                }
        }
        /*
         * 1、从控制台接受数据
         */
        private String getMessageFromConsole(){
                try {
                        return br.readLine();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return null;
        }
       
        public  void send(){
                String msg = getMessageFromConsole();
                if(null!=msg&&!msg.equals("")){
                        try {
                                dos.writeUTF(msg);
                                dos.flush();
                        } catch (IOException e) {
//                                e.printStackTrace();
                                isRunning = false;
                                CloseUtil.closeAll(dos);
                               
                        }
                }
        }
        @Override
        public void run() {
                //线程体
                while(isRunning){
                        send();
                        System.out.println("发送出去了!");
                }
               
        }
       

}

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 {
        private ServerSocket server;
        private List<MyClient> clients = new ArrayList<MyClient>();
        private boolean isRunning = true;
       
        public static void main(String[] args) throws IOException {
                new Server().start();
        }
        public void start(){
                //1.创建ServerSocket
                try {
                        server = new ServerSocket(8888);
                } catch (IOException e1) {
                        e1.printStackTrace();
                }
                while(isRunning){
                        try {
                                //2.接受客户端
                                Socket socket = server.accept();//这是一个阻塞式,会一直等待有个客户端连接
                                MyClient client = new MyClient(socket);
                                clients.add(client );//加入到容器中,统一管理
                                System.out.println("一个客户端连接了。。");
                                new Thread(client).start();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }       
                       
                }
               
        }
       
        //内部类:便于访问外部类的私有信息
        //一个客户端,一条道路
        private class MyClient implements Runnable{
                private Socket socket;
                private DataOutputStream dos = null;
                private DataInputStream dis = null;
               
                //构造函数、初始化
                public MyClient(Socket socket){
                        this.socket = socket;
                        try {
                                dos = new DataOutputStream(socket.getOutputStream());
                                dis = new DataInputStream(socket.getInputStream());
                        } catch (IOException e) {
//                                e.printStackTrace();
                                isRunning = false;
                                CloseUtil.closeAll(dis,dos);
                        }
                        }               
               
                public String Receive(){
                        String msg = "";
                        try {
                                msg = dis.readUTF();       
                        } catch (IOException e) {
//                                e.printStackTrace();
                                isRunning = false;
                                CloseUtil.closeAll(dis,dos);
                                clients.remove(this);
                        }
                        return msg;
                }
               
                public void send(String msg){
                        if(msg==null&msg.equals("")){
                                return ;                       
                        }
                        try {
                               
                                dos.writeUTF(msg);//把接收到的数据写出去
                                dos.flush();
                                System.out.println("要发送的数据:");
                        } catch (IOException e) {
//                                e.printStackTrace();
                                isRunning = false;
                                CloseUtil.closeAll(dis,dos);
                                clients.remove(this);
                        }
               
                }
                /*//发送给其他客户端
                private String  sendOther(){
            String         msg = this.Receive();
                        //遍历容器
                        for(Client other:clients){
                                if(other ==this){
                                        continue;
                                }else{
                                        other.send(msg);
                                }
                        }
                        return msg;
                }*/
                @Override
                public void run() {
                        if(isRunning){
                                send(Receive());
//                                System.out.println("发给其他人:"+sendOther());
                        }               
                       
                }
               
        }
}
您需要登录后才可以回帖 登录 | 加入黑马