I am trying to manage a client side in a chat program. So the client should get the input from the keyboard and send it to the server. In the meantime, it should be able to recieve masseges from the server.
In C
, we have the select()
function, and in linux
stdin has a file descriptor, so I could use select() to get indicated when stdin or the socket is ready for read.
My question:
- How can I do it in
C#
andwindows
?Socket.Select()
accepts only Sockets, but the StandardInput is a stream. How could I use it in Select()? - I know I can use another thread to monitor the stdin, but I'm looking for a sulution that handle it in one thread.
that is what I'm trying to do, in general:
public void Run()
{
clientSide.Connect(ipEndPoint);
//-> make a Socket for stdin:
//-> Socket stdinSocket = new Socket(????);
List<Socket> checkReadList = new List<Socket>();
checkReadList.Add(clientSide);
checkReadList.Add(stdinSocket);
while (CanRun())
{
Socket.Select(checkReadList, null, null, -1);
foreach(Socket current in checkReadList)
{
if(stdinSocket == current)
{
Console.ReadLine(.....);
....
}
if (clientSide == current)
{
SendMessageToServer(...);
}
}
}
Thanks!