首页 新闻 会员 周边 捐助

java socket服务端只能连接一个客户端

0
悬赏园豆:20 [已解决问题] 解决于 2015-03-31 17:47

java socket服务端只能连接一个客户端,怎么才能连过个客户端呢,代码如下,请指点

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

public class ServerSocket extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        new ServerSocket().createJFrame("宜诺腕表服务器程序");
        
    }

    private static JLabel jlCurrNumber;// 连接的具体数目
    private static JLabel jlSumNumber;// 总连接个数
    private static JTextArea jArea;//用于显示接收的数据
    private static int sumNumber = 0;// 连接总数
    private static int currNumber = 0;// 当前链接数

    /**
     * 创建窗体和相关控件
     * 
     * @param title
     *            窗体标题
     */
    public static void createJFrame(String title) {
        JFrame jFrame = new JFrame(title);
        Container container = jFrame.getContentPane();
        container.setLayout(null);
        // 添加监听按钮
        JButton jButton = new JButton("开始监听");
        jButton.setBounds(10, 10, 100, 30);
        jButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                jButton.setEnabled(false);
                jButton.setText("正在监听。。。");
                createThread();
            }
        });
        jButton.setFocusPainted(false); // 去掉文字周围的边框
        // 添加文本标签
        JLabel jlSumTitle = new JLabel("总连接数:");
        jlSumTitle.setBounds(10, 50, 80, 30);
        container.add(jlSumTitle);
        // 总连接个数
        jlSumNumber = new JLabel("0");
        jlSumNumber.setBounds(90, 50, 20, 30);
        container.add(jlSumNumber);
        // 当前连接数目标题
        JLabel jlCurrentTitle = new JLabel("当前连接数:");
        jlCurrentTitle.setBounds(140, 50, 80, 30);
        container.add(jlCurrentTitle);
        // 连接的具体数目
        jlCurrNumber = new JLabel("0");
        jlCurrNumber.setBounds(220, 50, 20, 30);
        container.add(jlCurrNumber);
        
        jArea = new JTextArea();
        jArea.setBounds(10, 100, 460, 250);
        jArea.setLineWrap(true);//激活自动换行功能 
        //jArea.setWrapStyleWord(true);//激活断行不断字功能 
        jArea.setBackground(Color.LIGHT_GRAY);
        container.add(jArea);

        container.add(jButton);
        container.setBackground(Color.white);
        jFrame.setBackground(Color.white);
        jFrame.setVisible(true);
        jFrame.setSize(500, 400);

        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private static java.net.ServerSocket server; // 服务端socket
    private static Socket socket;// 客户端socket
    private static BufferedReader reader;

    /**
     * 创建线程来监听客户端连接
     */
    private static void createThread() {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    server = new java.net.ServerSocket(2000);
                    System.out.println("服务器套接字已经创建成功!");
                    while (true) {
                        System.out.println("等待客户端的连接!");
                        socket = server.accept(); // 接收客户端连接
                        
                        
                        //reader = new BufferedReader(new InputStreamReader(getSocket().getInputStream()));
                        
                        
                        System.out.println("客户端:"
                                + socket.getRemoteSocketAddress());
                        sumNumber++;
                        currNumber++;
                        jlSumNumber.setText(String.valueOf(sumNumber));
                        jlCurrNumber.setText(String.valueOf(currNumber));
                        getClientMessage();
                        
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }

    /**
     * 接收客户端信息
     */
    private static void getClientMessage() {
        
        try {
            
            
            String str = null;
            System.out.println("接收客户端信息!");
            //(str = reader.readLine()) != null
            while (true) {
                InputStream in = socket.getInputStream();
                byte[] buffer = new byte[in.available()];
                if (buffer.length > 0) {
                    //System.out.println("长度=" + in.available());
                    in.read(buffer);
                    String message = new String(buffer, "UTF-8");
                    System.out.println(message);
                    jArea.append(message + "\n");
                }
                
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        try {
            if (reader != null) {
                reader.close();
            }
            if (socket != null) {
                currNumber--;
                socket.close();
            }
        } catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    
    public static JTextArea getjArea() {
        return jArea;
    }
    
    public static JLabel getjlCurrNumber() {
        return jlCurrNumber;
    }
    
    public static JLabel getJlSumNumber() {
        return jlSumNumber;
    }

    public static Socket getSocket() {
        return socket;
    }

    public static void setSocket(Socket socket) {
        ServerSocket.socket = socket;
    }
}
小小鸟努力飞翔的主页 小小鸟努力飞翔 | 初学一级 | 园豆:34
提问于:2015-01-14 15:46
< >
分享
最佳答案
0

private static Socket socket;// 客户端socket

改成

private static List<Socket> sockets;// 客户端socket

 

socket = server.accept(); // 接收客户端连接

改成:

Socket socket = server.accept(); // 接收客户端连接

sockets.Add(socket);

 

while (true) {
                InputStream in = socket.getInputStream();

改成:

while (true) {

 foreach(Socket socket in sockets)
                InputStream in = socket.getInputStream();

收获园豆:20
Launcher | 高人七级 |园豆:45050 | 2015-01-14 16:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册