クライアントを書き直すことにしましたが、サーバーに接続できません。私の古いクライアントはまだ接続しているので、何が間違っているのかよくわかりません。
私の友人は古いクライアントのほとんどを書きました。私はそれでソケット作業を行いました。
私の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;
}
}