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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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 客户机isn';无法连接到服务器_Java_Sockets_Data Structures_Connection_Client - Fatal编程技术网

Java 客户机isn';无法连接到服务器

Java 客户机isn';无法连接到服务器,java,sockets,data-structures,connection,client,Java,Sockets,Data Structures,Connection,Client,我决定重写我的客户端,但它不允许我连接到服务器。我的老客户仍然有联系,所以我不确定我做错了什么 我的朋友写了大部分旧客户机,我只是对它做了套接字工作,它成功了 在我的Stream.java类中,System.out.println(“连接”)显示在myconnectToServer()中方法,但系统.out.println(未连接)不会触发 注意:对于我的面板,我很可能会切换到CardLayout,我只是在测试不同的东西。请只发布有关问题本身的信息 这就是我所拥有的: Client.java p

我决定重写我的客户端,但它不允许我连接到服务器。我的老客户仍然有联系,所以我不确定我做错了什么

我的朋友写了大部分旧客户机,我只是对它做了套接字工作,它成功了

在我的
Stream.java
类中,
System.out.println(“连接”)显示在my
connectToServer()中方法,但
系统.out.println(未连接)不会触发

注意:对于我的面板,我很可能会切换到
CardLayout
,我只是在测试不同的东西。请只发布有关问题本身的信息

这就是我所拥有的:

Client.java

package Main;

import java.net.Socket;

import Frame.ClientFrame;
import Stream.FilteredStream;
import Stream.Stream;

public class Client implements Runnable {
    public static boolean loggedIn = false;
    public static Stream stream;

    Socket clientSocket;

    ClientFrame frame;
    Thread clientThread;

    public synchronized void start() {
        clientThread = new Thread(this);
        clientThread.start();
    }
    public synchronized void stop() {
        clientThread.interrupt();
    }
    public void run() {
        stream = new Stream(clientSocket);
        frame = new ClientFrame("Login");
        frame.displayFrame();

        while(loggedIn){

        }

    }

    public static void main(String[] args) {
        new Client().start();
    }
}
ClientFrame.java:

package Frame;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import Main.Client;

public class ClientFrame extends JFrame implements ActionListener {
    String name;

    public ClientFrame(String name) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.name = name;
    }

    private JButton loginButton;
    private JButton newAccButton;
    private JTextField usertextfield;
    private JTextField passtextfield;
    private JLabel label;


    public JPanel mainpanel;
    JPanel infopanel;

    public void disposePanel(String name) {
        switch(name) {
        case "mainpanel":
            mainpanel.removeAll();
        }
    }


    public void displayFrame() {
        switch(name) {
        case "Login": 
            int panelStage = 0;
            setSize(300, 150);

            mainpanel = new JPanel();
            mainpanel.setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();

            JLabel connect = new JLabel("Connecting to Server...");
            connect.setFont(new Font("Felix Titling", Font.BOLD, 24));
            mainpanel.add(connect, c);

            while(!Client.stream.isConnected) {
                if(panelStage < 1) {
                    add(mainpanel);
                    setVisible(true);
                    panelStage++;
                }
                try{
                    Client.stream.connectToServer();
                }catch(IOException e) { e.printStackTrace(); }
            }
            disposePanel("mainpanel");

            mainpanel.setLayout(new BorderLayout());

            infopanel = new JPanel();
                label = new JLabel("Username: ");
                infopanel.add(label, BorderLayout.WEST);

                usertextfield = new JTextField("Username", 10);
                usertextfield.addActionListener(this);
                usertextfield.setText("");
                infopanel.add(usertextfield, BorderLayout.EAST);
            mainpanel.add(infopanel, BorderLayout.NORTH);

            infopanel = new JPanel();
                label = new JLabel("Password: ");
                infopanel.add(label, BorderLayout.WEST);

                passtextfield = new JTextField("Password", 10);
                passtextfield.addActionListener(this);
                passtextfield.setText("");
                infopanel.add(passtextfield, BorderLayout.EAST);
            mainpanel.add(infopanel, BorderLayout.SOUTH);

            infopanel = new JPanel();
                loginButton = new JButton("Login");
                loginButton.addActionListener(this);
                infopanel.add(loginButton, BorderLayout.WEST);

                newAccButton = new JButton("New Acc");
                infopanel.add(newAccButton, BorderLayout.EAST);

            mainpanel.add(infopanel, BorderLayout.SOUTH);

            add(mainpanel);
            setResizable(false);
            setVisible(true);
            break;
        case "Chat":
            break;
        }
    }

    String loginUsername;
    String loginPassword;

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == loginButton){
            if(usertextfield.getText().length() > 0) {
                if(passtextfield.getText().length() > 0) {
                    loginUsername = usertextfield.getText();
                    loginPassword = passtextfield.getText();
                }
            }
            try {
                Client.stream.sendData("LOGININFO"+" "+loginUsername+" "+loginPassword);
            }catch(IOException exception) { exception.printStackTrace(); }
        }
        if(e.getSource() == userText) {
            if(userText.getText().length() > 0) {
                //sendMessageToServer(userText.getText());
                userText.setText("");
            }

        }
    }
}

始终在对象流的输入流之前创建输出流,否则会造成死锁情况

    output = new ObjectOutputStream(socket.getOutputStream()); 
    // output.flush(); if the underlying stream is buffered
    input = new ObjectInputStream(socket.getInputStream());

输入读取由输出写入的标题,除非先创建输出,否则标题不会出现。

好的。为什么在创建输出流时必须刷新?实际上不需要这样做。多年来我一直在说要这么做,但这会让它自己变红。整个建议的理由是将对象流头写在线路上,以便对等方的
ObjectInputStream
构造函数可以读取它。如果你先构造输入,双方都在死锁地等待读取流标题。@VinceEmigh Paranoia,对我来说。在这种情况下,您不需要这样做,因为基础流没有被缓冲,但是如果您使用缓冲,则需要这样做。@EJP它本身并不刷新,它只写入未缓冲的头,在这种情况下这很好,但是如果基础流需要刷新(),则必须添加此项。
    output = new ObjectOutputStream(socket.getOutputStream()); 
    // output.flush(); if the underlying stream is buffered
    input = new ObjectInputStream(socket.getInputStream());