gpt4 book ai didi

java - 客户端未连接到服务器

转载 作者:行者123 更新时间:2023-12-01 04:23:51 24 4
gpt4 key购买 nike

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

我的 friend 写了大部分旧客户端,我只是对其进行了套接字工作,并且它起作用了。

在我的 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;
}

}

最佳答案

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

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

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

关于java - 客户端未连接到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18681251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com