Solved

How to close a tcp-ip socket connection in a common way ?

Posted on 2004-03-30
8
587 Views
Last Modified: 2010-04-21
Hi experts,
I'm programming a client server application with TCP/IP socket connection. Program runs successfully.
But when I shutdwon the server and try to restart it ,I sometimes can't build the socket for a while and I receive
an socket errror. After ~ 1 minute waiting - it works. I guess the problem is the socket closing. Can you
recommend a offical handshake procedure? Do you have some code snippets for the client and the server
side program how to close the socket officially.
What do you think. Am I on the right track?
Thanks in advance
Martin
0
Comment
Question by:onsight
8 Comments
 
LVL 4

Expert Comment

by:oumer
ID: 10717668
I think the best examples on using sockets is the ones in the
book "unix network programming" by Richard Stevens, you can find the source code at

http://www.cis.temple.edu/~ingargio/cis307/software/StevensBook/stevens-net/

(a tarred version is there in the same page)


under the directory net you can find tcpcli.c and tcpserv.c, you can find the code on how to program a simple client and server. If you have any question regarding the code you find there, please don't hesitate to ask.....


0
 
LVL 9

Expert Comment

by:Alf666
ID: 10719135
To close the socket, just do a close(descriptor) on it before exiting. This is the proper way.
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 125 total points
ID: 10720951
you are facing the problem of lingering sockets ...

use setsockopt to either set SO_LINGER .... this will delay exit of close() or shutdown() until all packets are put on wire and socket is actually closed ... By default, this is done in background after close has returned success

Another way would be to use setsockopt to set SO_REUSEADDR ... this will allow new instance of your server to reuse that socket even if it has not yet been closed completely by previous operation

man 7 socket
man setsockopt
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 2

Expert Comment

by:PaulS_III
ID: 10726068
Another way to avoid this issue is to set your sever to run under xinet.d.

This way you are allowing the kernal to handle the creation and destruction of the socket. You don't have to worry about coding it at all.

If you would like I can provide you with more information

Paul
0
 
LVL 4

Expert Comment

by:NVRAM
ID: 10734500
PaulS_III's comment will help if you don't need to maintain any state information (in memory) between invocations.  I once wrote a web servers in K-Shell using this inetd!

But otherwise, check out the "setsockopt" man page and look at enabling SO_REUSEADDR on the socket after you bind() it.
0
 
LVL 1

Assisted Solution

by:ravenscr98
ravenscr98 earned 125 total points
ID: 10738873
You are running into the TIME_WAIT state of the TCP state machine.  A process doing an active close of a socket goes into this wait state for a duration of 2MSL, where MSL is the maximum segment lifetime.  Typical values of MSL range from 30 seconds to 2 minutes.  Thus 2MSL ranges from 1 to 4 minutes, which is why you wait 1 minute before you can restart your server.  The purpose of this delay is to allow lost TCP packets to expire.  If you restart your server and reestablish a connection with a previous client, you have the potential for lost packets from the previous connection showing up.  Also, the delay is needed to properly terminate a TCP full duplex connection.

You can test to see if this is your problem by using the "netstat -n" command after you shutdown your server.  The "-n" option displays numeric IP addresses.  This command will list all open ports and their state.  You should see that your server's port is in the TIME_WAIT state.

The solutions given in previous comments should help you solve your problem.  I'd recommend using "setsockopt" to enable SO_REUSEADDR on the socket.  Note that enabling SO_LINGER is not regarded as a reasonable solution by many experts.

The Stevens book mentioned by oumer is invaluable when writing client-server applications.  You'll find a good discussion of TIME_WAIT in Chapter 2 and "setsockopt" in Chapter 5.

- rob r.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now