× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Franz,

Can you explain why you are closing the listener socket and then trying to re-open it?   Why not close the connection (the "accept" socket) and keep the listener open?   That is the way this is meant to be done.

It is normal for a listener to take some time to close.  This is necessary because even after you stop accepting new connections, sometimes it is needed to be able to respond to communications for the old connections in order to handle possible network errors and acknowledgements properly.  It typically takes about 2 minutes after you close the listener socket before it will be available to re-use it.

Therefore, the best solution is to write your application so that it is not necessary to close the listener and re-open it.  Simply keep it active to use for subsequent connections.  If you cannot do that for some reason, the other possible approach is to allow bind to re-use the port with the SO_REUSEADDR socket option. This allows you to re-use the port while the old listener is still active to handle the potential errors and acknowledgements.


On 8/16/2019 7:17 AM, Franz.Rauscher@xxxxxxxxxxx wrote:
Hi!

i have a problem with my rpg server program.

i am followig the code from Scott Klement to wite a simple server program.

i create a socket, bind it, listen and accept a connection. the i send a
small string and receive a small string.

after that i close the accepted socket and the original server socket used
for bind.

But sometimes the connection stay open and the close of the socket is not
successfull. i cannot restart the serer because the socket is already
bound.

when i do netstat *cnn i see the connection is in state time_wait and the
listening socket is not closed.

both close calls return withaout an error so why does the os not close the
sockets?

Grettings,
Fanz

Here is the Code:

message = 'TEST';
address = *allx'00';
address.sin_family = AF_INET;
address.sin_port = 9001;
address.sin_addr.s_addr = INADDR_ANY;

connection = socket(AF_INET:SOCK_STREAM:IPPROTO_TCP);
if bind(connection:%addr(address):%size(address) ) = -1;
rc = close(connection);
return;
endif;

if listen(connection:1 ) < 0;
rc = close(connection);
return;
endif;

newsock = accept(connection:p_address2:len);
len = send(newsock:%addr(message:*data):%size(message):0);
len = recv(newsock:%addr(ret):%size(ret):0);
rc = close(newsock);
rc1 = close(connection);
printf('message: %s %s %s <<%s>>' + x'25' : %char(rc) : %char(rc1):
%char(len): ret);

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.