× 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.



Anybody here good with sockets programming?

I'm having a problem getting the setsockopt REUSEADDR to work.

I have a C "server" program, actually, "listener" would be more accurate
because it is just a single-threaded program that can only accept one
connection at a time.

I want to be able to run this program in a loop, so that after it runs,
it starts back up and listens again. I cannot perform this loop within
the program itself because of other processing that needs to occur.
Therefore, the flow is:

PGM
CLPtop:
misc. processing
socketpgm
sock1 = socket
setsockopt sock1 SO_LINGER
setsockopt sock1 SO_REUSEADDR
bind
listen
sock2 = accept
close sock1
perform conversation on sock2
close sock2
misc. processing
goto cmdlabel(CLPtop)
ENDPGM

I intend the SO_LINGER option to allow the system to discard any
buffered/unsent data after a close (a condition that should not occur)
so I set l_onoff = 1 and l_linger = 1.

The sockopt REUSEADDR is intended to allow the local address to be
reused when the server is restarted before the required wait time
expires. Without REUSEADDR, a period of approx. 2 minute must elapse
(after my program has closed the socket and ended) before I can try to
listen on the same port again. If my program runs again without waiting
two minutes, it will receive error "Address already in use" on the bind.
After the wait period expires, the program run again, no problem.

I thought that perhaps my SO_LINGER option was conflicting with
SO_REUSEADDR, so I removed SO_LINGER. This did not help. The system
continues to tie up my port for two minutes. I can watch it in WRKCFGSTS
*CNN, sitting in a Time-wait state. The help screen says Time-wait =
"Waiting to allow the remote host enough time to receive the local
system's acknowledgment to end the connection."

It seems to me that the SO_REUSEADDR option is not working. I am doing
this on V5R2 and have not tried any other release.

Any help appreciated.

Thanks,
-Marty


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

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.