fclose(obj) does not work immediately

3 views (last 30 days)
SRii
SRii on 24 Apr 2022
Edited: Walter Roberson on 27 Apr 2022
I have a TCPIP conncetion and I send and read data with an app designer application.
I use fclose(obj) function, but the tcp connection does not close immediately and it takes one or two minutes until it is closed.
after I close the connection I want to reopen it and I get this error:
Unsuccessful open: Address already in use:
connect

Answers (2)

Image Analyst
Image Analyst on 24 Apr 2022
In the help for tcpclient() and fclose(), I don't see any mention of using fclose() to close a tcp session. It says this:
Close the connection between the TCP/IP client and the remote host by clearing the object. Turn off the echotcpip server.
clear t
echotcpip("off")
You might also call flush() before closing just to see if that speeds anything up.
  7 Comments
SrinivasHarsha
SrinivasHarsha on 26 Apr 2022
Hi Image Analyst,
we need a help from you
can you send a response to my mail if possible.
we will contact you reg: image processing help
my mail id: srinivas.nato@gmail.com
Walter Roberson
Walter Roberson on 26 Apr 2022
Image Analyst does not volunteer email support. He will not help you through email.

Sign in to comment.


Walter Roberson
Walter Roberson on 25 Apr 2022
Edited: Walter Roberson on 27 Apr 2022
When you use tcp the default keep-alive timeout is 2 hours https://docs.microsoft.com/en-us/windows/win32/winsock/so-keepalive . When you close a socket it can take until the end of that interval before the system clears the connection.
This is an inherently hard problem in reliable asynchronous communications.
When your end says Bye, how do you know that the other end heard you so that you can remove the local resources? Well, you can require that the other end sends you a response. Can the other end clear their state immediately after sending the response? Well, how does the other end know that your end received the response? So your end has to send a response to the response. But how does your end know that the other end heard the response to the response? It has to send a response to that....
If you go through the logic, it turns out that for reliable service, you need an indefinite chain of acknowledgement of acknowledgement. So in practice you need a timeout system. If your end does not receive the last packet in the shutdown sequence but the other end has already removed the state then you might need to wait through the timeout period. This is not a MATLAB issue: it happens for all reliable asynchronous communications protocols.
  1 Comment
SRii
SRii on 27 Apr 2022
Thanks a lot for your complete explanation.
I have a Demo application which is written by C# I think. I am going to rewrite this application my self.
when I close connection in this c# demo software, it closes port immediately. same port same ip same device. but my matlab app has a problem. maybe it's because of priority or something

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!