当前位置: 代码迷 >> Java相关 >> “客户机”问题
  详细解决方案

“客户机”问题

热度:233   发布时间:2008-11-27 19:32:43.0
“客户机”问题
??????? 点击启动按钮,不知为啥,资源耗尽
           望诸位大虾就原思路改进, 在下感激不尽   !!!!!!!!! (只实现部分功能)


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;

public class MyServer extends JFrame implements ActionListener
{
    int port = 8888;
    ServerSocket serverSocket;
    
    JToolBar toolBar;
    JButton startButton, stopButton, exitButton;
    JTextArea textArea;
    JPanel downPanel;
    JLabel sendToLabel;
    JComboBox comboBox;
    JTextField textField;
    JButton sendButton;
    
    public MyServer()
    {
        toolBar = new JToolBar();
        startButton = new JButton("启动服务");
        startButton.setEnabled(true);
        startButton.addActionListener(this);
        stopButton = new JButton("停止服务");
        stopButton.setEnabled(false);
        stopButton.addActionListener(this);
        exitButton = new JButton("退出");
        exitButton.addActionListener(this);
        toolBar.add(startButton);
        toolBar.add(stopButton);
        toolBar.addSeparator();
        toolBar.add(exitButton);
        
        textArea = new JTextArea();
        textArea.setEditable(false);
        
        downPanel = new JPanel();
        sendToLabel = new JLabel("发送至:");
        comboBox = new JComboBox();
        comboBox.insertItemAt("所有人",0);
        comboBox.setSelectedIndex(0);
        textField = new JTextField(13);
        textField.setEditable(false);
        sendButton = new JButton("发送");
        sendButton.addActionListener(this);
        downPanel.add(sendToLabel);
        downPanel.add(comboBox);
        downPanel.add(textField);
        downPanel.add(sendButton);
        
        this.add(toolBar,"North");
        this.add(textArea,"Center");
        this.add(downPanel,"South");
        this.setSize(360,500);
        this.setResizable(false);
        this.setLocation(500,200);
        this.setTitle("服务器");
        this.show();
    }
    
    
    public void actionPerformed(ActionEvent e)
    {
        Object obj = e.getSource();
        
        if(obj==startButton)
        {
            //textArea.setText("服务器已开启");
            startService();
        }
        
        else if(obj==stopButton)
        {
            stopService();    
        }
        
        else if(obj==exitButton)
        {
            System.exit(0);
        }
    }
    
    
    public void startService()
    {
        textArea.append("服务器已开启");
        startButton.setEnabled(false);
        stopButton.setEnabled(true);
        sendButton.setEnabled(true);
        try{
            serverSocket = new ServerSocket(port);
            
            while(true)
            {
                Socket socket = serverSocket.accept();
                new MyThread(socket).start();
            }
        }catch(Exception e){
            e.printStackTrace();
        }    
    }
    
    
    public void stopService()
    {
        textArea.setText("服务器已经关闭");
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
    }
    
    
    public static void main(String[] args)
    {
        new MyServer();
    }
}




class MyThread extends Thread
{
    Socket s;
    ObjectOutput output;
    ObjectInput input;
    
    public MyThread(Socket s)
    {
        this.s = s;
    }
    
    public void run()
    {
        try{
            output = new ObjectOutputStream(s.getOutputStream());
            input  = new ObjectInputStream(s.getInputStream());
        }catch(Exception e){
            e.printStackTrace();
        }
        
    }
}


[[it] 本帖最后由 yinmingzheng13 于 2008-11-27 19:40 编辑 [/it]]
搜索更多相关的解决方案: 客户机  

----------------解决方案--------------------------------------------------------
你有一个while(true),随便一个这个东西都会占用很多资源
----------------解决方案--------------------------------------------------------
回复 第2楼 freish 的帖子
那有点    但注释了while  照样没有反应
  只是想在“文本域”内显示信息
----------------解决方案--------------------------------------------------------
  相关解决方案