473,413 Members | 1,811 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Socket Port going to CLOSE_WAIT after repeated accept

Hi All,

I have written an application, a part of which, in the application, is a socket server programe accepting connection requests from socket clients.

My server code typically contains,

a call to socket, bind, listen, accept and later read and write using the accept file descriptor (fd).

The client socket prog is so designed (who ever did so!) that every certain time period it closes the connection with the server and reconnects again with the server.

To handle the same how I have designed my code is, when my application finds that the connection with the client is closed it calls the accept function again to accept new connection request from the client.

Before accepting a new connection I was not closing the exiting accept fd.

This was working fine for some time but recently when I had to leave the application running for a longer time, I noticed that there were too many instances of the port (to which the server and client were talking) were in CLOSE_WAIT state, which sure was looking annoying and obiously not good for the system.

Trying to dig into what could be the possible reason, thinking that since I was not closing the accept fd before accepting the new connection request from the client, I introduced a close system call before the accept call to cose the previous accepted fd.

But this gives me a bizarre response like the client comes down immediately after reconnection etc.

I would be thankful if anyone could please help me on this as to how to handle this scenerio.

PS: Sorry for a long explanation :(

kash123
Nov 20 '08 #1
1 4454
ashitpro
542 Expert 512MB
I've read your post partially...sorry...
However, I can tell you that, your design for server is quite wrong..
Ideally...You should fork a new process or thread after accepting the connection from client. And wait for another connection request.
It doesn't even matters that how many times client closes the connection and reconnect to server.
Code should look something like this

while(1)
{
//socket initialization and etc.
accept();
pid = fork();
if( pid == 0 )
{
//Do what you want to do here,read/write etc
}

}

accept() always returns new socket descriptor. And you are supposed to read/write using that fd.
So, whenever your client connects to server, It'll be communicating with it by new socket.

Regards,
Ash
Nov 20 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: DreJoh | last post by:
I've read many articles on the subject and the majority of them give the same solution that's in article 821625 on the MSDN website. I'm using the following code and when a the client disconnects...
2
by: L.J.SONG | last post by:
I use following code to create socket and connect to a server app: /// code ///// Socket connecter = null; IPEndPoint localEp = null; IPEndPoint remoteEp = null; IPHostEntry he =...
0
by: jm | last post by:
I have a program listening on a socket and one sending data to that socket. The program sending the data to the listener was periodically giving me errors that the listener didn't have a socket....
2
by: Silby | last post by:
I've been messing around with sockets for a bit and i got most of it down (i think). The server side is an asynchronous tcp socket listener with sits and waits for data. The client side uses...
6
by: Sean | last post by:
Hi Everyone, My apologies for a somewhat dump question but I am really stuck. I have been working on this code for two days straight I am dont know what is wrong with it. when I run the code, All...
0
by: =?Utf-8?B?QWxwZXIgQUtDQVlPWg==?= | last post by:
Hello, First of all I wish you a good day. My help request is about .NET asynchrounus socket communication. I have developed Server-Client Windows Forms .NET applications in VC++ .NET v2003. I...
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
7
by: silverburgh.meryl | last post by:
Hi, I read the following code which open a server socket for client request. However, i would like to know how can I change it so that i just listen for client requestfor 10 seconds, after...
1
by: tvnaidu | last post by:
I have receiver socket on 8901 and sender socket on 9801, I can see receiver quue going up and state "CLOSE_WAIT" and sender Connection established, but receiver side an error, any idea?. tcp ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.