Re: How Can I call close() for all



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);
}




.



Relevant Pages

  • Storing data from multiple clients
    ... I'm developing a network server application which will be constantly receiving data from mobile GPRS clients and storing them in a centralized database; from the server's point of view, the clients will just be standard TCP/IP clients, sending data using a custom protocol I'll design. ... I'm going to use async sockets to handle the connections from the clients, so the data storing actions will be triggered by incoming socket data and will be handled by the socket callback methods; I think this is much better than using worker threads to wait on the sockets, since the number of simultaneous connections can potentially become quite high. ... I don't want to manually open and close a DB connection every time; I could open one when the server starts and do all of my data access using that one, but I don't know how this method works when using a multithreaded application; if I have to use some form of mutual exclusion here, then the single DB connection will become a bottleneck. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Detecting a dropped connection
    ... Are you talking about the server or the client? ... When the connection is lost, how do i then let the server know. ... I identify them by the remoteip of the socket, ... I need to be able to try and reconnect clients to the server but of ...
    (microsoft.public.win32.programmer.networks)
  • Re: select() timeouts even if there are pending connections
    ... > My server code opens a stream socket and then selects. ... > Dozens of clients connect to the server from remote sites. ... When a connection ...
    (comp.unix.aix)
  • Re: select() timeouts even if there are pending connections
    ... > My server code opens a stream socket and then selects. ... > Dozens of clients connect to the server from remote sites. ... When a connection ...
    (comp.unix.programmer)
  • Re: select() timeouts even if there are pending connections
    ... > My server code opens a stream socket and then selects. ... > Dozens of clients connect to the server from remote sites. ... When a connection ...
    (comp.unix.misc)

Loading