473,785 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Socket.Poll confusion

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.availabl e because it needs a asynchronous socket.

Socket outside = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
this is part of a threadpool Waitcallback method
----------------
while(flag)
{
try
{
if(outside.Poll (1000000,Select Mode.SelectRead )==false)
{ // if socket just sits there 100000 = 1 second
flag=false;
}
else
if((bytecount=o utside.Receive( recvbuffer2,0,r ecvbuffer2.Leng th,0))==0)
{
flag=false;
}
else if(clientSocket .Poll(1000000,S electMode.Selec tWrite))
{
clientSocket.Se nd(recvbuffer2, 0,bytecount,0);
c=5;
lock(this)
{ // for debugging
Writetb2("ID " +rnum+ "\r\n" +
System.Text.Enc oding.ASCII.Get String(recvbuff er2,0,bytecount )+"\r\n"+
"\r\n");
}
bytecount=0;
}
}
catch(Exception e)
{
clientSocket.Sh utdown(SocketSh utdown.Both);
clientSocket.Cl ose();
lock(this)
{
Form1.tcount-=1;
Writetb1("Threa d count",Form1.tc ount); // to delegate Invoked
Writetb2(e.ToSt ring()+"\r\n");
}
return;
}
}

thanks Mike
Nov 16 '05 #1
0 4522

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3846
by: Sven Groot | last post by:
I have a Windows Service application that acts as if it's an SMTP server. Outlook connects to this service, which is always running on the localhost. This works fine most of the time. However, sometimes it will no longer receive anything from Outlook. Socket.Receive would block indefinitely (actually, Socket.Poll times out, because that's what I'm using). Outlook sends it, because when I'm stepping through the code with the debugger,...
4
16830
by: Joe Kinsella | last post by:
The following code behaves differently from what I would expect: socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); socket.Blocking = false; socket.Connect(ipe); isConnected = socket.Poll(30*1000000, SelectMode.SelectWrite); I would have expected that once I set the socket to non-blocking, the connect would return immediately and the poll would wait up to 30 seconds
0
1425
by: Tony Caduto | last post by:
Hi, I am developing a socket server in C# and I want it to be blocking. Anyway I have it working with threads, as each client connects the socket spawned from the accept method is sent to a client thread. In each thread I have a loop like below: the data_available method is a wrapper around Socket.Poll() so I can use it in miliseconds instead of micro. Basicly I read a command via a streamreader and that all works, then depending on...
0
2072
by: Valerie Hough | last post by:
I have a process which accepts multiple asynchronous TCP socket connections. Periodically I want this task to check for abnormal disconnection by each client. The code that detects this correctly is: if (( socket.Poll( 1, SelectMode.Read )) && ( socket.Available == 0 )) { // abnormal disconnect }
2
702
by: Droopy | last post by:
Hi, I try to implement a reusable socket class to send and receive data. It seems to work but I have 2 problems : 1) I rely on Socket.Available to detect that the connection is closed (no more data to expect). Sometimes, Socket.Available returns 0 but the other end of the connection did not close it ! 2) This class will be used by many other classes so I have to use the
0
2044
by: jinfeng_Wang | last post by:
I have writen the following souce code : m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_Socket.Blocking = false; try { m_Socket.Connect(ipEndPoint); }
11
8619
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling mechanisms. I use a non-blocking approach for this, using the call to 'poll' to support the async mechanism - rather than the 'begin' and 'end' functions. I already found that connecting doesn't set the "isconnected" variable correctly...
3
3107
by: JDavis | last post by:
I have created a static class-level socket for use by the class member functions: static Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); The Connect and Disconnect methods work fine, as does the synchronous Send method. However, the Poll method returns true for SelectMode.SelectWrite and false for SelectMode.SelectRead. Therefore, synchronous Read won't return anything.
21
5671
by: puzzlecracker | last post by:
Problem: I send a lot of requests to the application (running on a different box, of course), and I receive back responses from the app . Below: socket corresponds to Socket socket=new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.