Re: How Can I call close() for all
- From: sreyas.jose@xxxxxxxxx
- Date: Thu, 29 Nov 2007 22:53:29 -0800 (PST)
On Nov 28, 8:53 pm, David Schwartz <dav...@xxxxxxxxxxxxx> wrote:
On Nov 28, 4:09 am, Rainer Weikusat <rweiku...@xxxxxxxxxxx> wrote:............................................................................................................................................................
Multithreaded code operating on shared ressources without proper
serialization can behave in unexpected ways. Film at 11. Relevance
regarding the question:
I have a server application that uses a communication loop
with a select to handle multiple clients.
How Can I call close() for all
the sockets currently open? (i.e., all the clients currently
connected)
or wrt the question if close can be used to close a TCP-connection or
under which circumstances (unread data) a shutdown/ close sequence may
be more appropriate: Zero.
You are, again, dangerously wrong. I swear, sometimes it seems like
you try to give people dangerously bad advice. I warn people about bad
things for good reasons, and you always want them to ignore the
warnings. What is up with that?!
Consider:
1) You call 'select' and get a list of sockets to process.
2) The first socket you process contains a command that tells the
server shut down, so you call 'close' on all your connections.
3) You call 'syslog' to log the fact that you are shutting down.
Unbeknowst to you, 'syslog' opens a TCP connection to write the log
event. (You neither know nor care how 'syslog' works its magic.) The
socket is left open in case there are further log entries.
4) You continue processing the list you got from 'select' because you
only check for shutdown at the top of the loop. You go to the next
socket (whose descriptor now refers to the log socket) and write some
sensitive data that should not go in the log to that connection.
Same problem. Calling 'close' without logically shutting down the
connection in your program logic is dangerous. The OP is asking if he
can blindly 'close' sockets and the answer is *NO*, you must "spin
them down" in your program logic first.
DS
I had used taskSpawn for each client connection with socket option.So
How Can I call close() for all the sockets currently open? (i.e., all
the clients currently connected).?...
I wanted to develop a Echo TCP server that can handle 10 clients and
can able to close server with out affecting...there should be one
entry point as well one exit.....this is my requirnemnet based on that
I had develped tcpserver pgm given below:
Pls give suggestion for this...for graceful shut down of the
server...echo server...only one entry(instead of sp tcpServer there
should be some function to spawn server)...10 connection(This I am
passing as a macro in listen function.?
/* TcpClient.c - TCP client source
* This file implements the client-side functionality of the Client-
Server
* application.The code demonstrates the usage socket routine calls.
*/
/* includes */
#include "vxWorks.h"
#include "sockLib.h"
#include "inetLib.h"
#include "stdioLib.h"
#include "strLib.h"
#include "hostLib.h"
#include "ioLib.h"
#include "Tcp.h"
#include "stdlib.h"
#include "fioLib.h"
/
*****************************************************************************
* NAME : tcpClient
*
* INPUT : name or IP address of server
*
* DESCRIPTION : tcpClient - send requests to server over a TCP socket
*
* RETURNS : OK, or ERROR if the message could not be sent to the
server.
*
******************************************************************************/
STATUS tcpClient
(
char * serverName /* name or IP address of server */
)
{
struct sockaddr_in serverAddr; /* server's socket address */
int sockAddrSize; /* size of socket address structure */
int sFd; /* socket file descriptor */
/* char * recv_strng; */
/* recv_strng = (char *)malloc(50); */
int nRead;
/*for non blocking call*/
const int on = 1;
static char msg[]="Hello";
/* create client's socket */
if ((sFd = socket (AF_INET, SOCK_STREAM, 0)) == ERROR)
{
perror ("socket");
return (ERROR);
}
/* bind not required - port number is dynamic */
/* build server socket address */
sockAddrSize = sizeof (struct sockaddr_in);
bzero ((char *) &serverAddr, sockAddrSize);
serverAddr.sin_family = AF_INET;
serverAddr.sin_len = (u_char) sockAddrSize;
serverAddr.sin_port = htons (SERVER_PORT_NUM);
if (((serverAddr.sin_addr.s_addr = inet_addr (serverName)) == ERROR)
&&
((serverAddr.sin_addr.s_addr = hostGetByName (serverName)) ==
ERROR))
{
perror ("unknown server name");
close (sFd);
return (ERROR);
}
/* connect to server */
if (connect (sFd, (struct sockaddr *) &serverAddr, sockAddrSize) ==
ERROR)
{
perror ("connect");
close (sFd);
return (ERROR);
}
/*To make fd as a non Blocking call*/
ioctl (sFd, FIONBIO, (int) &on);
/* Write data to the server and Read back data from server. */
/* if (write (sFd, (char *)"Test String", sizeof ("Test String")) ==
ERROR) */
if (write (sFd, msg, sizeof (msg)) == ERROR)
{
perror ("write");
close (sFd);
return (ERROR);
}
/* while ((nRead = fioRead (sFd, (char *) recv_strng, 50))!= 0) */
while ((nRead = fioRead (sFd, msg, sizeof(msg)))!= 0)
{
msg[11]= '\0';
printf("\n\rnRead = %d\n\n", nRead);
printf ("MESSAGE FROM SERVER %s\n\n", msg);
memset(msg, 0, 50);
if (nRead == ERROR) /* error from read() */
perror ("read");
close (sFd); /* close server socket
connection */
/* Terminate the client program gracefully. */
exit(0);
}
return (OK);
}
.
- Follow-Ups:
- Re: How Can I call close() for all
- From: David Schwartz
- Re: How Can I call close() for all
- References:
- How Can I call close() for all
- From: sreyas . jose
- Re: How Can I call close() for all
- From: Chris McDonald
- Re: How Can I call close() for all
- From: Rainer Weikusat
- Re: How Can I call close() for all
- From: David Schwartz
- Re: How Can I call close() for all
- From: Rainer Weikusat
- Re: How Can I call close() for all
- From: David Schwartz
- How Can I call close() for all
- Prev by Date: Confused with fork & vfork
- Next by Date: rpcgen cannot find any C Preprocessor
- Previous by thread: Re: How Can I call close() for all
- Next by thread: Re: How Can I call close() for all
- Index(es):
Relevant Pages
|
Loading