写的一个JAVA程序运行不了,求教。

仙境源地 发布于 2012/04/05 14:34
阅读 2K+
收藏 1


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JTextArea;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * 服务器.java
 *
 * Created on 2012-4-5, 12:13:43
 */
/**
 *
 * @author admin
 */
public class 服务器 extends javax.swing.JFrame {

    ServerSocket ss;
    Socket socket;

    /** Creates new form 服务器 */
    public 服务器() {
        initComponents();
        jTextArea1.append("服务器:\n");
        try {
            ss = new ServerSocket(5001);        
            try {
                while (true) {
                    Socket socket = ss.accept();
                    ServerThread st = new ServerThread(socket, jTextArea1);
                    Thread th = new Thread(st);
                    th.start();
                }
            } catch (Exception exc) {
                socket.close();
            }
        } catch (Exception e) {
            try {
                ss.close();
            } catch (Exception ex) {
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextArea1.setColumns(50);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(服务器.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(服务器.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(服务器.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(服务器.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new 服务器().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration
}

class ServerThread implements Runnable {

    Socket socket;
    JTextArea jTextArea1;
    PrintWriter pw;
    BufferedReader br;

    ServerThread(Socket s, JTextArea jTextArea1) {
        this.jTextArea1 = jTextArea1;
        try {
            socket = s;
            pw = new PrintWriter(socket.getOutputStream());
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (Exception e) {
            e.toString();
        }
    }

    public void run() {
        jTextArea1.append("客户端连接来自" + socket.getInetAddress() + "\n");
        pw.println("服务器连接成功");
        pw.flush();
        while (true) {
            try {
                String s = br.readLine();
                if (s.equals(socket.getInetAddress() + "," + socket.getPort() + ":close")) {
                    break;
                }
                jTextArea1.append(s + "\n");
                pw.println("发送成功");
                pw.flush();
            } catch (Exception e) {
                e.printStackTrace();
                try{
                socket.close();
                }catch(Exception ex){
                   
                }
            }
        }
    }
}

加载中
0
牛牛牛牛
牛牛牛牛
 class 服务器  我直接震惊了
0
南瓜5793
南瓜5793
你是怎么想的,哈哈 class 服务器
0
南瓜5793
南瓜5793
都不好啊,一堆英文里一个汉词,都写成中文都好
0
狄仁傑
狄仁傑

引用来自“牛牛牛牛”的答案

 class 服务器  我直接震惊了
c#我倒是經常這樣寫。。。
0
弦歌
弦歌
报什么错嘛?也不把错误信息报上来!
0
一号男嘉宾
一号男嘉宾
我凹凸曼了。
0
南瓜5793
南瓜5793
不好意思,不淡定了,报的错应该是 unicode code 不能作为标识符,java中标识符第一个字母只能是二十四个字母+'_',后面的字母可以是字母数字+'_',不包括中文(unicode).类名是标识符一种哦
0
_blank
_blank
 服务器 - -! 
0
Monkey
Monkey
new Thread(new Runnable() {

@Override
public void run() {
try {
ss = new ServerSocket(5001);
try {
while (true) {
Socket socket = ss.accept();
ServerThread st = new ServerThread(socket, jTextArea1);
Thread th = new Thread(st);
th.start();
}
} catch (Exception exc) {
socket.close();
}
} catch (Exception e) {
try {
ss.close();
} catch (Exception ex) {
}
}
}
}).start();
坑爹了,你把事件线程给阻塞了,根本跑不动。
0
Monkey
Monkey

纯中文比qing englist强,否则class FWQ 这玩意看的你都蛋疼你都不知道这名字怎么来的。ddh(订单号)

返回顶部
顶部