1

過去 5 日間、プログラムが機能しない理由を突き止めようとしましたが、ここで無数の質問をして良い回答を得ましたが、問題が見つかったようです。

背景ストーリー:

クライアントとサーバーの 2 つの個別のアプリケーションに分割されたチャット プログラムがあります。アイデアは、チャット プログラムを作成して、クライアントが画像といくつかのラベルといくつかのテキスト領域を含む GUI を持つようにすることです。

クライアントがGuiショーを起動し、ユーザーが接続ボタンを押してサーバーに接続できるようになったときはいつでも、これは私のプログラムで直接起こることであり、プログラムのこの部分は正常に動作します:

** 接続ボタンが押されています** クライアントがサーバーに送信: 1 サーバーがクライアントに送信: 1 サーバーがクライアントに送信: 1 クライアントがサーバーに送信: Marc (チャット担当者のユーザー名)

クライアントがサーバーに送信: 5 サーバーがクライアントに送信: 1 サーバーがクライアントに送信: 1 サーバーがクライアントに送信: Marc

クライアントがサーバーに送信: 8 サーバーがクライアントに送信: 8

そして今、私のGuiのスレッドが開始されるため、すべての地獄が解き放たれます。そしてここから、プログラムは明らかにサーバーからのメッセージを受信できなくなります。サーバーがメッセージを受信して​​も。どうすればいいの?

大量のコードを投稿しないようにするために、さらにコードが必要な場合は、プログラムの重要な部分を投稿します。投稿を更新して喜んでいます

スレッド (simpleController (GUI を制御するもの) 内)

public void run(){ 
    System.out.println("Thread started");
    System.out.println(client.getSocket().isConnected());
    ClientListner cl = new ClientListner(client);
    while (client.getSocket().isConnected()) {
        int key = 10;

            if (client.getInput().hasNext()) {
                txt_ChatPerson2.setText(client.reciveString());
                txt_ChatPerson2.setVisible(true);
            }
            try {
                key = client.reciveCommando();
                System.out.println("jeg er her");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



        System.out.println("Key "+key);
        switch (key) {
        // case 2 er recive chat:
        case 2:
            // først find ud af hvilket ID der har sendt chatten:
            int y = 0;
            try {
                y = client.reciveCommando();
                System.out.println("y" + y); 
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // derefter få beskeden og send den så ud til resten.
            String says = client.reciveChat().toString();
            System.out.println("Says :"+says);
            if (y == 1) {
                txt_ChatPerson1.setText(says);
                txt_ChatPerson1.setVisible(true);
            }else if (y == 2) {
                txt_ChatPerson2.setText(says);
                txt_ChatPerson2.setVisible(true);
            }else {
                chatPerson3.setVisible(true);
                txt_ChatPerson3.setVisible(true);
                txt_ChatPerson3.setText(says);
            }

            break;

        default:
            break;
        }
    }
}

クライアントクラス

 import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;


public class Client {
// disse var static
    public final static int portNumber = 6040;
    public Socket socket;
    private PrintWriter pw;
    private Scanner input;
    private int clientId;
    /**
     * @param args
     * @throws IOException 
     */

    public Client(Socket socket, PrintWriter pw, Scanner input, int clientId){
        this.socket = socket;
        this.pw = pw;
        this.input = input;
        this.clientId = clientId;
    }
    public void connect() throws IOException{
        // du kan vælge at bruge inetadressen til at connecte i socketet.
        InetAddress adr = InetAddress.getByName("localhost");
        socket = new Socket("localhost", portNumber);
        input=new Scanner(socket.getInputStream());
        pw = new PrintWriter(socket.getOutputStream());

    }
    /**
     * This method sends the message (that the client(chat person) writes to the user)
     * @param x
     * @throws NullPointerException
     * @throws IOException 
     */
    public void SendChat(String x) throws NullPointerException{
            pw.print("CHAT:"+x);
            pw.flush();
        /*  pw.println(2);
            pw.flush();
            pw.println(SimpleController.currentClientId);
            pw.flush();
            pw.println(x);
            pw.flush(); 
        */
    }
    public int sendCommando(int id) throws IOException{
        System.out.println("Jeg sender"+ id);
        pw.println(id);
        pw.flush();
        /*
         * this part of the program sends a command to the server if the command is 1 then 1 is = Connect.
         * the program then ask the server is the server is full or is it ok to connect? 
         * if the response is not 10 then the program will allow a connection to happen the return type will be the Id of which 
         * the chat person becomes!
         */
        // should the method return 0 the Application will do NOTHING!
        switch (id) {
        case 1:
    int k = reciveCommando();
            if (k== 10) {
                return 10;
            }else if (k < 3) {
                System.out.println("returned k" + k);
                return k;
            }else {

            return 10;
            }
            /*
             * Closes the connection with the server!
             */
        case 3:

            socket.close();
            return 0;

        case 5:
            int y  = reciveCommando();
            return y;

        case 8:
            return 8;
        default:
            return 0;
        }

    }
    /*
     * this method recives a command from the server! the comands can be found in the ChatCommands.txt
     * returns the command as an integer!
     */
    public int reciveCommando() throws IOException{
        Integer i = input.nextInt();
        return i;
    }
    /**
     * Gets a String response from the server. This method i used to create other users and give them the correct username.
     * 
     * @param i
     * @return
     * @throws IOException
     */
    public String getStringResponse(int i) throws IOException {
        pw.print(i);
        pw.flush();
        String x = input.nextLine();
        return x;

    }
/*
 * Work in progress - client getter og setter methoder!
 */

public Socket getSocket(){
    return socket;
}
public Scanner getInput(){
    return input;
}
public PrintWriter getPw(){
    return pw;
}
public int getClientId(){
    return clientId;
}

public void setClientId(int i ){
    clientId = i;
}
public String reciveChat(){
        return getInput().nextLine();

}
public String reciveString(){
    String x =input.next();
    return x;
}
public void sendString(String x){
    pw.println(x);
    pw.flush();
}



}

サーバーコード

    import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


public class Server extends Thread {

    private Socket connection;
    private PrintWriter pw;
    private Scanner input;
    private ServerInformation info = new ServerInformation();
    private ChatPerson p;

    public Server(Socket connection, PrintWriter pw, Scanner input){
        this.connection = connection;
        this.pw = pw;
        this.input = input;
    }

    @Override
    public void run() {
        while (connection.isConnected()) {
            if (input.hasNextInt()) {
                System.out.println("Det var sgu da en int");
                int i = getInput().nextInt();
                System.out.println(i);
                checkCommand(i);    
            }else if (input.hasNext()) {
                System.out.println("det var ikke en int");
                String inString = input.nextLine();
                System.out.println(inString);
                chatCase(inString);
            } 
        }


    }
    private void chatCase(String x) {
        String k =x.substring(6 , x.length());
        System.out.println("k "+k);
        pw.print(x);

        pw.flush();
    }

    private void checkCommand(int i) {
        System.out.println("i "+i);
        switch (i) {
        // this case accepts the connection and creates a new user;
        case 1:


int id = info.getNextID();
        pw.println(1);
        pw.flush();
        pw.println(id);
        pw.flush();
        p = new ChatPerson(id, input.next());
        info.addToList(p);
        break;

        // this is the chat case virker ikke endnu
    case 2:
        int clientID = input.nextInt();
        String x = reciveString();
        System.out.println(x);
        pw.println(clientID);
        pw.flush();
        pw.print(x);
        pw.flush();
        break;
    // this case sends information about other chat users to the client
    case 5:
        pw.println(5);
        pw.flush();
        pw.println(info.getList().size());
        pw.flush();
        for (int j = 0; j < info.getList().size(); j++) {
            pw.println(info.getList().get(j).getId());
            System.out.println("info.get "+info.getList().get(j).getId());
            pw.flush();
            pw.println(info.getList().get(j).getName());
            pw.flush();
        }
        break;

    case 8:
        pw.println(8);
        pw.flush();
        break;
    default:
        break;
    }

}

private String reciveString() {
    if (input.next().contains(" ")) {
        String x = input.nextLine();
        return x;
    }
    return input.next();
}

public Socket getConnection(){
    return connection;
}
public PrintWriter getPw(){
    return pw;
}
public Scanner getInput(){
    return input;
}




}

これは私にとって大きな問題なので、誰かが私を助けてくれることを願っています。さらに情報が必要な場合は、コメントしてできるだけ多く提供してください。

4

1 に答える 1

3

プログラムを作り直す必要があります。

  1. GUI 要素は、スタンドアロン スレッドからではなく、GUI スレッド (EDT) 内からのみ更新する必要があります。

  2. クライアント接続は、メッセージの送信と受信という 2 つの弱く接続された部分に分割する必要があります。受信は別のスレッドにする必要があり、送信はスレッドまたはクラスにすることができます。

  3. 受信スレッドがメッセージを読み取ると、 を使用して EDT に送信しますSwingUtilities.invokeLater(Runnable)。例は他の場所で見つけることができます。

  4. ユーザーがテキストを入力すると、テキストはソケットに直接送信されるか、キューに入れられ、後で送信スレッド (存在する場合) によって処理されます。

于 2012-10-13T14:10:20.473 に答える