简体   繁体   English

客户端未连接到服务器

[英]Client isn't connecting to server

I decided to rewrite my client, but it's not letting me connect to my server. 我决定重写我的客户端,但这不能让我连接到服务器。 My old client still connects, so I'm not really sure what I'm doing wrong. 我的旧客户仍然可以连接,因此我不确定自己做错了什么。

My friend wrote most of the old client, I just did the socket work on it, and it worked. 我的朋友写了大多数旧客户端,我只是在上面做了套接字工作,它就起作用了。

In my Stream.java class, the System.out.println("Connecting"); 在我的Stream.java类中, System.out.println("Connecting"); shows in my connectToServer(); 显示在我的connectToServer(); method, but the System.out.println(isConnected); 方法,但System.out.println(isConnected); does not trigger. 不触发。

NOTE: I'm most likely gonna switch to CardLayout for my panels, I'm just testing different stuff. 注意:我很可能会为我的面板切换到CardLayout ,我只是在测试不同的东西。 Please only post about the issue itself. 请仅发布有关问题本身的信息。

This is what I have: 这就是我所拥有的:

Client.java 客户端.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: 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("");
            }

        }
    }
}

Stream.java: Stream.java:

package Stream;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class Stream {
    public boolean isConnected = false;

    Socket socket;

    public Stream(Socket socket) {
        this.socket = socket;
    }

    ObjectInputStream input; ObjectOutputStream output;
    Object data;


    public void connectToServer() throws IOException {
        System.out.println("Connecting..");
        socket = new Socket("thisisatestip.zapto.org", 43594);

        input = new ObjectInputStream(socket.getInputStream());
        output = new ObjectOutputStream(socket.getOutputStream());

        isConnected = true;
        System.out.println(isConnected);
    }

    public void sendData(Object data) throws IOException {
        output.writeObject(data);
        output.flush();
    }

    protected Object recieveData() throws IOException, ClassNotFoundException {
        return data = input.readObject();
    }

    public boolean exists() {
        if(socket.isClosed()) return false; else return true;
    }

}

Always create output output stream before your input stream for Object streams, otherwise you create a deadlock situation. 始终在对象流的输入流之前创建输出输出流,否则会产生死锁情况。

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

The input reads the header written by the output, which won't be there unless the output is created first. 输入读取由输出写入的标头,除非首先创建输出,否则该标头将不存在。

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

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