[Mono-list] Socket.Select problem
Dennis Jarosch
ya_luva@uni.de
Mon, 03 May 2004 22:36:56 +0200
Hi!!
This is probably not mono-devel related but I'd really appreciate it, if
somebody could help.
I am writing a client application (w/ mono and C#) that communicates
with the server via message-strings. These strings are terminated by the
usual '\n'. The '\n' may be followed by additional data, which is why
the string has to be processed before reading the rest.
I am currently getting one character at a time, checking the
socket-state with Socket.Select. Yes, using a buffer would be nice, but
the server-protocol forces me to do it this way. ;-)
Somehow Select (and Poll, which I have also tried) does not seem to work
correctly. Sometimes I get correct results, mostly nothing is read
though. When using a buffer, everything works rather fine. When using -1
as timeout value, everything works fine but blocks after the last char.
This is my code:
++++++++++++++++++++++++ Select
ArrayList listenList = new ArrayList();
listenList.Add(socket);
int count = 0;
bool read = true;
while (read)
{
read = false;
Socket.Select(listenList, null, null, 5000);
if (listenList.Contains(socket))
{
read = true;
count = socket.Receive(buffer, 1, SocketFlags.None);
Console.WriteLine("count: " + count);
message += (char)buffer[0];
if ((char)buffer[0] == '\n')
return message;
}
}
++++++++++++++++++++++++ Poll
while (socket.Poll(-1, SelectMode.SelectRead))
{
count = socket.Receive(buffer, 1, SocketFlags.None);
message += (char)buffer[0];
if ((char)buffer[0] == '\n')
return message;
}
++++++++++++++++++++++++
Thanks for reading!
Dennis