当我们在 socket.accept() 方法之后创建一个新线程时发生了什么?
What is happening when we create a new Thread after the socket.accept() method?
我正在尝试编写一个多客户端套接字系统,它通过客户端发送的字符串进行通信,它将根据其内容触发一个事件。
有很多关于如何做的材料,但我无法理解其背后的逻辑。
In this example and enter link description here 在这一个中,有一段真正的代码有两个主要指令:
socket.accept();
Thread t = new Thread(runnable);
我不明白这是怎么回事:
- while(true) 连续传递那些指令,但只有在 accept() 方法单击时才创建线程?
- 新线程是否有专用端口? socket通信不是一对一吗?
- 软件如何跟踪生成的套接字线程,这真的重要吗?
- 如何发送回复给我的帖子?
也许是我缺乏 google 技能,但我找不到好的教程来做这些事情:求助?
while(true) 连续传递这些指令,但只有在 accept() 方法单击时才创建线程?
the execution stop on method accept() until someone try to connect.You can see this using the debug mode on our IDE.
新线程是否有专用端口? socket通信不是一对一吗?
No, you have many connections on the same port
软件如何跟踪生成的套接字线程,这真的重要吗?
Do bother about this for now
如何发送回复给我的帖子?
When someone try to connect you receive an object to respond this user, check the documentation
•while(true) 连续传递那些指令,但只有在accept() 方法点击时才创建线程?
创建了一个新线程来侦听通过套接字传入的数据(参见 Socket.getInputStream())
•新线程是否有专用端口? socket通信不是一对一吗?
线程没有端口。但是Socket有专门的地址可以和这个client通信
•软件如何跟踪生成的套接字线程,这真的重要吗?
这取决于软件。但是大多数时候,您会在某种 Collection
中记录已连接的套接字 - 也许是 List
,或者如果客户端正在登录,则在用户 ID 和套接字之间保持 Map
.
•我如何发送回复给我的帖子?
从简单的意义上说,就这么简单
ServerSocket ss = ...;
Socket s = ss.accept();
PrintStream ps = new PrintStream(s.getOutputStream());
ps.println("Hello World");
您需要确保您的 PrintStream 之后不会被收集垃圾,因为这将关闭 Stream / Socket
我正在尝试编写一个多客户端套接字系统,它通过客户端发送的字符串进行通信,它将根据其内容触发一个事件。
有很多关于如何做的材料,但我无法理解其背后的逻辑。
In this example and enter link description here 在这一个中,有一段真正的代码有两个主要指令:
socket.accept();
Thread t = new Thread(runnable);
我不明白这是怎么回事:
- while(true) 连续传递那些指令,但只有在 accept() 方法单击时才创建线程?
- 新线程是否有专用端口? socket通信不是一对一吗?
- 软件如何跟踪生成的套接字线程,这真的重要吗?
- 如何发送回复给我的帖子?
也许是我缺乏 google 技能,但我找不到好的教程来做这些事情:求助?
while(true) 连续传递这些指令,但只有在 accept() 方法单击时才创建线程?
the execution stop on method accept() until someone try to connect.You can see this using the debug mode on our IDE.
新线程是否有专用端口? socket通信不是一对一吗?
No, you have many connections on the same port
软件如何跟踪生成的套接字线程,这真的重要吗?
Do bother about this for now
如何发送回复给我的帖子?
When someone try to connect you receive an object to respond this user, check the documentation
•while(true) 连续传递那些指令,但只有在accept() 方法点击时才创建线程?
创建了一个新线程来侦听通过套接字传入的数据(参见 Socket.getInputStream())
•新线程是否有专用端口? socket通信不是一对一吗?
线程没有端口。但是Socket有专门的地址可以和这个client通信
•软件如何跟踪生成的套接字线程,这真的重要吗?
这取决于软件。但是大多数时候,您会在某种 Collection
中记录已连接的套接字 - 也许是 List
,或者如果客户端正在登录,则在用户 ID 和套接字之间保持 Map
.
•我如何发送回复给我的帖子?
从简单的意义上说,就这么简单
ServerSocket ss = ...;
Socket s = ss.accept();
PrintStream ps = new PrintStream(s.getOutputStream());
ps.println("Hello World");
您需要确保您的 PrintStream 之后不会被收集垃圾,因为这将关闭 Stream / Socket