繁体   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