繁体   English   中英

Java服务器未从客户端接收数据

[英]Java server is not recieving data from client

单击join按钮时,它应将数据发送到服务器,但不将数据发送到服务器,也不在控制台中打印消息。 为什么?

服务器

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");
        }
    }

}

服务器视图

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();
    }
}

服务器控制器

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();
    }
}

客户

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);
        }
    }

}

我注意到在ServerView的start()方法下,您永远不会这样做

 this.serverButton.setActionCommand("join");

但是您要这样做才能开始和停止。 也许与为什么连接不能正常工作有关,因为稍后在ServerController中有此操作

else if(e.getActionCommand()=="join")
    {
        this.join();
    }

您的服务器正在运行吗?

看起来serverOnline初始化为false,所以当您进入

while(serverOnline) 

它立即失败并继续,在finally块中将其设置为true。 如果此时要再次“启动”服务器,它应该开始等待连接,但是您的用户界面似乎要求您先单击“停止”,这会将serverOnline设置为false。 在Server.start()的开头添加一行,将serverOnline设置为true,它应该可以工作。

与实际运行服务器无关的两个建议:

1)在Server.close()中,您要关闭套接字。 我将其移至Server.start()中的finally块,以便您的套接字可以在强制关闭之前完成其所拥有的所有连接

2)在ServerView的构造函数中,“服务器”被拼写为“服务器”。 糟糕! :-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM