473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

TCPClient status no change

91
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
May 28 '08 #1
15 3326
Plater
7,872 Expert 4TB
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.
Expand|Select|Wrap|Line Numbers
  1. //note: this only works if there is no data waiting to be read
  2. //and in the case of a listener socket; no pending connections
  3. public static bool isSocketConnected(Socket s)
  4. {
  5.    bool retval=true;
  6.    bool part1 = s.Poll(1000, SelectMode.SelectRead);
  7.    bool part2 = (s.Available == 0);
  8.    if (part1 & part2)
  9.    {//connection is closed
  10.       retval=false;
  11.    }
  12.    return retval;
  13. }
  14.  
May 28 '08 #2
qfchen
91
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
May 28 '08 #3
Plater
7,872 Expert 4TB
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.
Expand|Select|Wrap|Line Numbers
  1. 'note: this only works if there is no data waiting to be read 
  2. 'and in the case of a listener socket; no pending connections 
  3. Public Shared Function isSocketConnected(ByVal s As Socket) As Boolean 
  4.     Dim retval As Boolean = True 
  5.     Dim part1 As Boolean = s.Poll(1000, SelectMode.SelectRead) 
  6.     Dim part2 As Boolean = (s.Available = 0) 
  7.     If part1 And part2 Then 
  8.         'connection is closed 
  9.         retval = False 
  10.     End If 
  11.     Return retval 
  12. End Function 
  13.  
May 28 '08 #4
qfchen
91
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.
May 29 '08 #5
Plater
7,872 Expert 4TB
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.
May 29 '08 #6
qfchen
91
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
May 29 '08 #7
Plater
7,872 Expert 4TB
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.
May 29 '08 #8
qfchen
91
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
Jun 9 '08 #9
Plater
7,872 Expert 4TB
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.
Jun 9 '08 #10
qfchen
91
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.
Jun 9 '08 #11
Plater
7,872 Expert 4TB
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.
Jun 9 '08 #12
qfchen
91
Thanks Plater,

Where is to set connectiontimeout value? is it one of property of socket?
Jun 9 '08 #13
qfchen
91
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
Jun 9 '08 #14
qfchen
91
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.
Jun 10 '08 #15
qfchen
91
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.
Jun 10 '08 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
7
by: user | last post by:
Hello How can i read IP of incoming connection when i have TcpClient for that connection ? Thanx
7
by: David Dvali | last post by:
How can I check if TcpClient is connected (or not connected)?
1
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...
0
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...
8
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...
1
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...
4
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...
6
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.