| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 429 人关注过本帖
标题:大家看看这个服务器程序错在哪里
只看楼主 加入收藏
灰兜兜
Rank: 2
等 级:论坛游民
帖 子:25
专家分:18
注 册:2012-4-15
结帖率:100%
收藏
 问题点数:0 回复次数:3 
大家看看这个服务器程序错在哪里
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import *;
import *;
import javax.swing.*;


public class MyServer extends JFrame implements ActionListener {

    JTextArea jta;
    JButton jb1, jb2;
    JPanel jp;
    int port = 888;
    ServerSocket ss;
    BufferedReader br;
    PrintWriter pw;

    public MyServer() {
        super("服务器");
        jta = new JTextArea();
        jb1 = new JButton("中断");
        jb2 = new JButton("启动");
        jp = new JPanel();
        jp.add(jb2);
        jp.add(jb1);
        this.add(jp, "North");
        this.add(jta);
        jb1.addActionListener(this);
        jb2.addActionListener(this);
        this.setBounds(300, 300, 250, 350);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getActionCommand() == "启动") {
            jb2.setEnabled(false);
            jb1.setEnabled(true);

            try {
                ss = new ServerSocket(port);
                jta.append("服务器启动成功" + "\n");
                //new ServerListen(ss, jta, br, pw).start();
                new Thread(new Runnable() {
                    
                    @Override
                    public void run() {
                        
                        while (true) {            
                            Socket server;
                            try {
                                server = ss.accept();
                                br = new BufferedReader(new InputStreamReader(server.getInputStream()));
                                pw=new PrintWriter(new OutputStreamWriter(server.getOutputStream()));
                                new Thread(new Runnable() {
                                    
                                    String message;
                                    @Override
                                    public void run() {
                                        try {
                                            while ((message = br.readLine()) != null) {
                                                
                                                pw.println("**"+message);   
                                                pw.flush();
                                                jta.append("客户说:"+message + "\n");   
                                                jta.append("说:"+"**"+message+"\n");
                                                            
                                            }
                                        } catch (IOException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }
                                    }
                                }).start();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }            
                        }
                        
                    }
                }).start();
                  

            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        if (e.getActionCommand() == "中断") {
            
            jb1.setEnabled(false);
            jb2.setEnabled(true);
            jta.setText("");
            pw.close();
            try {
                br.close();
            } catch (IOException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }
            try {
                ss.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }        
            
        }

    }
    public static void main(String[] args) {
        new MyServer();
    }
}
做的是一个socket服务器程序
点击中断按钮后有异常,求大神指点
搜索更多相关主题的帖子: 服务器 public import super 
2013-01-07 00:16
w527705090
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:441
专家分:1882
注 册:2011-6-28
收藏
得分:0 
哟哟。。。怎么也该来点分吧。。。

有心者,千方百计;无心者,千难万难。
2013-01-07 19:15
灰兜兜
Rank: 2
等 级:论坛游民
帖 子:25
专家分:18
注 册:2012-4-15
收藏
得分:0 
回复 2楼 w527705090
不好意思啊,现在也给不了,下次吧,能帮我改一下吗
2013-01-07 19:51
灰兜兜
Rank: 2
等 级:论坛游民
帖 子:25
专家分:18
注 册:2012-4-15
收藏
得分:0 
回复 2楼 w527705090
呵呵,现在也没办法给了,下次吧,能帮我改一下吧
2013-01-07 19:54
快速回复:大家看看这个服务器程序错在哪里
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027126 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved