Socket 编程—多个客户端

        在Socket编程 - 单个客户端中,服务端只支持单个客户端的连接请求。 

        如果有多个客户端连接呢?需要有一个线程一直监听客户端请求,收到请求后分发给其它的线程处理。这和jetty的处理方式类似,只需要改动服务端的代码就可以了。 

服务端: 

package com.bijian.study02;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class MyMultiClientsServer {

    private static final int SERVER_PORT = 4451;
    ServerSocket server;
    Socket socket;

    public MyMultiClientsServer() {
        try {
            server = new ServerSocket(SERVER_PORT);

            while (true) {
                socket = server.accept();
                new DispatchRequestThread(socket).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // Release the resources opened
        }
    }

    class DispatchRequestThread extends Thread {

        BufferedReader in;
        PrintWriter out;
        Socket socket;

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

        @Override
        public void run() {
            try {
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                out = new PrintWriter(socket.getOutputStream(), true);

                while (true) {
                    String str = in.readLine();
                    System.out.println(socket.getPort() + ": " + str);
                    out.println("Received: " + str);

                    if (str.equals("end")) {
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // Release the resources opened
            }
        }
    }

    public static void main(String[] args) {
        new MyMultiClientsServer();
    }
}

客户端:(完全同Socket 编程—单个客户端中的客户端)

package com.bijian.study02;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class MyClient {

    Socket connection;
    BufferedReader in;
    PrintWriter out;
    BufferedReader reader;

    public MyClient() {

        try {
            // Establish a connection to the Socket server
            connection = new Socket(InetAddress.getLocalHost(), 4451);

            // Construct a reader to get the response from the server
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            // Construct a writer to send messages to the server
            out = new PrintWriter(connection.getOutputStream());

            // Construct a reader to accept the user input
            reader = new BufferedReader(new InputStreamReader(System.in));

            // Continuously send out the messages to the server and receive the response 
            while (true) {
                String str = reader.readLine();
                out.println(str);
                out.flush();
                System.out.println("sent out: " + str);

                if (str.equals("end")) {
                    break;
                }

                System.out.println(in.readLine());
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // Release the resources - omitted
        }
    }

    public static void main(String[] args) {
        new MyClient();
    }
}

        开启服务端后,开启多个客户端进行测试,结果如下: 

客户端1:

hello
sent out: hello
Received: hello
boy
sent out: boy
Received: boy
bye
sent out: bye
Received: bye
end
sent out: end

客户端2:

hello
sent out: hello
Received: hello
girl
sent out: girl
Received: girl
bye
sent out: bye
Received: bye
end
sent out: end

服务端:

51373: hello
51373: boy
51377: hello
51377: girl
51373: bye
51377: bye
51377: end
51373: end

 

文章来源:http://czj4451.iteye.com/blog/1466091

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值