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.connected property, it always shows "True". what could be happened? Thanks
15 3326
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
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.
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
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.Client 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.SelectRead) 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 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
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.
What I want to detect is whenever the remote device (server) is power off, then the software should be able to know the connection is lost or the socket is disconnected or it is closed and so on. Then the client will try to reconnect it back to the server. but it will fail to do so until the server is powered on again.
That function I gave you will only tell you if the connected socket you had was closed. It detects physical disconnections (unplugged, power off, etc) as well as software disconnections (server software is closed, server closed connection)
If you are trying to find out if the server is running before connecting to it, that is completely different. And all you would do is turn down the connectiontimeout values so that it will return very quickly if no connection can be made.
Thanks Plater,
Where is to set connectiontimeout value? is it one of property of socket?
What I want is that the connection is established initially, if the server power off, my software should be able to detect this state change, then close the socket and wait for re-connection when the server power on. Thanks
Is there any way to ping the remote server with the socket in my application? by checking the response, I may detect whether the remote server is power down.
Yes, using "My.Computer.Network.Ping" function is able to detect the remote server power down, it will return false if ping fail.
I have another question regarding the event of data received via network. Since TCPClient does not implement data_received event like Winsock, how can I program an event routine to handle this event? Thanks.
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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...
|
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,...
| |