仲翎逸仙

放下傲慢,放下贪嗔痴,专注于此时此刻,日日如此,必有所成。

Socket 服务端使用多线程实现多客户端通讯实例

socket通信原理

  

  Java多线程实现Socket通讯

  1、服务端

复制代码
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * <p>
 * 基于socket通讯-服务端
 * <p>
 * 
 * @author <a href="mailto:yangkj@corp.21cn.com">yangkj</a>
 * @version
 * @since 2017年1月12日
 */
public class Server {

    private static ServerSocket SERVER_SOCKET =null;;
    
    static{
        try {
            SERVER_SOCKET = new ServerSocket(9090);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            System.out.println("******服务器已启动,等待客户端连接*****");
            Socket socket = null;
            while(true){
                //循环监听客户端的连接
                socket = SERVER_SOCKET.accept();
                //新建一个线程ServerSocket,并开启
                new ServerSocketThread(socket).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
复制代码

  2、服务端线程处理类

复制代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
/**
 * <p>
 * Socket线程服务类
 * <p>
 * 
 * @author <a href="mailto:yangkj@corp.21cn.com">yangkj</a>
 * @version
 * @since 2017年1月13日
 */
public class ServerSocketThread extends Thread {

    private Socket socket;

    public ServerSocketThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        OutputStream os = null;
        PrintWriter pw = null;
        try {
            // socket获取字节输入流
            is = socket.getInputStream();
            // 将字节输入流转字符输入流
            isr = new InputStreamReader(is);
            // 将字符输入流转行输入流
            br = new BufferedReader(isr); //
            String message = null;
            while ((message = br.readLine()) != null) {
                System.out.println("客户端发来消息:" + message);
            }
            // 必须先关闭输入流才能获取下面的输出流
            socket.shutdownInput();
            // 获取输出流
            os = socket.getOutputStream();
            pw = new PrintWriter(os);
            pw.write("欢饮您进入socket");
            pw.flush();
            // 关闭输入流
            socket.shutdownOutput();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (pw != null) {
                pw.close();
            }
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (isr != null) {
                    isr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (isr != null) {
                    isr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
复制代码

  3、客户端

复制代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * <p>
 * 基于socket通讯-客户端
 * <p>
 * 
 * @author <a href="mailto:yangkj@corp.21cn.com">yangkj</a>
 * @version
 * @since 2017年1月12日
 */
public class Client {

    public static void main(String[] args) {

        Socket socket = null;
        OutputStream os = null;
        PrintWriter pw = null;
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            socket = new Socket("127.0.0.1", 9090);
            // 获取输出流向服务端写入数据
            os = socket.getOutputStream();
            pw = new PrintWriter(os);
            pw.write("用户名:admin 密码:123");
            pw.flush();
            socket.shutdownOutput();
            // 获取输入流接受服务端返回的信息
            is = socket.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            String message = null;
            while ((message = br.readLine()) != null) {
                System.out.println("服务器说:" + message);
            }
            socket.shutdownInput();

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (isr != null) {
                    isr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (pw != null) {
                pw.close();
            }
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
阅读更多
个人分类: java多线程
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Socket 服务端使用多线程实现多客户端通讯实例

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭