簡體   English   中英

while 循環不適用於套接字/多線程(java)(僅一輪)

[英]while loop not working (only once round) with sockets/ multi-threaded (java)

正如您從客戶端 class 中看到的那樣,如果 while 中斷,則(客戶端)系統會打印一條消息,此消息永遠不會打印。

服務器打印的唯一內容是“fromClient = CLIENT:你剛剛告訴我以下內容......:服務器:你已連接,你的 ID 是 1”

(參見“ThreadWorker”中的 try 塊)

然而,服務器不顯示“已理解”消息(應作為對發送給客戶端的“101”的響應發送,並且客戶端以“已理解”響應)並且它也不會顯示重復寫入的客戶端響應這應該在循環中包含一個遞增的“num”值。

這告訴我線程或 sockets 中出現了故障,但我正在學習它,我真的希望它只是我遺漏的明顯東西,但我花了大約兩天時間嘗試不同的東西,一些幫助,一些成功更差。

現在我知道發布大量代碼有時並不是那么好,但我認為我需要在這里。 對不起,如果我不這樣做。

想法? 謝謝:)

(順便說一句,對不起,如果你已經讀過這篇文章,這篇文章有點亂七八糟,希望我已經解決了之前的問題)

客戶

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();
    }
    }

線工

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);
        }
    }
}

調試這個,看起來有問題的代碼是os.writeBytes("101"); . 您需要發回os.writeBytes("101\n"); 或者另一邊的 readLine() 不會繼續。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM