JLabel的套接字服务器问题

时间:2014-07-25 19:29:46

标签: java swing sockets networking leaderboard

很长一段时间以来,我一直在努力创建一个以预期方式运行的套接字。作为一个例子,我创建了以下简单的swing程序来表示问题。

运行时,用户在文本字段中输入一个数字并按下发布按钮。然后创建套接字并将得分(输入的数字)带到服务器程序,该服务器程序执行称为排行榜的方法。根据输入的数字和输入的数量,结果将以一个名为rank的字符串的形式给出,这是我的问题开始...

无论我编写什么代码,代码的顺序是什么,我都无法让服务器返回字符串排名并将字符串排名设置为客户端程序中JLabel l2的文本。

我对socket编程很新,我相信我可能错过了一些重要的行。如果有任何人可以给出任何建议,我们将不胜感激,非常感谢。

客户代码:

package client;


public class client implements ActionListener {

public static JButton b;
public static JTextField tf;
public static JLabel l2;
String score;

public client(){
    JFrame f = new JFrame("SocketServer");

    JPanel bg = new JPanel();
    bg.setBounds(0, 0, 192, 266);
    bg.setBackground(Color.white);
    bg.setLayout(null);

    JLabel l1 = new JLabel("ENTER YOUR SCORE HERE:");
    l1.setBounds(10, 20, 172, 20);

    tf = new JTextField();
    tf.setBounds(10, 50, 172, 20);

    l2 = new JLabel();
    l2.setBounds(10, 80, 172, 20);

    b = new JButton("POST SCORE >>");
    b.setBounds(10, 120, 172, 20);
    b.addActionListener(this);

    f.setSize(200, 300);
    f.setLayout(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationRelativeTo(null);
    f.setVisible(true);

    bg.add(l1);
    bg.add(tf);
    bg.add(l2);
    bg.add(b);
    f.add(bg);

}

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

public void initClientSocket(){
    try {
        Socket socket = new Socket("localhost", 55555);

        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        out.write(score);
        out.close();            
        socket.close();


        //BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        //System.out.print("Recieved string: '\n"); ///// original and working
        //System.out.print("Recieved string: " + in.readLine() + "'\n"); ///// modified
        //while (!in.ready()) {}
        //l2.setText(in.readLine());
        //in.close();
    }
    catch(Exception e) {
        System.out.print("Whoops! It didn't work!\n");
    }
}

@Override
public void actionPerformed(ActionEvent a) {
    if (a.getSource() == b){
        //initClientSocket();
        score = tf.getText();
        tf.setText("");
        initClientSocket();

    }
}
}

服务器代码:

package server;

class server {

static long scor;
static long s1 = 0;
static long s2 = 0;
static long s3 = 0;
static long s4 = 0;
static long s5 = 0;
static String rank;

public static void main(String args[]) {
    try {
        System.out.println("Waitng for client to connect.....");
        ServerSocket server = new ServerSocket(55555);
        Socket socket = server.accept();
        System.out.print("Client has connected!\n");

        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String score = (in.readLine());
        scor = Long.parseLong(score);
        leaderboard();
        //in.close();////////////////////

        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        System.out.print("Sending rank: " + rank);
        out.print(rank);
        //out.flush();
        out.close();
        socket.close();
        server.close();
    }
    catch(Exception e) {
        System.out.print("Whoops! It didn't work!\n");
    }
}

public static void leaderboard(){
    if (scor <= s1){
        if (scor <= s2){
            if (scor <= s3){
                if (scor <= s4){
                    if (scor <= s5){
                        rank = "Too low!";
                        return;
                    }
                    if (scor > s5){
                        s5 = scor;
                        rank = "You rank 5th!";
                        return;
                    }
                }
                if (scor > s4){
                    s4 = scor;
                    rank = "You rank 4th!";
                    return;
                }
            }
            if (scor > s3){
                s3 = scor;
                rank = "You rank 3rd!";
                return;
            }
        }
        if (scor > s2){
            s2 = scor;
            rank = "You rank 2nd!";
            return;
        }
    }
    if (scor > s1){
        s1 = scor;
        rank = "You rank 1st!";
        return;
    }
}
}

0 个答案:

没有答案