473,395 Members | 1,863 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,395 software developers and data experts.

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("localhost", 54321)) {
using (Stream stream = client.GetStream()) {
using(StreamReader reader = new StreamReader(stream)){
...
}
}
}
}

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(string filename) "shortcut"
constructor? I begin to think this shortcut was a bad idea....
Nov 15 '05 #1
0 1934

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

Similar topics

4
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"...
4
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...
0
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. ...
35
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...
9
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...
7
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...
1
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...
2
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...
4
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.