I have a problem with modbus master and slave TCP communication,

On windows computer: When Master and Slave connect each other they communicate successfuly on 50905->502 ports, when I disconnect to ethernet cable from master device and connect again ethernet port changing for master like 50908->502 ( than master send ack to slave but least tcp connection alive for the slave (50905->502 ) and anyone close it. Master PC couldnt close before socket, so slave device ended all new connection request for master, slave try to close before connection every connection trying. Q1) Why pc open new tcp port when on plug ethernet connection ? Why is current communication not resumed from the previous port when the connection is restored? Is that correct situation or not ? Q2) Should slave manage port close operation, or master responsible for this ?

How can I analyse my problem and solve please help me ?


According to command : I change below values:

/* Keepalive values, compliant with RFC 1122. Don't change this unless you know what you're doing */
#define  TCP_KEEPIDLE_DEFAULT     1000UL /* Default KEEPALIVE timer in milliseconds */

#define  TCP_KEEPINTVL_DEFAULT    1000UL   /* Default Time between KEEPALIVE probes in milliseconds */

and I add below line in lwipopth.h:

#define LWIP_TCP_KEEPALIVE  1// enable "kepp-alive"

When I un plug ethernet cable and connect again I get below packets forever. I couldnt solve above solution how can I solve this problem ? enter image description here

1.40 => slave, other master

1 Answer 1


If the connection was brutally closed on one side, the other side has no way of knowing it, so the socket used on that side stays open.

Even after the socket is closed, the operating system keeps the connection in limbo for some time, so will prevent any other connection from being accepted for the same quadruplet of "source address, source port, destination address, destination port".

Under Windows, you may shorten the time a connection stays around by setting TcpTimedWaitDelay.

If the code is under your control, the solution is to set the socket option in the program with setsockopt like this:

setsockopt(socket,SOL_SOCKET,SO_REUSEADDR ... )

The option SO_KEEPALIVE could be useful for the socket to check periodically if the other side is still connected.

See also this answer of mine for how to set Windows to periodically check all sockets for being still connected on both sides.

  • Thank you for your detail. On the slave side control with lwip tcp from me but master side PC . Will I change master or slave side ?
    – hobik
    Jan 12, 2021 at 7:09
  • On one or both sides, as you like. Normally SO_REUSEADDR and TcpTimedWaitDelay are used on the server and SO_KEEPALIVE may be used on both.
    – harrymc
    Jan 12, 2021 at 7:12
  • Thank you for your answer, and I will proceed this way.
    – hobik
    Jan 12, 2021 at 7:17
  • I add new updated in my topic. I try to reduce time from slave and it didnt work
    – hobik
    Jan 12, 2021 at 7:48
  • By the way my application using netcon api this is maybe different things include becase when I change the settings i didint see any difference
    – hobik
    Jan 12, 2021 at 10:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .