By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,727 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,727 IT Pros & Developers. It's quick & easy.

Socket.Poll confusion

P: n/a
Can I use Socket.Poll for blocking socket?
It is unclear in documentation if I can use this with a synchronous socket,
can my code possibly turn a synchronous socket to asynchronous?
It seems to work but I have some problems with
outside.Receive, it gives me a SocketException "An existing connection was
forcibly closed by the remote host"
at random times, also outside.Poll will say it has data but outside.Receive
returns with nothing sometimes.
I can't use socket.available because it needs a asynchronous socket.

Socket outside = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
this is part of a threadpool Waitcallback method
----------------
while(flag)
{
try
{
if(outside.Poll(1000000,SelectMode.SelectRead)==fa lse)
{ // if socket just sits there 100000 = 1 second
flag=false;
}
else
if((bytecount=outside.Receive(recvbuffer2,0,recvbu ffer2.Length,0))==0)
{
flag=false;
}
else if(clientSocket.Poll(1000000,SelectMode.SelectWrit e))
{
clientSocket.Send(recvbuffer2,0,bytecount,0);
c=5;
lock(this)
{ // for debugging
Writetb2("ID " +rnum+ "\r\n" +
System.Text.Encoding.ASCII.GetString(recvbuffer2,0 ,bytecount)+"\r\n"+
"\r\n");
}
bytecount=0;
}
}
catch(Exception e)
{
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
lock(this)
{
Form1.tcount-=1;
Writetb1("Thread count",Form1.tcount); // to delegate Invoked
Writetb2(e.ToString()+"\r\n");
}
return;
}
}

thanks Mike
Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.