Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 构建一个简单的聊天客户端_Java_Chat_Inputstream_Outputstream - Fatal编程技术网

Java 构建一个简单的聊天客户端

Java 构建一个简单的聊天客户端,java,chat,inputstream,outputstream,Java,Chat,Inputstream,Outputstream,我正在构建一个简单的聊天客户端,它应该只能够发送和接收消息 我使用的服务器运行在我自己的计算机上,它将发送给它的任何消息发送回连接到服务器的所有用户 当我通过单击“发送按钮”向服务器发送消息时,服务器并没有按预期将消息发送回我。因此,要么我的输出流不工作,要么我的输入消息侦听器不工作,但无法找出问题所在 我可以补充一点,我没有收到任何错误消息/异常,连接到服务器正常工作 public class Chatt extends JFrame implements Runnable{ priv

我正在构建一个简单的聊天客户端,它应该只能够发送和接收消息

我使用的服务器运行在我自己的计算机上,它将发送给它的任何消息发送回连接到服务器的所有用户

当我通过单击“发送按钮”向服务器发送消息时,服务器并没有按预期将消息发送回我。因此,要么我的输出流不工作,要么我的输入消息侦听器不工作,但无法找出问题所在

我可以补充一点,我没有收到任何错误消息/异常,连接到服务器正常工作

public class Chatt extends JFrame implements Runnable{
    private JPanel topPanel = new JPanel();
    private JPanel bottomPanel = new JPanel();
    private JTextArea chattArea = new JTextArea();
    private JButton sendButton = new JButton("Skicka");
    private JLabel chattPerson = new JLabel("Du chattar med: ");
    private JTextField chattField = new JTextField(15);
    private Thread thread;
    private int port;
    private String ip;
    private DataInputStream in;
    private DataOutputStream out;
    private Socket s;


    public Chatt(String ip, int port){
        this.ip=ip;
        this.port=port;
        Konstruktor();
        }
    public Chatt(){
        ip="127.0.0.1";
        port=2000;
        Konstruktor();
        }
    public Chatt(String ip){
        this.ip=ip;
        port=2000;
        Konstruktor();
        }

    public void Konstruktor(){
        setLayout(new BorderLayout());

        chattArea.setSize(70, 50);  
        add(chattArea, BorderLayout.CENTER);

        bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
        bottomPanel.add(sendButton);
        bottomPanel.add(chattField);
        sendButton.addActionListener(new sendListener());
        add(bottomPanel, BorderLayout.SOUTH);

        topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
        topPanel.add(chattPerson);
        add(topPanel, BorderLayout.NORTH);

        try {
            //s = new Socket("atlas.dsv.su.se", 9494);
            s=new Socket(ip, port); 
            } 
        catch (UnknownHostException e) {
                System.out.println("Connection failed");
                } 
        catch (IOException e) {
                    }
        try{
        in= new DataInputStream(new BufferedInputStream(s.getInputStream()));
        out= new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
        }
        catch(UnknownHostException e){
                System.out.println("Host unknown");
            }
        catch(IOException e){

        }
        thread = new Thread(this);
        thread.start();

        setTitle("Connected to "+ip+" på port "+port);
        chattArea.setEditable(false);
        setSize(400, 500);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public void run() {

            while(true){
                System.out.println("tråden igång");
                try {
                    String temp = in.readUTF();
                    System.out.println(temp);
                    chattArea.append(temp);
                } catch (IOException e) {

                }

            }       
    }


    public class sendListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            String chattString = chattField.getText();
                try {
                    out.writeUTF(chattString);
                    out.flush();
                    } 
                catch (IOException e1) {

                    }
                chattArea.append("Du: "+chattString+"\n");
                chattField.setText("");

            }

    }



    public static void main(String[] args){
        //new Chatt("127.0.0.1", 2000);
        //new Chatt();
        new Chatt("127.0.0.1");
    }

}

我可以确认聊天服务器工作不正常。我确实构建了自己的服务器,发送/接收消息工作正常,所以代码没有问题

看起来您已经粘贴了客户端代码,您的问题似乎与服务器有关。你可能需要发布这两个。是的,这只是客户端代码,但这是一个家庭作业,我们已经从老师那里得到了服务器,所以我猜它正在工作。无法访问服务器的源代码或任何文档,但声明服务器会将您发送给它的任何消息发回。不应该调用任何特定的服务器方法您正在消化大多数异常
catch(IOException e){}
您确定没有任何异常吗?-考虑将<代码> E.PrrtStActhTrace]()/代码>加入其中。在所有的<代码> catch(IOExeExe)之后添加一个PrtTrine,所以非常肯定,我不需要查看你的代码,它应该工作。看起来服务器有问题。为您的目的创建一个测试“echo服务器”,并验证您的客户端。