1

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

我的朋友写了大部分旧客户端,我只是在它上面做了套接字工作,它工作了。

在我的Stream.java课堂上,System.out.println("Connecting");显示在我的connectToServer();方法中,但System.out.println(isConnected);没有触发。

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

这就是我所拥有的:

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

        }
    }
}

流.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;
    }

}
4

1 回答 1

0

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

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

输入读取输出写入的标头,除非首先创建输出,否则该标头将不存在。

于 2013-09-08T06:57:56.293 回答