I was able to open a connection using TCPClient, it is able to read and write data or string, however when I turn off the remote device or unplug the network cable, then check the client.connecte d property, it always shows "True". what could be happened? Thanks
15 3360 Plater 7,872
Recognized Expert Expert
The connection property is only reflective of the "last read or write operation", which I presonally feel makes it rather useless. You have to try to read/write on a closed socket before it the property changes.
Fortunatly I wrote the following function that I think works rather well. -
//note: this only works if there is no data waiting to be read
-
//and in the case of a listener socket; no pending connections
-
public static bool isSocketConnected(Socket s)
-
{
-
bool retval=true;
-
bool part1 = s.Poll(1000, SelectMode.SelectRead);
-
bool part2 = (s.Available == 0);
-
if (part1 & part2)
-
{//connection is closed
-
retval=false;
-
}
-
return retval;
-
}
-
Thank you,
But I'm using VB2005, is there a similar function as s.poll( ) in VB? by the way what is the poll function doing?
Thanks
Plater 7,872
Recognized Expert Expert
All of that is available in .NET. So whether its VB.NET or C#, you can still use the calls. You can check the Poll() function on msdn
(Just need to change to VB.NET syntax)
EDIT: Quick online convert produced this, should be pretty close. -
'note: this only works if there is no data waiting to be read
-
'and in the case of a listener socket; no pending connections
-
Public Shared Function isSocketConnected(ByVal s As Socket) As Boolean
-
Dim retval As Boolean = True
-
Dim part1 As Boolean = s.Poll(1000, SelectMode.SelectRead)
-
Dim part2 As Boolean = (s.Available = 0)
-
If part1 And part2 Then
-
'connection is closed
-
retval = False
-
End If
-
Return retval
-
End Function
-
Thank you very much.
There are so many sokets from .net. Like Winsock, Sockets, TCPCLient. All of them can do the data transmission. only sockets has Poll( ) function. But I really don't know what are the difference between them? and how to choose one of them for my application? Just try one by one. Any comment?
Thanks.
Plater 7,872
Recognized Expert Expert
Well quick overview
WinSock is COM implementation of sockets
Socket is a .NET impementation of sockets
TcpClient(and TcpListener and UDPClient as well) are wrapper classes around the .net Socket. They all contain properties for accessing the underlying sockeet directly.
So, How can I Poll using Winsock and TCPClient, the Poll function is not implemented for this two objects. Is there any other way to do?
very appreciate for your help
Plater 7,872
Recognized Expert Expert
Well winsock is a COM object, you shouldn't be using that anyway.
TcpClient, as I already said, is just a "wrapper" type class of the socket and has a property that is the instance of the socket.
myTcpClient.Cli ent is a Socket, so you can just pass that into the function.
Thanks Plater,
However, after try the following code, the return value of s.Poll(1000, SelectMode.Sele ctRead) always 'false', and s.Available always '0'. no matter the remote device is powered on or off. Any other way to test the remote device is connected? or power off?. For your info, the device and the PC are connected via an ethernet switch. Thanks.
'note: this only works if there is no data waiting to be read
'and in the case of a listener socket; no pending connections
Public Shared Function isSocketConnect ed(ByVal s As Socket) As Boolean
Dim retval As Boolean = True
Dim part1 As Boolean = s.Poll(1000, SelectMode.Sele ctRead)
Dim part2 As Boolean = (s.Available = 0)
If part1 And part2 Then
'connection is closed
retval = False
End If
Return retval
End Function
Plater 7,872
Recognized Expert Expert
I've never once had any trouble with it. It's always been dead on, not even any delay.
You are using a TcpClient so I'm not sure what your issue is.
Unless it has something to do with the linger state?
SelectRead
true if Listen has been called and a connection is pending;
-or-
true if data is available for reading;
-or-
true if the connection has been closed, reset, or terminated;
otherwise, returns false.
That would lead me to believe that your socket is still connected.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Patrick |
last post by:
Hi...
I've written a small client, that uses the TCPClient for connecting to a
server and receives data from the server. When the connection is idle, after
5 minutes I get disconnected from the server. When this happens, I want to
reconnect to the server, but my problem is, I don't know how to find out if
I'm still connected or not.
Any ideas how to get that status?
|
by: user |
last post by:
Hello
How can i read IP of incoming connection when i have TcpClient for that
connection ?
Thanx
|
by: David Dvali |
last post by:
How can I check if TcpClient is connected (or not connected)?
|
by: hamil |
last post by:
I am having trouble using the TcpListener and TcpClient classes.
At the end of this post is server code that runs, and a class whose purpose
is described below.
I need to know when the client closes the connection so I can exit my
listener code. Here is what I have tried.
First I asked MSDN and they said..
|
by: Holger Steinestel |
last post by:
Hello,
I have a problem with the TCPClient class.
After closing the connection with the Close() method, the connection on the
Linux server remains open with the CLOSE_WAIT status.
I found the Knowledge Base article #821625 , which recommends to close the
NetworkStream before the TCPClient is closed. However, this does not solve
the problem.
| |
by: pickedaname |
last post by:
I work for a telecom company.
We need to get data from our 5ESS switches to be consumed by a .NET app.
The data I am getting back is incomplete.
For example, the beginning of the first line recvd from the device should
read:
SWT5 5e16(2) 02.00
Instead I get
a bunch of gibberish then SWT5
|
by: Prasanta |
last post by:
Hello,
Please cnay one can tell me how to read mail as formatted.... i have made
some code using that able to read but not able to serialize..... so am i need
to parse the HTML, or is there any class using that i can directly get the
full info ablt the mail, like TO,From,Subject,Body,Attachments,
Below is the code which i develop,
using System;
|
by: =?Utf-8?B?R2Vvcmdl?= |
last post by:
Hello everyone,
I am wondering if I am using TCPClient class in C#, how to setup timeout
value? Timeout I mean, when connects to server for the 1st time, and during
<timeoutinterval, if no server response is received, the GetStream method
will return will return other than wait forever.
thanks in advance,
|
by: Bjoern Schliessmann |
last post by:
Hello,
I'm currently trying to implement a simulation program with Kamaelia
and need a reliable TCP connection to a data server.
From Twisted, I know that a method is called if the connection fails
by whatever reason. I tried to get the same results with Kamaelia's
TCPClient component. If I start up the component and try to connect
to a closed TCP port it fails and sends a message out of the signal
box, that's okay.
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |