473,748 Members | 8,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SocketException/How To Close Connections?

I have a program that repeatedly opens and closes TCP connections to a
host on the same computer:

while (true) {
using (TcpClient client = new TcpClient("loca lhost", 54321)) {
using (Stream stream = client.GetStrea m()) {
using(StreamRea der reader = new StreamReader(st ream)){
...
}
}
}
}

These connections have very short lifetimes, and it seems to me that I'm
somehow using them up! After a while, I get this SocketException :

"Only one usage of each socket address (protocol/network address/port)
is normally permitted."

However, if I catch this exception and sleep for a few seconds, I can
resume making connections again for a short while before another such
exception is thrown, and so on and so forth. Can somebody explain to me
what's going on? Please help, thanks in advance.

An aside: in the example code above, is it good practice to give the
StreamReader (or StreamWriter) its own "using" block? I'm thinking not,
because there always seems to be a way to directly access the underlying
stream and, hence, close the stream. Besides, that would make more
sense; it's the Stream, not the StreamReader, that's using resources,
right? And in that case, why is the StreamReader even made IDisposable
at all? To close its StreamReader(st ring filename) "shortcut"
constructor? I begin to think this shortcut was a bad idea....
Nov 15 '05 #1
0 1942

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
11055
by: Amit Yadav | last post by:
Hi, I am trying to write a simple TCP Client Server program which exchange some text messages. Both of my Client and Server are running on the same machine and hence i am using "localhost" when connecting from the client side. However i am getting the following Exception:
4
18132
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed as I find appropriate. I am using the socket asynchronously by calling the BeingSend and BeginReceive calls. I would like to be able to call shutdown and close asynchronously if possible.
0
2906
by: Alessandro | last post by:
Hi, I can't figure out why this code gives me the SocketException with ErrorCode 10049 when Bind method is called. Notes: 1)I have no Firewall and I can reach all servers on the Internet. 2) In my code the member this.m_sRemoteHost is the correct IP address
35
11418
by: Eric Sabine | last post by:
In my Finally block, I was using cn.close (where cn is an ADO.NET connection object, SQLConnection to be exact) and then I came across the following in some microsoft code. If Not cn Is Nothing Then CType(cn, IDisposable).Dispose() End If I have to admit, I'm not sure what happens here. Will someone explain this line of code (the middle one, not the if statement LOL) to me please?
9
6813
by: Henrik | last post by:
I'm developing an application that uses two threads to communicate with two different devices on my network. One of the devices is reached through an API from the supplier of that device. The API has support for detecting when the connection to the device is lost and signal back to my application. When I use this whitout starting the other thread everything works fine. The problem occurs when I start the other thread where the following...
7
5604
by: Lee | last post by:
Hey all, I'm using the following code to send stuff accross the network, appologies for it being in full, but I've really no idea exactly where this error is occuring. =======network code============ using System; using System.Net; using System.Net.Sockets; using System.Threading;
1
4468
by: larspeter | last post by:
Hi all. I have a problem with TcpClient ... I am conneting to a server with TcpClient and returning the answer through a webservice. It actully all works fine. BUT if I make a lot of (re)connection (hitting the submit button) then I start to recieve a an error: IOException:System.IO.IOException: Der kunne ikke læses data fra transportforbindelsen (LPF: could not read data from the transport
2
1924
by: XenReborn | last post by:
Hi, I wrote a simple socket application, which connects to a simple socket server application I also wrote, the server opens a socket, listens, accepts connections, sends back a string of information and then disconnects. Problem is the client, is the harder to manage, I use blocking mode to receive, after the timeout receive is supposed to throw a socketexception (according to the docs) but it never does... please help me... the code...
4
3320
by: Rainer Queck | last post by:
Hello NG, in my application I get a SocketException in my receive callback method, if the remote socket closes. What would be the right way to treat this exception? I want to keep my socket working, so that I can continue to receive messages from the remote application, as it comes back online. My problem at the moment is, that if I call the udpClient.BeginReceive in this moment, I again get the SocketException and from now on can't...
0
8984
marktang
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...
0
8823
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9312
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,...
0
8237
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
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();...
0
4593
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...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
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
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.