java多线程实现多客户端socket通信

Posted chenzheng8975

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
        }
    }
    
    

}

三、测试结果

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

服务端运行结果:

客户端运行结果:

以上是关于java多线程实现多客户端socket通信的主要内容,如果未能解决你的问题,请参考以下文章

Java 多线程 用socket通信

java socket一对多通信如何实现?

Java Socket 通信之多线程

delphi中如何实现多线程socket通信时

Java千百问_02基本使用(012)_如何编写多线程Socket程序

c++ 求助socket多线程网络通信怎么实现并发