zoukankan      html  css  js  c++  java
  • java多线程实现多客户端socket通信

    一、服务端

    package com.czhappy.hello.socket;
    
    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server {
        public static void main(String[] args) {
            //创建ServerSocket示例,指定端口,侦听
            try {
                ServerSocket serverSocket = new ServerSocket(8888);
                System.out.println("服务即将启动,等待客户端连接");
                //记录客户端的数量
                int count = 0;
                Socket socket = null;
                while(true){
                    //开始监听,等待客户端的连接
                    socket = serverSocket.accept();
                    ServerThread serverThread = new ServerThread(socket);
    serverThread.setPriority(4);//设置线程的优先级,范围是:[1,10],默认为5 serverThread.start(); count
    ++; System.out.println("客户端的数量="+count); InetAddress address = socket.getInetAddress(); System.out.println("当前客户端的IP地址为:"+address.getHostAddress()); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
    package com.czhappy.hello.socket;
    
    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.io.UnsupportedEncodingException;
    import java.net.Socket;
    
    /**
     * 服务器线程处理类
     * @author Administrator
     *
     */
    public class ServerThread extends Thread{
        
        Socket socket = null;
        
        public ServerThread(Socket socket){
            this.socket = socket;
        }
        
        public void run() {
            InputStream is = null;
            InputStreamReader isr = null;
            BufferedReader br = null;
            OutputStream os = null;
            PrintWriter pw = null;
            try {
                //获取字节输入流
                is = socket.getInputStream();
                //将字节输入流转化为字符输入流
                isr = new InputStreamReader(is, "utf-8");
                //为字符输入流添加缓冲
                br = new BufferedReader(isr);
                String data = br.readLine();
                while(data!=null){
                    System.out.println("我是服务器,客户端说:"+data);
                    data = br.readLine();
                }
                //关闭输入流
                socket.shutdownInput();
                //获取输出流,响应客户端请求
                os = socket.getOutputStream();
                pw = new PrintWriter(os);
                pw.write("欢迎您!");
                pw.flush();
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                try {
                    if(pw!=null){
                        pw.close();
                    }
                    if(os!=null){
                        os.close();
                    }
                    if(br!=null){
                        br.close();
                    }
                    if(isr!=null){
                        isr.close();
                    }
                    if(is!=null){
                        is.close();
                    }
                    if(socket!=null){
                        socket.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
        }
    
    }

    二、客户端

    package com.czhappy.hello.socket;
    
    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;
    
    public class Client {
        
        public static void main(String[] args) {
            try {
                //创建客户端Socket,指定服务器地址和端口
                Socket socket = new Socket("192.168.0.45", 8888);
                //获取输出流,向服务器发送消息
                OutputStream os = socket.getOutputStream();//字节输出流
                PrintWriter pw = new PrintWriter(os);//将输出流包装成打印流
                pw.write("用户名:admin;密码:123");
                pw.flush();
                socket.shutdownOutput();//关闭输出流
                //获取输入流,读取服务器端响应信息
                InputStream is = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String data = br.readLine();
                while(data!=null){
                    System.out.println("我是客户端,服务器说:"+data);
                    data = br.readLine();
                }
                
                br.close();
                is.close();
                pw.close();
                os.close();
                socket.close();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        
    
    }

    三、测试结果

    需要先启动服务端,再启动客户端

    服务端运行结果:

    客户端运行结果:

  • 相关阅读:
    黄聪:jquery mobile通过a标签页面跳转后,样式丢失、js失效的解决方法
    黄聪:jquery mobile使用form进行post提交表单没有反应,显示空白页解决方案
    黄聪:手机移动站Web响应式开发工具Viewport Resizer插件(360浏览器、谷歌Chrome浏览器兼容)
    黄聪:Discuz自制模板带jquery时与discuz本身冲突解决办法
    HTML ISO-8859-1 参考手册
    黄聪:wordpress自定义post_type,并且自定义固定链接
    黄聪:wordpress如何使用get_avatar禁止调用gravatar头像,替换为自定义头像
    Pro MapFrame
    ArcGIS Pro Layout
    pro Map
  • 原文地址:https://www.cnblogs.com/chenzheng8975/p/9579901.html
Copyright © 2011-2022 走看看