i am new to linux
i have a server program written in c.
its a socket server creats a socket on port 1234 which is listining.then my program goes into an infinite loop for accepting client socket.
i close the program by pressing ctl-c .
i need a method to close the server program properly
so the i can close the Listining socket

Some time after closing the program using ctl-c ,the next time when i run the program ,i get error message ,"Socket already bound"

is there any method or linux command to close the socket

please help me
Who is Participating?
I think you have to use the shutdown method:

shutdown( socket, 2 );
close( socket );

If you want to close a connected socket you first have to shutdown sending, then if you read zero bytes from the socket shutdown both and then close it:

shutdown( clientsock, 1 );
read the buffered data...
shutdown( clientsock, 2 );
close( clientsock );

Have also a look at select and poll functions.

You could also use a signal handler (on SIGUSR1 or something like that) and send some kill signal to the process id.
v_iyengarAuthor Commented:
tell me more clearly as i am new to linux
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

v_iyengarAuthor Commented:
how to trap ctl-c ,what signal should i trap
v_iyengarAuthor Commented:
how to trap ctl-c ,what signal should i trap
A signal handler will allow a program to clean itself up or perform some other action when it receives a signal.  When you type kill, by default the SIGTERM signal is sent.  Of course, with the -s option, you can specify which signal to send.  Here's an example of a simple signal handler:

#include <signal.h>
#include <unistd.h>
#include <iostream>

void handle_signal(int signal)
  std::cout << "In signal handler" << std::endl;

int main()
  signal(SIGTERM, handle_signal);

  return 0;

When you type kill <process id>, this will spit out the "In signal handler" string and exit.  This is the same kind of thing that you need.  You'll replace handle_signal with whatever cleanup code that you need for your socket or whatever, and when you type kill <process id>, the cleanup that you need will be executed.  I hope this helps.
v_iyengarAuthor Commented:
i am using signals now
        sigset(SIGCHLD, child_end);
        signal(SIGTERM, signal_handler);
        signal(SIGQUIT, signal_handler);
        signal(SIGINT, signal_handler);
Now the socket is closing properly
but some time when i press ctl-c and close program the socket is not closed.

do i have to trap some more signal

Ctrl+C should be SIGINT, so there's no reason that your signal handler shouldn't handle it.
v_iyengarAuthor Commented:
in my signal handler  i have written the code to close the socket.It works fine. But some time very rarely the socket is not closed
is there any situation when a socket can not be closed ?
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder or Mindphaser will return to finalize these if they are still open in 7 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.

To view your locked questions, please click the following link(s) and evaluate the proposed answer.

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link 
POINTS FOR EXPERTS awaiting comments are listed in the link below
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
Thanks everyone.
Moderator @ Experts Exchange
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Splt: superschlonz {http:#6303701} & mnashadka {http:#6304721}

Please leave any comments here within the next seven days.

EE Cleanup Volunteer
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.