简体   繁体   English

服务器套接字中的线程和处理程序

[英]Threads and Handlers in server socket

How is the new Handler(clientSocket); new Handler(clientSocket); instantiated without an object?. 没有对象实例化? Can somebody give some insight? 有人可以提供一些见解吗?

public class Server1 {

    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(15897);
            while (true) {
                Socket clientSocket = serverSocket.accept();

                new Handler(clientSocket);

            }
        } catch (IOException e)
        {
            System.out.println("problem here 1");
        }
    }
}

class Handler implements Runnable {

    int red;
    int reads, r;

    Socket clientSocket;

    Handler(Socket s)
    {
        this.clientSocket = s;

        new Thread(this).start();

    }

    public void run(){        
        BufferedReader bufferedReader;
        try {

            bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

            String inputLine = bufferedReader.readLine();

Your code is incomplete, so it's difficult to say with certainty, but observe that in the Handler's constructor a thread is created and started, which executes run() . 你的代码是不完整的,所以很难肯定地说,但是在Handler的构造函数中观察到一个线程被创建并启动,执行run()

Inside that function (and therefore in a separate thread), the input stream is read from the socket into a BufferedReader, from which the first line is obtained. 在该函数内部(因此在单独的线程中),输入流从套接字读入BufferedReader,从中获取第一行。

The thread will block until a line is received over the socket connection. 线程将阻塞,直到通过套接字连接接收到一行。

Because your code is cut off from that point, I can't say what else it does. 因为你的代码从那时起被切断了,我不能说它还有什么用。

In you code Server is made to keep listening the incoming Clients. 在您的代码中,服务器将继续监听传入的客户端。

       Socket clientSocket = serverSocket.accept();

serverSocket.accept() waits until a connection or a client is found. serverSocket.accept()等待直到找到连接或客户端。 If client is found then accept() function returns a local socket which is connected to another socket at the client which is given to clientSocket in you code. 如果找到client,则accept()函数返回一个本地套接字,该套接字连接到客户端的另一个套接字,该套接字在代码中提供给clientSocket

new Handler(clientSocket);

Now clientSocket is given to Handler class in which you are using thread for reading the data given by that clientSocket . 现在clientSocket被赋予Handler类,您在其中使用thread来读取该clientSocket给出的数据。

The purpose of thread here is to handle each incoming clientSocket seprately. 的目的thread这里是处理每个传入clientSocket seprately。

I am not sure what exactly does the code do but explaining it to you as per the concept of OOPs, it creates an instance of the Handler class everytime the while loop is executed. 我不确定代码究竟做了什么,但是根据OOP的概念向你解释它,每次执行while循环时它都会创建一个Handler类的实例。

Warning: The while(true) loop is bad code and when the project is run, it will run in an infinite loop. 警告:while(true)循环是错误代码,当项目运行时,它将以无限循环运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM