🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #49

Socket Programming Help

#49
From: Kenneth Soh
Date: Wed, 04 Nov 1992 14:30
53 lines
2312 bytes
Hi ! I have a problem in TCP/IP programming. Hope someone
can help.

I am developing a client-server based application. I 
need to write a deamon program which resides on the 
server (SUN) and loops indefinitely to accept() client
requests. The deamon will then fork() a process
for every new incoming request from the client.

I keep having "Address already in use" (errno 48) problem
everytime I restart the deamon with the same port number -
the bind() function returns error. The same port number
seems to be "released" and re-usable after say, half an
hour.

I hence suspect that those child processes which I spawned
might have inherited duplicated socket descriptors. The deamon
program was then modified so that, upon connection, simply
close() all the sockets and exits - no forking at all.
I have the same problem even with this simpler version.

My observation is that, if I start the deamon program and
CTRL-C it while it is blocking at accept(), the above problem
ceases and I can restart the deamon. In summary, only after
a connection is made, the deamon will fail at bind() when
re-executed.

I guess the problem is in the use of accept() but I don't
understand want actually went wrong. Below is my "program":

------------------------------------------------------
universal_socket = socket( AF_INET, SOCK_STREAM, 0 );

sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = atoi(argv[1]); <-- just a port number
bind( universal_socket, &sin, sizeof( sin ) )

listen( universal_socket, 3 );

client_socket = accept( universal_socket, 0, 0 );

close( client_socket );
close( universal_socket );
------------------------------------------------------

Also... can someone tell me when should we use shutdown() ?
I've tried to use it but I don't really understand what it
is used for.

Regards,
Kenneth Soh
Singapore
Technical Details
Message-ID: <1992Nov4.143004.22495@nuscc.nus.sg>
Path: arc1-ca.pugleaf.net!sparky!uunet!europa.asd.contel.com!emory!wupost!gumby!yale!yale.edu!jvnc.net!nuscc!solomon.technet.sg!scstech

Show Full Headers
Complete Article Headers
Newsgroups: comp.unix.programmer
Path: arc1-ca.pugleaf.net!sparky!uunet!europa.asd.contel.com!emory!wupost!gumby!yale!yale.edu!jvnc.net!nuscc!solomon.technet.sg!scstech
From: scstech@solomon.technet.sg (Kenneth Soh)
Subject: Socket Programming Help
Message-ID: <1992Nov4.143004.22495@nuscc.nus.sg>
Sender: usenet@nuscc.nus.sg
Organization: Singapore Computer Systems Ltd
Date: Wed, 4 Nov 1992 14:30:04 GMT
Lines: 53