0

クライアント ソケットとサーバー ソケットを使用する Messenger プログラムを作成していますが、DataInputStream.readUTF() メソッドの実行時に問題が発生し続けます。java.io.EOFException が発生します。私の貧弱なプログラミングとひどい名前の変数について申し訳ありません。私のクライアントのコードはほとんどここにあります。

import java.awt.CardLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.ScrollPane;
import java.awt.Scrollbar;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

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

public class GUI extends JFrame implements ActionListener{
private InetAddress inet;
private Socket s = new Socket();enter code here
private DataOutputStream output;
private DataInputStream input;
private JButton send;
private JTextArea tarea;
private BufferedReader br;
private JTextField tf;
private JTextField ip;
private JTextField port;
private JButton connect;
private JPanel one;
private JPanel two;
private CardLayout cardlayout;
private JPanel cards;
private JTextField uname;
private JScrollPane scroll;
private Thread listen;
String a;
                                            //contructor
    public GUI() {
    super("Messenger v2.0");
    cards = new JPanel(new CardLayout());
    tarea = new JTextArea(10,30);
    scroll = new JScrollPane(tarea);
    tf = new JTextField("type your message here", 20);
    send = new JButton("Send");
    ip = new JTextField("ip", 5);
    port = new JTextField("port", 5);
    connect = new JButton("Connect");
    uname = new JTextField("Type your username", 13);

    one = new JPanel();
    one();
    two = new JPanel();
    two();
    cards.add(one, "1");
    cards.add(two, "2");
    first();
}//end constructor



                                            //one method
public void one(){
    one.add(ip);
    one.add(port);
    one.add(uname);
    one.add(connect);

}//end one method
                                        //two method



public void two(){
    two.add(scroll);
    two.add(tf);
    two.add(send);

}//end two method
                                        //first method



public void first(){
    add(cards);
    cardlayout = (CardLayout) cards.getLayout();
    cardlayout.show(cards, "1");

    connect.addActionListener(this);

}//end first method
                                        //second method



public void second(){
    cardlayout.show(cards, "2");
    setSize(400, 300);
}//end second method
                                        //action performed



@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource() == connect){
        second();
        String ip = this.ip.getText();
        String port = this.port.getText();
        try {
            connect();
        } catch (NumberFormatException | IOException e1) {
            e1.printStackTrace();
        }
        second();
    }
    else if(e.getSource() == send){
        try {
            send(tf.getText());
        } catch (Exception e1) {

        }
    }

}//end actionperformed
                                        //connect method





public void connect() throws NumberFormatException, IOException{
    String z = ip.getText();
    inet = InetAddress.getByName(z);;
    s = new Socket(inet, Integer.parseInt(port.getText()));
    input = new DataInputStream(s.getInputStream());
    this.output = new DataOutputStream(this.s.getOutputStream());
    String g;
    g = uname.getText() + " has joined the chat";
    send(g);
                                        //thread to listen


listen = new Thread(){

    public void run(){
            while(true){
            if(s.isInputShutdown() == false){
        try {
            a = input.readUTF();
            if(a.equals("DISSCONNECT_SESSION") || a.equals("-1") || a.equals(-1) ||       a.equals(null)){

                input.close();
                break;
            }
            write(a);
            a = null;
            }
         catch (IOException e) {

            e.printStackTrace();
        }
            }else{
                break;
            }
            }
    }
};

listen.start();



}//end connect() method
                                        //send(String) method



private void send(String text) throws IOException {
    output.writeUTF(text);
    write(text);
}//end send(String) method
                                            //write method



public void write(String message){

    tarea.setText(tarea.getText() + "\n" + message );
}//end write(String message) method
}//end class

そして、私のサーバーコードのほとんどは.

import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class The_Server {
DataInputStream input;
DataOutputStream output;
Seconday_Thread st;
String help2;
The_Main tm;
private ServerSocket ss;
private Socket s;
private ArrayList<Socket> cs = new ArrayList<Socket>();
private ArrayList<DataOutputStream> outputs = new ArrayList<DataOutputStream>();
String name;
int counter;
public void createServer(String a) throws IOException{
    int b = Integer.parseInt(a);
    ss = new ServerSocket(b);
         counter = 0;
            while (true){
                s = ss.accept();
            cs.add(counter, s);
            output = new DataOutputStream(cs.get(counter).getOutputStream());
            outputs.add(counter, output);
            input = new DataInputStream(cs.get(counter).getInputStream());
            name = input.readUTF();

            send("has joined", name);
            counter++;
            st = new Seconday_Thread(input, name); // this thread is     DataInputStream.readUTF() in a while loop
            st.start();

        }
}
public void send(String text, String name) {
    int counter_num_2 = 0;
    while(true){
                if(outputs.get(counter_num_2).equals(null)){
                    break;
                }
                else{
            if(counter_num_2 == 40){
                break;
            }

            else if(cs.get(counter_num_2).isClosed()){

            }
            else if(cs.get(counter_num_2).isClosed() && counter_num_2 == 39){
                counter = 0;
                break;
            }
            else{
        try {
             help2 = name + " " + text;
             tm = new The_Main();
             tm.sendToServer(name, text);
            outputs.get(counter_num_2).writeUTF(help2);
            outputs.get(counter_num_2).flush();
        } catch (Exception e) {
            try {
                cs.get(counter_num_2).close();
            } catch (IOException e1) {

            }
        }
        counter_num_2++;
            }
    }
    }
}
}

誰かがコードを見てエラーを見つけてください。私は多くの時間を費やしましたが、それを理解できませんでした。

4

1 に答える 1