0

It should send data to server when join button clicks but it does not send the data to server and does not print the message in console. why?

Server

package clientServer;
import java.net.*;
import java.io.*;

public class Server {
    private ServerView view;



    private boolean serverOnline=false;
    private ServerSocket server;
    private InputStream serverInStream;

    public Server(ServerView view)
    {
        this.view=view;
    }


    public void start()
    {
        //Manipulate model
        System.out.println("Server is started");
        //Optionally update view


        Socket listenPort;
        try
        {
            this.server=new ServerSocket(13131);

            while(this.serverOnline)
            {
                listenPort=this.server.accept();

                this.serverInStream=listenPort.getInputStream();

                BufferedReader bfw=new BufferedReader(new InputStreamReader(this.serverInStream));
                System.out.println(bfw.readLine());

        this.serverInStream.close();

            }
        }
        catch(IOException e)
        {
            System.err.println(e);
        }
        finally
        {
            this.serverOnline=true;
        }
    }

    public void stop()
    {
        try
        {
        this.serverOnline=false;
        this.server.close();
        }
        catch(IOException e)
        {
            System.err.println("Problem in stopping server" + e);
        }
        finally
        {
            System.out.println("Server has been stopped");
        }
    }

}

ServerView

package clientServer;

import javax.swing.*;
import java.awt.*;

public class ServerView {

    private JFrame window;
    private Container holder;
    private JButton serverButton;
    private JLabel label;
    private JPanel panel;
    private JButton serverJoinButton;
    private ServerController controller;

    public ServerView(ServerController controller) {
        this.controller = controller;
        this.window = new JFrame("Twenty nine");

        this.panel = new JPanel();
        this.holder = this.window.getContentPane();

        this.serverButton = new JButton("start");
        this.serverButton.setActionCommand("start");
        this.serverButton.addActionListener(this.controller);

        this.label = new JLabel("Serever is offline");

        this.holder.add(this.panel);

        this.panel.add(this.label);
        this.panel.add(this.serverButton);

        this.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.window.setSize(800, 900);
        // this.window.setLayout(new BorderLayout());
        this.window.setVisible(true);

    }

    public void start() {
        this.label.setText("Server is online");
        this.serverButton.setActionCommand("stop");
        this.serverButton.setText("stop");

        //Adds join buttton
        this.serverJoinButton = new JButton("Join");
        this.serverJoinButton.setText("join");
        this.serverJoinButton.addActionListener(this.controller);

        this.panel.add(this.serverJoinButton);
        //this.panel.repaint();
        this.panel.revalidate();
    }

    public void stop()
    { 
        this.label.setText("Server is offline");
        this.serverButton.setActionCommand("start");
        this.serverButton.setText("start");

        this.panel.remove(this.serverJoinButton);

        this.panel.repaint(); //Adding works properly removing dont
        this.panel.revalidate();
    }
}

ServerController

package clientServer;

import java.awt.event.*;

public class ServerController implements ActionListener {

    private Server model;
    private ServerView view;

    public void setModel(Server server) {
        this.model = server;

    }

    public void setView(ServerView view) {
        this.view = view;

    }

    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand()=="start")
        {
            this.start();
        } 
        else if(e.getActionCommand()=="stop")
        {
            this.stop();
        }
        else if(e.getActionCommand()=="join")
        {
            this.join();
        }

    }

    public void start() {
        //Reponse to event immidiately
        this.view.start();
        //Response and manipulate model
        //Should start a new thread instead of using swing eventDispatch thread
        this.model.start();
    }
    public void stop() {
        //Reponse to event immidiately
        this.view.stop();
        //Response and manipulate model
        this.model.stop();
    }
    public void join()
    {
        System.out.println("Client tries to connect");
        Client cl=new Client();
        cl.join();
    }
}

Client

package clientServer;

import java.net.*;
import java.io.*;

public class Client {

    private Socket socket;

    public Client()
    {
        try
        {
        this.socket=new Socket("127.0.0.1",13131);
        }
        catch(UnknownHostException e)
        {
            System.err.println(e);
        }
        catch(IOException e)
        {
            System.err.println(e);
        }
    }

    public void join()
    {
        System.out.println("Client join called");
        System.out.println("Client socket is connected:" + this.socket.isConnected());
        try
        {
        OutputStream op=this.socket.getOutputStream();

        BufferedWriter bfw=new BufferedWriter(new OutputStreamWriter(op));
        bfw.write("Client is connected \n");

        bfw.close();
        }
        catch(IOException e)
        {
            System.err.println(e);
        }
    }

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
varuog
  • 3,031
  • 5
  • 28
  • 56
  • 1
    because you have got an issue with Event Dispatch Thread described in Oracle tutorial Concurency in Swing, Swing GUI doesn't care, isn't notified somehow that background process to change something out of EDT – mKorbel Jul 10 '13 at 13:49
  • @mkorbel how to resolve this? – varuog Jul 10 '13 at 13:55
  • @mKorbel is correct; there's a complete example [here](http://stackoverflow.com/a/3245805/230513). – trashgod Jul 10 '13 at 18:28
  • @trashgod I am studying your example, but it would be better if you can post and explain as an answer why my sample program is not working. – varuog Jul 10 '13 at 20:44

2 Answers2

0

I noticed under ServerView under the start() method you never do this

 this.serverButton.setActionCommand("join");

but you do do this for start and stop. Maybe that has a bit to do with why the join isn't working properly since you have this later in ServerController

else if(e.getActionCommand()=="join")
    {
        this.join();
    }
dghalbr
  • 109
  • 1
  • 12
  • oh and I meant to ask...Does everything else work but the join button? Your question makes it look like that's the only thing that doesn't work. – dghalbr Jul 10 '13 at 15:05
0

Is your server running?

It looks like serverOnline initializes to false, so when you get to

while(serverOnline) 

It immediately fails and continues on, where it is set to true in the finally block. If you were to "start" the server again at this point it should begin waiting for connections, but it looks like your UI will require you to hit "stop" first, which will set serverOnline to false. Add a line to the beginning of Server.start() that sets serverOnline to true and it should work.

Two suggestions not related to actually getting your server to run:

1) in Server.close() you are closing your socket. I would move that to the finally block in Server.start() so that your socket can finish up any connections it has before being forced to close

2) There's a point in ServerView's constructor where "server" is spelled "serever". Oops! :-)

John M
  • 310
  • 4
  • 15