Sponsored Content
Top Forums Programming [C++] [Unix] TCP non-blocking. Detect server disconnection procedure over, from client. Post 302589331 by ikeban on Wednesday 11th of January 2012 10:31:27 AM
Old 01-11-2012
[C++] [Unix] TCP non-blocking. Detect server disconnection procedure over, from client.

Hello!

I searched forum for similar topic, with no luck, if you know one, delete this topic, and send me private message with link please.

Little background:
I have a lot of clients and one serwer. Client can make multiple connections on different ports and ips, but only one can be acctive (first have to be close before second is opened).
TCP connection is non-blocking.
Serwer and client detect different situactions (write, read, disconnect, connect) with operating system function : select().

I have to close connection in client, before i make new one. I do it like that:
Client:
Remove file descriptors from select pools, and close() file descriptor. Immediately make new connection on new port.

Serwer:
Select return 0 when read on closed port -> remove file descriptor. Then handle new connection.

That ok! but.... sometimes client make new connection really fast, and server get new connection event faster that disconnection event (disconnecting between socket is 2 phases procedure... so it take a little time).

So what I need to do:
I want to close socket in client, and wait until server confirm that disconnection was successful, before I make new connection. How to do this?
To start disconnecting procedure, I have to use close on file descriptor, but then i have to remove this file descriptor from select pool, or i will get "bad file descriptor" error from select. If I remove file descriptor from select pool, and then close... select no longer monitor this file descriptor, so i don't get any information from it.
First option was a timer -> client wait 500 ms before make new connection -> this help, but i don't want to use this 500 ms delay.. I want to make new connection as soon as server let me.
Second option that I checked, was reuse the same port.. but this is bad too, because when I try to use the same port i get error: port still used. Of course I can try to connect again, and it will finally work, but I really don't want to use this method (more reasons here, but hard to explain).

Do you have any idea how to wait in client to let server finish disconnection and detect this case? I can't modify serwer :/
Maybe there is something that i don't know about select? Should it return this error: "bad file descriptor"? There are really weak manuals on internet (not a lot of information).

Sorry for poor English skills.
Ikeban

Edit: I have other question, related to topic: How to start disconnecting procedure, without closing socket?

Last edited by ikeban; 01-11-2012 at 12:15 PM..
 

10 More Discussions You Might Find Interesting

1. Cybersecurity

RedHat9:How to find what is blocking the port 1526/tcp

I'm trying to configure IDS9.40 on Ret Hat 9. The server has opened the port 1526/tcp nmap (nmap -sT -O linux) reports correctly that the port is open. However, portqry (portqry.exe -n 192.168.0.101 -e 1526 -p TCP) reports that the port is closed for connection: TCP port 1526 (turbo... (0 Replies)
Discussion started by: Juhasz Lajos
0 Replies

2. UNIX for Dummies Questions & Answers

updating "share" without disconnection current client

I have a directory that is currently being shared by several clients. I would like to add another client and be able to mount it without kicking the other clients off. I edited /etc/dfs/dfstab and added the client's hostname towards the end the line. i.e. share -F nfs -o... (3 Replies)
Discussion started by: benq70
3 Replies

3. Programming

tcp server listening, client connecting problems

hello everyone. I tried searching for something related to this, but I figured it was time to ask my own question. I am experiencing these problems using Ubuntu 7.04 I am starting up a TCP listener/server and once connected, will act as a communication/control link with a program on another... (3 Replies)
Discussion started by: pjwhite
3 Replies

4. Programming

can-not detect TCP disconnects well

Hello everyone. Thanks for reading. I am using Ubuntu 7.04 to experience this problem: I have written my own programs that communicate to eachother and I am having a hard time detecting a TCP socket disconnect when the remote side's computer has a power-failure (for example). On the computer... (6 Replies)
Discussion started by: pjwhite
6 Replies

5. Programming

Concurrent TCP client/server

I made a program and now I need to make it concurrent. Can someone pls help me do this ? The code is this: #include <sys/types.h> #include <sys/socket.h> #include <sys/time.h> #include <netinet/in.h> #include <errno.h> #include <unistd.h> #include <stdio.h> #include <string.h>... (4 Replies)
Discussion started by: Johnny22
4 Replies

6. Programming

How detect TCP/IP socket shutdown when ethernet cable is disconnected

Hi, I want to code TCP/IP client/server in linux application capable to fastly detect ethernet cable disconnection in any condition. So I have activate SO_KEEPALIVE options and set TCP_KEEPCNT, TCP_KEEPIDLE and TCP_KEEPINTVL to 1. When I disconnect ethernet cable I have the following... (5 Replies)
Discussion started by: jeje_clb
5 Replies

7. Windows & DOS: Issues & Discussions

Office server => laptop =>client server ...a lengthy and laborious ftp procedure

Hi All, I need your expertise in finding a way to solve my problem.Please excuse if this is not the right forum to ask this question and guide me to the correct forum,if possible. I am a DBA and on a daily basis i have to ftp huge dump files from my company server to my laptop and then... (3 Replies)
Discussion started by: kunwar
3 Replies

8. Red Hat

I want to know the disconnection time of my broadband tcp/ip connection.

Hello, I want to know the disconnection time of my broadband tcp/ip connection as it got disconnected while downloading data. I use Vuze bit torrent client and JDownloader . I use broadband internet connection and my os is slack 11. The problem I have been facing for last 2/3 days that my... (1 Reply)
Discussion started by: vectrum
1 Replies

9. Solaris

Too much TCP retransmitted and TCP duplicate on server Oracle Solaris 10

I have problem with oracle solaris 10 running on oracle sparc T4-2 server. Os information: 5.10 Generic_150400-03 sun4v sparc sun4v Output from tcpstat.d script TCP bytes: out outRetrans in inDup inUnorder 6833763 7300 98884 0... (2 Replies)
Discussion started by: insatiable1610
2 Replies

10. UNIX for Advanced & Expert Users

How to detect files transmission at UNIX server ?

Dear Expert Users, I know that Unix utility ( fuser ) is used to detect "transmission" of files from Source system to "current Linux Server". I have scheduled this Unix script to work every 30th minutes of the hour and whenever files transmission starts at that very moment. And, the file... (4 Replies)
Discussion started by: schandrakar1
4 Replies
socket(n)						       Tcl Built-In Commands							 socket(n)

__________________________________________________________________________________________________________________________________________________

NAME
socket - Open a TCP network connection SYNOPSIS
socket ?options? host port socket -server command ?options? port _________________________________________________________________ DESCRIPTION
This command opens a network socket and returns a channel identifier that may be used in future invocations of commands like read, puts and flush. At present only the TCP network protocol is supported; future releases may include support for additional protocols. The socket command may be used to open either the client or server side of a connection, depending on whether the -server switch is specified. Note that the default encoding for all sockets is the system encoding, as returned by encoding system. Most of the time, you will need to use fconfigure to alter this to something else, such as utf-8 (ideal for communicating with other Tcl processes) or iso8859-1 (useful for many network protocols, especially the older ones). CLIENT SOCKETS
If the -server option is not specified, then the client side of a connection is opened and the command returns a channel identifier that can be used for both reading and writing. Port and host specify a port to connect to; there must be a server accepting connections on this port. Port is an integer port number (or service name, where supported and understood by the host operating system) and host is either a domain-style name such as www.tcl.tk or a numerical IP address such as 127.0.0.1. Use localhost to refer to the host on which the command is invoked. The following options may also be present before host to specify additional information about the connection: -myaddr addr Addr gives the domain-style name or numerical IP address of the client-side network interface to use for the connection. This option may be useful if the client machine has multiple network interfaces. If the option is omitted then the client-side interface will be chosen by the system software. -myport port Port specifies an integer port number (or service name, where supported and understood by the host operating system) to use for the client's side of the connection. If this option is omitted, the client's port number will be chosen at random by the system soft- ware. -async The -async option will cause the client socket to be connected asynchronously. This means that the socket will be created immedi- ately but may not yet be connected to the server, when the call to socket returns. When a gets or flush is done on the socket before the connection attempt succeeds or fails, if the socket is in blocking mode, the operation will wait until the connection is com- pleted or fails. If the socket is in nonblocking mode and a gets or flush is done on the socket before the connection attempt suc- ceeds or fails, the operation returns immediately and fblocked on the socket returns 1. Synchronous client sockets may be switched (after they have connected) to operating in asynchronous mode using: fconfigure chan -blocking 0 See the fconfigure command for more details. SERVER SOCKETS
If the -server option is specified then the new socket will be a server for the port given by port (either an integer or a service name, where supported and understood by the host operating system; if port is zero, the operating system will allocate a free port to the server socket which may be discovered by using fconfigure to read the -sockname option). Tcl will automatically accept connections to the given port. For each connection Tcl will create a new channel that may be used to communicate with the client. Tcl then invokes command with three additional arguments: the name of the new channel, the address, in network address notation, of the client's host, and the client's port number. The following additional option may also be specified before port: -myaddr addr Addr gives the domain-style name or numerical IP address of the server-side network interface to use for the connection. This option may be useful if the server machine has multiple network interfaces. If the option is omitted then the server socket is bound to the special address INADDR_ANY so that it can accept connections from any interface. Server channels cannot be used for input or output; their sole use is to accept new client connections. The channels created for each incoming client connection are opened for input and output. Closing the server channel shuts down the server so that no new connections will be accepted; however, existing connections will be unaffected. Server sockets depend on the Tcl event mechanism to find out when new connections are opened. If the application does not enter the event loop, for example by invoking the vwait command or calling the C procedure Tcl_DoOneEvent, then no connections will be accepted. If port is specified as zero, the operating system will allocate an unused port for use as a server socket. The port number actually allo- cated may be retrieved from the created server socket using the fconfigure command to retrieve the -sockname option as described below. CONFIGURATION OPTIONS
The fconfigure command can be used to query several readonly configuration options for socket channels: -error This option gets the current error status of the given socket. This is useful when you need to determine if an asynchronous connect operation succeeded. If there was an error, the error message is returned. If there was no error, an empty string is returned. -sockname This option returns a list of three elements, the address, the host name and the port number for the socket. If the host name cannot be computed, the second element is identical to the address, the first element of the list. -peername This option is not supported by server sockets. For client and accepted sockets, this option returns a list of three elements; these are the address, the host name and the port to which the peer socket is connected or bound. If the host name cannot be computed, the second element of the list is identical to the address, its first element. EXAMPLES
Here is a very simple time server: proc Server {channel clientaddr clientport} { puts "Connection from $clientaddr registered" puts $channel [clock format [clock seconds]] close $channel } socket -server Server 9900 vwait forever And here is the corresponding client to talk to the server: set server localhost set sockChan [socket $server 9900] gets $sockChan line close $sockChan puts "The time on $server is $line" SEE ALSO
fconfigure(n), flush(n), open(n), read(n) KEYWORDS
bind, channel, connection, domain name, host, network address, socket, tcp Tcl 8.0 socket(n)
All times are GMT -4. The time now is 07:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy