Java sockets not sending/receiving messages

dodo

Im making a game that involves two clients, one hosts a server for the other then they can play together. In my code I have one client use a serversocket, the other a socket, and the serversocket then gets a socket by serversocket.accept(). My problem is that whenever I then use PrintWriter.println(...) the message does not get received by the other client. Im not sure whether its when i send the message or when it is received. This is the main class: (I have removed most of the GUI stuff out of the way)

public class Base extends JPanel {

PrintWriter pw;
JButton host = new JButton("Host New Game");
JButton join = new JButton("Join Existing Game");
JButton ready = new JButton("Ready");
Base b = this;
String ip;
String port;
String message;
boolean otherready;
int textip = 0;
Socket s;
int count = 0;

public Base () {

    Handler handler = new Handler();

    host.addActionListener(handler);
    join.addActionListener(handler);
    text.addActionListener(handler);

}

public void notifyOfPlayer () {

    game = true;
    setUpGame();

}

public void sendMessage (String message) {

    try {

        if (!doneonce) {

            pw = new PrintWriter(s.getOutputStream());
            doneonce = true;

        }

        pw.println(message);
        pw.flush();

    } catch (Exception e) {

        e.printStackTrace();

    }

}

public void setPlayer (Socket player) {

    s = player;

}

private class Handler implements ActionListener, MouseListener, MouseMotionListener {

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseClicked(MouseEvent e) {



    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == host) {

            remove(host);
            remove(join);
            text.setText("Server Mode, Waiting for player");
            text.setSize((int)text.getPreferredSize().getWidth(), 25);
            text.setLocation((int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2) - (text.getPreferredSize().getWidth() / 2)), 200);
            Thread t = new Thread(new ServerMode(b));
            t.start();
            repaint();

        } else if (e.getSource() == join) {

            remove(host);
            remove(join);
            text.setText("Enter IP");
            textip = 1;
            /*text.setSize((int)text.getPreferredSize().getWidth(), 25);
            text.setLocation((int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2) - (text.getPreferredSize().getWidth() / 2)), 200);
            */text.setEditable(true);
            repaint();

        } else if (e.getSource() == text) {

            if (textip == 1) {

                textip ++;
                ip = text.getText();
                text.setText("Enter Port");
                /*text.setSize((int)text.getPreferredSize().getWidth(), 25);
                text.setLocation((int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2) - (text.getPreferredSize().getWidth() / 2)), 200);
                */
            } else {

                textip = 0;
                port = text.getText();
                text.setText("Joining Game...");
                text.setSize((int)text.getPreferredSize().getWidth(), 25);
                text.setLocation((int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2) - (text.getPreferredSize().getWidth() / 2)), 200);

                try {

                    s = new Socket(ip, Integer.parseInt(port));
                    text.setText("Game Joined");
                    remove(text);
                    game = true;
                    setUpGame();
                    repaint();
                    Thread t = new Thread(new RecieveHandler(b, s));
                    t.start();

                } catch (Exception e1) {

                    text.setText("Could not connect");
                    text.setSize((int)text.getPreferredSize().getWidth(), 25);
                    text.setLocation((int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2) - (text.getPreferredSize().getWidth() / 2)), 200);
                    text.setEditable(false);
                    e1.printStackTrace();

                }

            }

        } else if (e.getSource() == ready) {

            sendMessage("ready");

        }

    }

}

public void setOtherReady () {

    otherready = true;
    JOptionPane.showMessageDialog(null, "Your opponent is ready!", "Ready!", JOptionPane.INFORMATION_MESSAGE);

}

}

Here is my RecieveHandler:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;


public class RecieveHandler implements Runnable {

Base b;
Socket s;
BufferedReader r;

public RecieveHandler(Base base, Socket socket) {

    try {

        b = base;
        s = socket;
        r = new BufferedReader(new InputStreamReader(s.getInputStream()));

    } catch (Exception e) {

        e.printStackTrace();

    }

}

@Override
public void run() {

    try {

        while (true) {

            String message = r.readLine();
            System.out.println("Recieved message: " + message);
            if (message.equals("ready")) {

                b.setOtherReady();

            }

        }

    } catch (Exception e) {

        e.printStackTrace();

    }

}

}

And here is ServerMode:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;


public class ServerMode implements Runnable {

ServerSocket ss;
Base b;
PrintWriter pw;
BufferedReader r;

public ServerMode (Base base) {

    b = base;

}

@Override
public void run() {

    try {

        ss = new ServerSocket(3602);

        while (true) {

            Socket s = ss.accept();
            pw = new PrintWriter(s.getOutputStream());
            pw.println("HI!!!");
            InputStreamReader isr = new InputStreamReader(s.getInputStream());
            r = new BufferedReader(isr);
            b.notifyOfPlayer();
            b.setPlayer(s);
            Thread t = new Thread(new RecieveHandler(b, s));
            t.start();

        }

    } catch (Exception e) {

        e.printStackTrace();

    }

}

}

This code is the same for both clients.

What have I done wrong?

dodo

I did an extreme thorough check of my code and found that the button which sends the message did not have .addActionListener(...) attached. Oops :|

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related