?
Solved

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

Posted on 2004-03-30
8
Medium Priority
?
606 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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
Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

 
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 500 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

801 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