chipKIT® Development Platform

Inspired by Arduino™

tcpclient's isConnected() strange behaviours

Created Wed, 31 Jul 2013 15:58:59 +0000 by thepascalix


thepascalix

Wed, 31 Jul 2013 15:58:59 +0000

Hi at all, i'm working with ethernet shield and digilent's DNET library. I setup a sketch in with i have a TCP server on my max32 boards that attend a connection from a client installed on my Pc, and after that, if a connection is established, it read from the client some information. It work very well (also beceause i've structured my application on the provided digilent's example). I check if the connection between tcp server on max32 and tcp client on pc is up by the method isConnected(). If don't arrive anythings, i don't close the connection (with tcpclient.close()) and attend.

At this point i've a problem. If i close a tcp connection from my program in my Pc, the sketch work perfect and recognize the loss of connetion immediatly, but if i interrupt abruptly the ethernet connection of my pc (if remove the ethernet cable from the router or disable ethernet card from my OS), the sketch recognize the loss of connection after few minutes.

Anything have this problem? Do you know any solution for this problem?

Thanks a lot and excuse for my bad english


thepascalix

Fri, 30 Aug 2013 12:43:09 +0000

has anyone an ideo of this mistake?


majenko

Fri, 30 Aug 2013 12:55:55 +0000

That's normal ethernet (TCP/IP) behaviour.

When you close a connection properly a special packet is sent by the client to say "I have closed the connection" and the server acts on it accordingly. If you just unplug the connection that packet never arrives, so the server doesn't know that the client is disconnected, so all it can do is wait for some timeout period while it retries sending packets. Once that timeout period is past it assumes the connection is gone and closes the socket.


thepascalix

Fri, 30 Aug 2013 13:21:00 +0000

thanks a lot majenko for your explanation. So, at this point, do you have a possible troubleshooting or can you suggest me a resolution?


majenko

Fri, 30 Aug 2013 13:30:51 +0000

"Troubleshooting", or "resolution" implies it's a problem. It isn't - it is intended operation.

Unplug the cable and plug it back in again before the timeout - your connection should survive fine. It's how it works. It's what it's meant to do.

isConnected() operates on layer 5 of the OSI model. The cable is layer 1. You would need some mechanism for monitoring layer 1 or layer 2. The chip would have some knowledge of the state of layer's 1 and 2 (Physical and Data) of the network, but whether that knowledge is exposed to you in some way I can't begin to guess.


thepascalix

Fri, 30 Aug 2013 13:45:39 +0000

Excuse me for my bad use of word and thanks a lot for your availability to explain me the basic of network communication (and ecsue for my bad english ;))

I wanted to say in my precedent post if you know how deep i've get off in code for try to implement the mechanism that you suggest in my precedent reply (and you have response me :) )

thanks a lot for all :D


majenko

Fri, 30 Aug 2013 13:55:05 +0000

I'm not familiar with the library you are using. Can you point me to it?


thepascalix

Fri, 30 Aug 2013 14:22:38 +0000

yes, of sure. The library that i use is the chipkit DNET that i found at: http://digilentinc.com/Products/Detail.cfm?NavPath=2,892,942&Prod=CHIPKIT-NETWORK-SHIELD

this library is based on Microchip TCP/IP stack.


majenko

Fri, 30 Aug 2013 14:59:51 +0000

It looks to me like that board just extends the MAX32 with a PHY interface to the on-chip ethernet. If that is the case, then there is the EMAC1MIND register, which has bit 3:

bit 3 LINKFAIL: Link Fail bit When ‘1’ is returned - indicates link fail has occurred. This bit reflects the value last read from the PHY status register.

Monitoring that bit could indicate if the cable is connected or not

if (EMAC1MIND & 0x08) {
  // ....
}

thepascalix

Fri, 30 Aug 2013 15:14:00 +0000

yes is true. If i disconnect the cable from my max32 chipkit, it recognize the cable's disconnection and close tcpclient connection. In my application, i have a max32 where is installed a modified TCP server example, that send always information when a connection is alive, a computer that connetc to max32 program and receive its message, and a router where max32 and my Pc are connected. So, if i disconnect my PC from router (unplug the cable), I aspect that program in max32 recognize the lost of connection, but how have you tell in precedent reply, i have to do anything and retrieve the forced drop connection of my Pc.

This is my (simplified) problem :? .


majenko

Fri, 30 Aug 2013 15:53:45 +0000

Ah, it's detecting the remote connection state that you're after. Not much to be done about that at all. There is a timeout setting for write commands, which might speed things up if you reduce it:

/***    void SetSecTimeout(unsigned int cSecTimeout)
**
**  Synopsis:   This routine allows you to set the timeout value for
**              write methods. By default this is 
**              set to 30 seconds.
**              This is a Digilent extension.
**
**  Parameters:
**      cSecTimeout - The maximum number of seconds to wait for the API to complete
**  
**  Return Values:
**      None
**
**  Errors:
**
**  Description:
**      This will apply to all of the client writes in the server list.
*/
void Server::SetSecTimeout(unsigned int cSecTimeout)
{
    _cSecTimeout = cSecTimeout;
}

... Worth a try...?


thepascalix

Fri, 30 Aug 2013 18:17:59 +0000

Yes, i will try and update my progress. :D

and if i have problem, i'll repost on this topic :lol:


thepascalix

Mon, 02 Sep 2013 14:40:49 +0000

hi majenko, i'm here, again.

Your suggest is referred to another class and another Library. I use the DNETLibrary. But i've try to use the other library without any result.

Now i try to add a timer or anythings for do that i've need.


majenko

Mon, 02 Sep 2013 14:58:57 +0000

The library I was looking at is the one on the page you linked to. where is the actual library you are using?


thepascalix

Mon, 02 Sep 2013 15:44:23 +0000

hi, in the downloaded zip file that you retrieve from digilent, there are more than one library. I'm using the DNETcK library.

[url]http://www.digilentinc.com/Agreement.cfm?DocID=DSD-0000318[/url]

thank you so much for help