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
1 4478 ashitpro 542
Recognized Expert Contributor
I've read your post partially...sor ry...
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 the child socket remains in
the CLOSE_WAIT state. Anyone have any idea what's missing?
----------------------------- Socket Code -----------------------------
namespace Sockets
{
#region Class - SocketClient
|
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 = Dns.Resolve("localhost");
IPAddress localAddr = he.AddressList;
localEp = new IPEndPoint(localAddr,11000);
|
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. I waited a little
bit, seconds to minutes and tried again and it worked.
I went to the PC listening and issued netstat -a. I found the
LISTENING socket. But I found a bunch of open ports (all the same as
the listening port.) The listener...
|
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 TcpClient to connect.
Connecting, sending and receiving data, error handling, ... all works fine.
The problem is when closing the connection.
Now as i understand it, closing the socket works as follows:
client -> tcp finish command
|
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 I get is Input: and the program
quits. I also tried reading this online but I didn't quite get it.
What is the diff between sin_addr and sin_addr.s_addr. My understanding
is that the latter is the IP address of my machine where as the former
is...
| |
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
have several problems re-establishin connection between peers. Below are my
problem cases after closing of the first successfull communication;
#1) I re-start the Server to accept connection requests. While it is
waiting, I run the Client. It is...
|
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 mechanisms.
I use a non-blocking approach for this, using the call to 'poll' to
support the async mechanism - rather than the 'begin' and 'end'
functions. I already found that connecting doesn't set the
"isconnected" variable correctly...
|
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 that, it bows out?
Code:
|
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 52927 0 10.138.156.41:8901 150.169.7.110:4493 CLOSE_WAIT
tcp 52927 0 10.138.156.41:8901 150.169.7.110:4492 CLOSE_WAIT
tcp 52927 0 10.138.156.41:8901 150.169.7.110:4495 CLOSE_WAIT
tcp 52927 ...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |