Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

socket

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
v_iyengar
Asked:
v_iyengar
2 Solutions
 
superschlonzCommented:
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
 
mnashadkaCommented:
You could also use a signal handler (on SIGUSR1 or something like that) and send some kill signal to the process id.
0
 
v_iyengarAuthor Commented:
tell me more clearly as i am new to linux
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
v_iyengarAuthor Commented:
how to trap ctl-c ,what signal should i trap
0
 
v_iyengarAuthor Commented:
how to trap ctl-c ,what signal should i trap
0
 
mnashadkaCommented:
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
 
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

0
 
mnashadkaCommented:
Ctrl+C should be SIGINT, so there's no reason that your signal handler shouldn't handle it.
0
 
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 ?
0
 
MoondancerCommented:
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
 
jmcgOwnerCommented:
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Tackle projects and never again get stuck behind a technical roadblock.
Join Now