简体   繁体   English

Java中套接字和stdin之间的轮询

[英]polling between sockets and stdin in java

I have to write a p2p program that has to act as both the client and the server. 我必须编写一个既要充当客户端又要充当服务器的p2p程序。 The program has to listen to both incoming connections(acts as server) and wait for the user input(acts as client). 该程序必须侦听两个传入的连接(充当服务器)并等待用户输入(充当客户端)。 I am not sure what approach to take to do this. 我不确定采取哪种方法。

I tried using NIO to list all the selectablechannels. 我尝试使用NIO列出所有可选通道。 I still am not able to figure out how to make the stdin as non-blocking!! 我仍然不知道如何使标准输入成为非阻塞!

My program operates in console. 我的程序在控制台中运行。 So, no AWT allowed. 因此,不允许AWT。

Please help!! 请帮忙!!

您至少需要两个线程:一个线程用于处理stdin,一个或多个用于处理套接字,具体取决于您使用的是非阻塞NIO还是阻塞I / O。

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

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