Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

socket

Posted on 2001-07-20
12
Medium Priority
?
331 Views
Last Modified: 2010-04-21
i am new to linux
i have a server program written in c.
its a socket server program.it 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
0
Comment
Question by:v_iyengar
[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
12 Comments
 
LVL 3

Accepted Solution

by:
superschlonz earned 100 total points
ID: 6303701
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.

0
 
LVL 8

Expert Comment

by:mnashadka
ID: 6304021
You could also use a signal handler (on SIGUSR1 or something like that) and send some kill signal to the process id.
0
 

Author Comment

by:v_iyengar
ID: 6304290
tell me more clearly as i am new to linux
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:v_iyengar
ID: 6304296
how to trap ctl-c ,what signal should i trap
0
 

Author Comment

by:v_iyengar
ID: 6304377
how to trap ctl-c ,what signal should i trap
0
 
LVL 8

Assisted Solution

by:mnashadka
mnashadka earned 100 total points
ID: 6304721
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;
  exit(0);
}

int main()
{
  signal(SIGTERM, handle_signal);
  sleep(120);

  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.
0
 

Author Comment

by:v_iyengar
ID: 6307381
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

0
 
LVL 8

Expert Comment

by:mnashadka
ID: 6308414
Ctrl+C should be SIGINT, so there's no reason that your signal handler shouldn't handle it.
0
 

Author Comment

by:v_iyengar
ID: 6311496
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 ?
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6979314
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.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

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.
http://www.experts-exchange.com/questions/Q.20131796.html
http://www.experts-exchange.com/questions/Q.20139126.html
http://www.experts-exchange.com/questions/Q.20154396.html
http://www.experts-exchange.com/questions/Q.20294809.html

To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20252012.html

*****  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
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://www.experts-exchange.com/commspt/Q.20277028.html
 
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.
Moondancer
Moderator @ Experts Exchange
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9871680
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.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

670 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