[C++] [Unix] TCP non-blocking. Detect server disconnection procedure over, from client.


 
Thread Tools Search this Thread
Top Forums Programming [C++] [Unix] TCP non-blocking. Detect server disconnection procedure over, from client.
# 1  
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..
# 2  
Old 01-11-2012
Quote:
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?
At the application level, you cannot - unless you have some out-of-band mechanism such as another connection between the client and server which will enable the server to send a message to the client confirming that the socket was successfully "disconnected".
This User Gave Thanks to fpmurphy For This Post:
# 3  
Old 01-12-2012
Hi!

Thanks for answer fpmurphy!
You have right, this is impossible to CLOSE socket and then read from this socket any confirmation about disconnection... but I found solution for my problem -> let's focus on my additional question:
"How to start disconnecting procedure, without closing socket?" -> This is possible.
I didn't share all information about case that I try to resolve, but to make my solution helpful for others, let's say that you are in situation, when server sent all information to client, and now client don't have more requests. Time to close connection, but as in my case, client have to be sure, that server really close connection (in non-blocking tcp type). What I did:
In client I call shutdown(SHUT_WR). This inform server, that client want to close connection (select on server side inform, that there is data to read on socket, but after read it server got 0 -> no more information from client -> disconnection!). So server close socket permanently. Because of this, client got 0 after select + read procedure on the same socket -> server finished disconnection! Now client close socket too, and make new connection on other socket without a problem!
Case solved!

Thanks for everyone for reading this thread, especially fpmurphy.

Ikeban
This User Gave Thanks to ikeban For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question