This is a very modest server that serves only one client and then turns itself off. The service provided is to read one byte from the client and then to write the byte back to the client.
SO_REUSEADDR
allows us to bind another TCP/IP server to the same port
while the original server is still in its death throes
(i.e., while the socket of the original server is still in the
TIME_WAIT
state).
The client is launched after the server prints the
SO_REUSEADDR
value.
g++ -o echoserver echoserver.C -lsocket -lnsl ls -l echoserver ./echoserver The SO_REUSEADDR option of the socket is 4. I have accepted a client whose IP address is 127.0.0.1.
g++ -o echoclient echoclient.C -lsocket -lnsl ls -l echoclient ./echoclient Trying 127.0.0.1... Connected to localhost. A
The
manual page
for OS X
getsockopt
says that the fifth argument has to be a pointer to a
socklen_t
rather than a pointer to a
size_t
,
so change the type of the variable
opt_length
in
echoserver.C
to
socklen_t
.
I ran
netstat
after launching the server.
The server is in the
LISTEN
state.
netstat -a -f inet -P tcp TCP: IPv4 Local Address Remote Address Swind Send-Q Rwind Recv-Q State -------------------- -------------------- ----- ------ ----- ------ ----------- i5.ssh cpe-66-108-119-216.nyc.res.rr.com.50471 17368 0 65700 0 ESTABLISHED i5.ssh TSOA0843-5WK-OSX.PHOTO.TSOA.NYU.EDU.65329 66608 0 66608 0 ESTABLISHED i5.ssh 172-26-25-243.DYNAPOOL.NYU.EDU.61245 15388 0 64860 0 ESTABLISHED i5.ssh 172-26-25-243.DYNAPOOL.NYU.EDU.61260 16260 0 64860 0 ESTABLISHED i5.80 208.91.121.170.rbitech.net.62716 56940 0 65700 0 TIME_WAIT *.3000 *.* 0 0 65536 0 LISTEN *.sunrpc *.* 0 0 65536 0 LISTEN *.* *.* 0 0 65536 0 IDLE *.ssh *.* 0 0 65536 0 LISTEN i5.ssh 216-165-45-96.DYNAPOOL.NYU.EDU.50769 524280 0 66608 0 ESTABLISHED *.fs *.* 0 0 65536 0 LISTEN *.servicetag *.* 0 0 65536 0 LISTEN *.80 *.* 0 0 65536 0 LISTEN *.3306 *.* 0 0 65536 0 LISTEN i5.80 172-26-25-243.DYNAPOOL.NYU.EDU.61264 15824 0 64860 0 FIN_WAIT_2 i5.80 208.91.121.170.rbitech.net.58939 65140 0 65700 0 TIME_WAIT i5.ssh 216-165-55-207.DYNAPOOL.NYU.EDU.1052 65203 0 64240 0 ESTABLISHED i5.ssh ND-IMAC-19.NDLAB.ITS.NYU.EDU.49350 524280 0 66608 0 ESTABLISHED i5.ssh ND-IMAC-19.NDLAB.ITS.NYU.EDU.49351 524280 0 66608 0 ESTABLISHED i5.ssh 216-165-55-207.DYNAPOOL.NYU.EDU.1062 65383 0 64240 0 ESTABLISHED i5.80 74.125.112.82.64037 6912 0 66646 0 ESTABLISHED i5.80 74.125.114.87.65271 17152 0 66646 0 ESTABLISHED *.smtp *.* 0 0 65536 0 LISTEN i5.ssh 172-26-13-4.DYNAPOOL.NYU.EDU.49497 524280 0 65664 0 ESTABLISHED i5.smtp RECON.ES.ITS.NYU.EDU.53099 5888 0 66608 0 TIME_WAIT i5.80 208.91.121.170.rbitech.net.62032 26280 10219 65700 0 ESTABLISHED i5.80 208.91.121.170.rbitech.net.16113 26280 7299 65700 0 ESTABLISHED i5.ssh CFSCONSULTANT.SAS.ITS.NYU.EDU.3600 65415 0 64240 0 ESTABLISHED i5.ssh 172-26-192-235.DYNAPOOL.NYU.EDU.57322 524280 0 65664 0 ESTABLISHED i5.ssh ND-IMAC-19.NDLAB.ITS.NYU.EDU.50179 524280 0 66608 0 ESTABLISHED i5.ssh KMS-LAPTOP-REPLACE.STS.ITS.NYU.EDU.49174 524280 0 66608 0 ESTABLISHED i5.ssh CFSCONSULTANT.SAS.ITS.NYU.EDU.3613 64631 0 64240 0 ESTABLISHED i5.80 172-26-25-170.DYNAPOOL.NYU.EDU.49586 57960 0 66240 0 FIN_WAIT_2
I ran
netstat
again, after the server had finished serving the client.
netstat -a -f inet -P tcp TCP: IPv4 Local Address Remote Address Swind Send-Q Rwind Recv-Q State -------------------- -------------------- ----- ------ ----- ------ ----------- i5.ssh cpe-66-108-119-216.nyc.res.rr.com.50471 17368 0 65700 0 ESTABLISHED i5.ssh 172-26-25-243.DYNAPOOL.NYU.EDU.61245 15460 0 64860 0 ESTABLISHED i5.ssh 172-26-25-243.DYNAPOOL.NYU.EDU.61260 16260 0 64860 0 ESTABLISHED *.sunrpc *.* 0 0 65536 0 LISTEN *.* *.* 0 0 65536 0 IDLE *.ssh *.* 0 0 65536 0 LISTEN i5.ssh 216-165-45-96.DYNAPOOL.NYU.EDU.50769 524280 0 66608 0 ESTABLISHED *.fs *.* 0 0 65536 0 LISTEN *.servicetag *.* 0 0 65536 0 LISTEN *.80 *.* 0 0 65536 0 LISTEN *.3306 *.* 0 0 65536 0 LISTEN i5.ssh 216-165-55-207.DYNAPOOL.NYU.EDU.1052 65203 0 64240 0 ESTABLISHED i5.ssh ND-IMAC-19.NDLAB.ITS.NYU.EDU.49350 524280 0 66608 0 ESTABLISHED i5.ssh ND-IMAC-19.NDLAB.ITS.NYU.EDU.49351 524280 0 66608 0 ESTABLISHED i5.ssh 216-165-55-207.DYNAPOOL.NYU.EDU.1062 65383 0 64240 0 ESTABLISHED *.smtp *.* 0 0 65536 0 LISTEN i5.ssh 172-26-13-4.DYNAPOOL.NYU.EDU.49497 524280 0 65664 0 ESTABLISHED i5.80 165.155.200.70.42865 65535 0 64860 0 ESTABLISHED i5.80 165.155.200.70.45555 65028 0 64860 0 FIN_WAIT_2 i5.ssh CFSCONSULTANT.SAS.ITS.NYU.EDU.3600 65415 0 64240 0 ESTABLISHED i5.ssh ND-IMAC-19.NDLAB.ITS.NYU.EDU.50179 524280 0 66608 0 ESTABLISHED i5.ssh KMS-LAPTOP-REPLACE.STS.ITS.NYU.EDU.49174 524280 0 66608 0 ESTABLISHED i5.ssh CFSCONSULTANT.SAS.ITS.NYU.EDU.3613 64631 0 64240 0 ESTABLISHED i5.80 KMS-LAPTOP-REPLACE.STS.ITS.NYU.EDU.49847 66608 0 66608 0 TIME_WAIT i5.80 KMS-LAPTOP-REPLACE.STS.ITS.NYU.EDU.49870 524280 0 66608 0 ESTABLISHED i5.80 KMS-LAPTOP-REPLACE.STS.ITS.NYU.EDU.49872 524280 0 66608 0 ESTABLISHED localhost.34978 localhost.3000 73620 0 73620 0 TIME_WAIT localhost.3000 localhost.34978 73620 0 73621 0 TIME_WAIT i5.80 cpe-71-79-30-47.cinci.res.rr.com.61749 524280 0 66608 0 ESTABLISHED i5.80 cpe-71-79-30-47.cinci.res.rr.com.61750 524280 0 66608 0 ESTABLISHED i5.80 cpe-71-79-30-47.cinci.res.rr.com.61751 524280 0 66608 0 ESTABLISHED i5.80 cpe-71-79-30-47.cinci.res.rr.com.61752 524048 0 66608 0 ESTABLISHED
To run the client on a Mac, change
the
"localhost"
in the client to
"i5.home.nyu.edu"
.
Trying 128.122.109.53... Connected to i5.home.nyu.edu. A ./a.out: Socket is not connected echo $? 8