1

Clientクラスからわかるように、whileが中断すると、(Client)システムがメッセージを出力しますが、このメッセージは出力されません。

サーバーが出力するのは、「fromClient = CLIENT:あなたから次のように言われたばかりです...:サーバー:接続されています。IDは1です」だけです。

(「ThreadWorker」のtryブロックを参照してください)

ただし、サーバーは「理解された」メッセージ(「101」がクライアントに送信され、クライアントが「理解された」で応答したことに対する応答として送信される必要があります)を表示せず、繰り返し書き込まれたクライアントの応答も表示しません。これには、ループに増分された「num」値が含まれている必要があります。

これは、スレッドまたはソケットのいずれかで何かが失敗していることを示していますが、私はそれをatmで学習しています。それが私が見逃している明らかな何かであることを本当に願っていますが、私は約2日間、さまざまなことを試しました。悪い。

大量のコードを投稿するのは時々それほど良くないことはわかっていますが、ここで行う必要があると思います。そうしないとすみません。

アイデア?ありがとう :)

(ところで、これをすでに読んでいる場合は申し訳ありませんが、これは投稿のちょっとした難破です。うまくいけば、以前の問題を分類しました)

クライアント

import java.io.*;
import java.net.*;

public class Client 
{
    public static void main(String[] args) throws IOException 
    {
        Socket clientSocket = null;
        DataOutputStream os = null;    
        BufferedReader is = null;
        String fromServer = null;

        try 
        {
            clientSocket = new Socket("localhost", 4444);
            os = new DataOutputStream(clientSocket.getOutputStream());
            is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        } 
        catch (UnknownHostException e) 
        {
            System.err.println("Don't know about host: localhost.");
            System.exit(1);
        } 
        catch (IOException e) 
        {
            System.err.println("Couldn't get I/O for the connection to: localhost.");
            System.exit(1);
        }    

        while(clientSocket != null && is != null && os != null)
        {
            fromServer = is.readLine();
                if(fromServer.equals("101"))
                {
                    os.writeBytes("Understood" + "\n");
                }
                else
                {
                    os.writeBytes("CLIENT: I was just told the following by you...: " + fromServer + "\n");
                }
        }
        System.out.println("If you're reading this then the while loop (line 30 - 48) has broken");
        os.close();
        is.close();
        clientSocket.close();
    }
}

サーバ

import java.io.*;
import java.net.*;

public class Server
{
    ServerSocket server = null; 
    Socket service = null;

    public Server()
    {       
        try
        {
            server = new ServerSocket(4444);
        }
        catch(IOException e)
        {
            System.err.println(e + "Could not listen on port 4444");
            System.exit(1);
        }

        System.out.println("Listening for clients on 4444");

        int id = 1;
        boolean b = true;
        while(b = true)
        {
            try
            {
                service = server.accept();

                ThreadWorker tW = new ThreadWorker(service, id);
                new Thread(tW).start();
            }
            catch(IOException e)
            {
                System.err.println("Exception encountered on accept. Ignoring. Stack Trace: ");

                e.printStackTrace();
                System.exit(1);
            }
            id++;
        }
    }

    public static void main(String args[]) throws IOException 
    {
        Server s = new Server();
    }
    }

THREADWORKER

import java.io.*;
import java.net.*;
public class ThreadWorker implements Runnable
{
    protected Socket clientSocket = null;
    protected int id;
    boolean running = true;
    DataOutputStream os = null;
    BufferedReader is = null;
    String fromClient;

    public ThreadWorker(Socket service, int i)
    {
        clientSocket = service;
        id = i;
    }

    public void run()
    {
        try
        {
            os = new DataOutputStream(clientSocket.getOutputStream());
            is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            os.writeBytes("Server: you are connected, your ID is " + id + "\n");
            fromClient = is.readLine();
            System.out.println("fromClient = " + fromClient);
            os.writeBytes("101");
            fromClient = is.readLine();
            System.out.println("fromClient = " + fromClient);
            if(fromClient.equals("Understood"))
            {
                System.out.println("please work(line 35 ThreadWorker)");
                while(is != null && os != null && clientSocket != null)//running)
                {
                    int num = 1;
                    os.writeBytes("Hello client, here is a number:" + num + " from thread: " + "\n");
                    fromClient = is.readLine();
                    System.out.println("'Hello client, here is a number: " + num + "' written to connected client with id of " + id + " (line 36 ThreadWorker)");
                    System.out.println(fromClient);
                    num++;
                }
            }
        }
        catch(IOException e)
        {
            System.err.println("Error line 38: " + e);
        }
    }
}
4

1 に答える 1

3

これをデバッグすると、問題のあるコードはのように見えますos.writeBytes("101");。返送する必要がありますos.writeBytes("101\n");。そうしないと、反対側のreadLine()は続行されません。

于 2012-04-07T09:46:59.410 に答える