infinitloop
infinitloop

Reputation: 3011

what happens to the client side port/socket after server has disconnected and reconnected?

My application talks to pinpad device. The pinpad acts as server waiting for clients to connect. I fire my application, it connects to the pinpad I send a command I get an ack back and things are good.

Now I unplug ethernet cable from the pinpad, I send a command and after timeout my application spits out an error message saying 'device cannot be reached/found'.

Ok fine, I plug the cable back into the pinpad and now every time I try to send a command to the pinpad I get the same 'device cannot be reached/found' message. So I have to restart my application and things are good.

Does that mean that the original socket the two parties were using is lost after I unplug the cable or the port my application was talking on is useless after unplugging the cable? And now I need a new port?

The two applications are talking over TCP/IP.

Upvotes: 0

Views: 1603

Answers (1)

Celada
Celada

Reputation: 22251

You didn't say which transport protocol you're using.

If you are using TCP (stream socket), then it depends. If there is no data transmission during the interval in which the connectivity is interrupted (the connection is idle) and TCP keepalives are not configured, then there is no effect: data transmitted after connectivity is restored will go through as if there was connectivity all along. If, on the other hand, data is transmitted while the connectivity is broken, then one of the following things will happen:

  • If the connectivity interruption is brief, then the data will eventually get retransmitted after connectivity is restored and the connection will recover to normal state.
  • If the connectivity interruption is long, the TCP stack on one or both ends will eventually declare that a timeout has occured. You will either get a ETIMEDOUT error when this happens or a ECONNRESET error after connectivity is restored. In either case, the socket will not work again after this event. If you still want to communicate, the client will have to open a new connection with the server.

If you are using UDP (datagram socket), then you may get errors and timeouts if you transmit while the connectivity is interrupted, but everything will work normally again once connectivity is restored. There is no "memory".

Upvotes: 1

Related Questions