For quite a while now I have been fighting to create a socket that behaves in the intended way. As an example I have created the following simple swing program to represent the problem.
When ran, a user enters a number into the textfield and presses the post button. A socket is then created and the score (the number entered) gets taken to the server program which performs a method called leaderboard. Depending on the number entered and how many entries are made, a result would be given in the form of a string called rank, which is where my problem begins...
No matter what code I write, what order the code is in, I just cannot get the server to return the string rank and set string rank as the text of JLabel l2 in the client program.
I am very new to socket programming, and I am sure that I have probably missed some important lines. If there is any advice that anyone could give, it would be greatly appreciated, many thank.
CLIENT CODE:
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();
}
}
}
SERVER CODE:
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;
}
}
}