473,503 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


Hi. I am getting the below error:
Event Type: Error
Event Source: PeskyService
Event Category: None
Event ID: 0
Date: 1/30/2008
Time: 5:49:10 PM
User: N/A
Computer: Computer
Description:
Only one usage of each socket address (protocol/network address/port)
is normally permitted
The service is starting (successfully) and then throwing this error
immediately after. It does this about 1,900 times in a very short
period of time and then throws an event log too full error.

I am not closing something when I should be - I feel. I have provided
the relevant methods (I think) showing where stuff gets opened and
closed.

The methods are:

protected override void OnStart(string[] args)
{
this.timer1.Enabled = true;
new Thread(new ThreadStart(Listen)).Start();

}

protected override void OnStop()
{
this.timer1.Enabled = false;
base.OnStop();

}
public void Listen()
{

Queue unsyncq = new Queue();
connectionQueue = Queue.Synchronized(unsyncq);

try
{

TcpListener listener = new TcpListener(_telnetServer,
_telnetPort);
listener.Start();

while (true)
{

_telnetSocket = listener.AcceptTcpClient();
connectionQueue.Enqueue(_telnetSocket);

Thread workingthread = new Thread(new
ThreadStart(TheConnectionHandler));
workingthread.Start();

}

}
catch (Exception e)
{
eventLog2.WriteEntry("Error in Listener:" + e.Message,
EventLogEntryType.Error);
return;
}

}

....
public void TheConnectionHandler()
{

_telnetSocket = (TcpClient)connectionQueue.Dequeue();
_clientStream = _telnetSocket.GetStream();

while (true)
{

_bytesRead = 0;

try
{

_bytesRead = _clientStream.Read(message, 0,
4096); //message

}
catch
{
//a socket error has occured
break;
}
if (_bytesRead == 0)
{
break;
}

statusMessage += ASCII.GetString(message, 0,
_bytesRead);

[SNIP]

_telnetSocket.Close();
_clientStream.Close();
}
Jan 30 '08 #1
9 38047
This normally happens when a client tries to connect on a socket on which
another connection has already been accepted...

"pbd22" wrote:
>
Hi. I am getting the below error:
Event Type: Error
Event Source: PeskyService
Event Category: None
Event ID: 0
Date: 1/30/2008
Time: 5:49:10 PM
User: N/A
Computer: Computer
Description:
Only one usage of each socket address (protocol/network address/port)
is normally permitted
The service is starting (successfully) and then throwing this error
immediately after. It does this about 1,900 times in a very short
period of time and then throws an event log too full error.

I am not closing something when I should be - I feel. I have provided
the relevant methods (I think) showing where stuff gets opened and
closed.

The methods are:

protected override void OnStart(string[] args)
{
this.timer1.Enabled = true;
new Thread(new ThreadStart(Listen)).Start();

}

protected override void OnStop()
{
this.timer1.Enabled = false;
base.OnStop();

}
public void Listen()
{

Queue unsyncq = new Queue();
connectionQueue = Queue.Synchronized(unsyncq);

try
{

TcpListener listener = new TcpListener(_telnetServer,
_telnetPort);
listener.Start();

while (true)
{

_telnetSocket = listener.AcceptTcpClient();
connectionQueue.Enqueue(_telnetSocket);

Thread workingthread = new Thread(new
ThreadStart(TheConnectionHandler));
workingthread.Start();

}

}
catch (Exception e)
{
eventLog2.WriteEntry("Error in Listener:" + e.Message,
EventLogEntryType.Error);
return;
}

}

....
public void TheConnectionHandler()
{

_telnetSocket = (TcpClient)connectionQueue.Dequeue();
_clientStream = _telnetSocket.GetStream();

while (true)
{

_bytesRead = 0;

try
{

_bytesRead = _clientStream.Read(message, 0,
4096); //message

}
catch
{
//a socket error has occured
break;
}
if (_bytesRead == 0)
{
break;
}

statusMessage += ASCII.GetString(message, 0,
_bytesRead);

[SNIP]

_telnetSocket.Close();
_clientStream.Close();
}
Jan 31 '08 #2
OK, thanks.
I sort of figured that but am unclear where exactly to post the stop
command.
If I do something like the below, the intllisense tells me that
listener.stop() will
never be reachable. Where, exactly, do i call stop??

Thanks.
TcpListener listener = new TcpListener(_telnetServer,
_telnetPort);
listener.Start();

while (true)
{

_telnetSocket = listener.AcceptTcpClient();
connectionQueue.Enqueue(_telnetSocket);

Thread workingthread = new Thread(new
ThreadStart(TheConnectionHandler));
workingthread.Start();

}
listener.Stop();
Jan 31 '08 #3
anybody care to address the OP? :)

Should I be flusing my queue and closing the listener?
If so, where exactly should I be placing my closing logic?

I'd appreciate any help here. Thanks!
Jan 31 '08 #4
On Thu, 31 Jan 2008 06:12:10 -0800, pbd22 <du*****@gmail.comwrote:
OK, thanks.
I sort of figured that but am unclear where exactly to post the stop
command.
If I do something like the below, the intllisense tells me that
listener.stop() will
never be reachable. Where, exactly, do i call stop??
Where you call it now would be fine, if you had any way to exit the loop.
So, one fix is to change the loop so there's a way to exit it. :)

Personally, I wouldn't bother with the loop. I'd use the asynchronous API
so that I didn't have a thread sitting blocked at a call to an Accept
method, and thus I could stop and close the socket wherever I like without
worrying about some thread that I needed to coordinate with.

But barring that, another alternative would be to put a try/catch around
the call to AcceptTcpClient() and then just stop the listening socket from
somewhere else when you need to shut down the service. Then you catch the
exception that's thrown when the socket is stopped and closed, and in that
way detect that it's time to exit the loop (of course, in that scenario
there's no need to call Stop() after the loop, since calling Stop() and
Close() would be what got you out of the loop in the first place :) ).

Pete
Feb 1 '08 #5
Thanks Pete.

So, something like this?

TcpListener listener = new TcpListener(_telnetServer, _telnetPort);
listener.Start();

while (true)
{
try
{
_telnetSocket = listener.AcceptTcpClient();
}
catch (Exception)
{
break;
}
finally
{
connectionQueue.Enqueue(_telnetSocket);
Thread workingthread = new Thread(new
ThreadStart(TheConnectionHandler));
workingthread.Start();
}

}

And, elsewhere in the program, when it is time to exit, i call:

_telnetSocket.Close()?

Thanks again,
Peter
Feb 1 '08 #6
On Fri, 01 Feb 2008 09:37:58 -0800, pbd22 <du*****@gmail.comwrote:
Thanks Pete.

So, something like this?
No, definitely not. The "finally" clause will _always_ execute, whether
an exception occurs or not. You don't want to go starting up a new client
connection thread if AccepTcpClient() throws an exception.

So, skip the "finally" clause...just leave that code outside the try/catch
block, and leave the "break" in the "catch" clause so that the loop exits
when an exception is thrown.

Pete
Feb 1 '08 #7
Pete:

What exactly am I doing wrong here?

Cannot access a disposed object.
Object name: 'System.Net.Sockets.NetworkStream'.

I pretty easy stab is that I have closed my network stream and i am
trying to open it again.
What I don't get is that the prior connection closed everything and
the next user has to log on again (telnet 192.22.23.4).
So, shouldn't the new session open the stream as it always does?

Peter
Feb 1 '08 #8
Thanks.

I think the "refreshed data" may be my problem. How do I ensure that,
on the next
connection, the buffer is clean and that no "old" data will be
processed?

For what it is worth, If something goes bad in my program (either via
user logic or program error) this is
what i use:

private void SendResponse(string message)
{

//add some message to the buffer here...

_clientStream.Write(buffer, 0, buffer.Length);
loginState = LoginState.Login;
_telnetSocket.Close();

}
Here is my handler that sets up the connection

public void TheConnectionHandler()
{

_telnetSocket = (TcpClient)connectionQueue.Dequeue();
_clientStream = _telnetSocket.GetStream();

buffer = ASCII.GetBytes(" ");
_clientStream.Write(buffer, 0, buffer.Length);
_clientStream.Flush();

while (true)
{

_bytesRead = 0;

try
{

//blocks until a client sends a message
_bytesRead = _clientStream.Read(message, 0,
4096); //message

}
catch
{
//a socket error has occured
break;
}
if (_bytesRead == 0)
{
break;
}

statusMessage += ASCII.GetString(message, 0,
_bytesRead);

// and so on...

PS. "I pretty easy stab = A pretty easy stab". Was typing fast.
Feb 1 '08 #9
Yeah, its the statusMessage variable.

Its not clean when a new connection happens.

A clean version is:

statusMessage = <x><y></y><z></z></x>

When the error happens, statusMessage reads:

statusMessage = <x><y></y><z></z></x><

the last "<" must have carried over from the former connection.

How do I make sure this doesn't happen?

Thanks.
Peter

Feb 1 '08 #10

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

Similar topics

0
1402
by: Josh | last post by:
I'm getting an error I dont understand. Here is the output: Content-Type: text/html Traceback (most recent call last): File "C:\PROGRA~1\APACHE~1\APACHE2\CGI-BIN\test.py", line 12, in ?...
1
3770
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket...
0
2817
by: Alphamacaroon | last post by:
All, I'm wondering if anyone can help me with a strange problem I'm having. First off, here's what I'm trying to do: I'm developing a UDP network application that should allow the end user to...
3
2481
by: Fredric Ragnar | last post by:
Hi, I am making a prototype system with Remoting in the bottom of the system. An XML Web Service is using the remote object on an IIS to present data. I am using a TcpChannel for communicating...
2
13662
by: Beemer Biker | last post by:
I have an app that binds to 5003 and 5004. It works fine on a desktop VS2005/sp1 dotnet 2. It will not run on a laptop VS2005/sp1 dotnet 2 giving the above error on the very first bind. I...
2
1912
by: yogesh | last post by:
i developing a game in c++, for that i got the socket program is working successfull in freebsd , but the same program is shifted to the Redhat linux 9.0 . i change the ip address and the port...
0
3101
by: jkhan | last post by:
Hi! I have problem which I shown in title and below as well. I have a Form name as Cancelation. The clientThread2() method is called on Form Load if I Close the Form and want to reload then the...
1
2531
by: =?Utf-8?B?Um9kcmlnbyBHYXJjw61h?= | last post by:
I have a client making web service calls to a NLB cluster. When the cluster was comprised by two machines everything went fine serving near two hundred service requests per second. Now I increased...
1
5759
by: sdbranum | last post by:
For those who have found it impossible to get help in communicating with Wasp's WDT-2200 Barcode Scanner through a COM Port, except by using their supplied DTComm software, I am presenting what I...
0
7207
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,...
0
7093
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
7357
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
7468
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...
0
4690
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...
0
3180
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...
0
1522
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 ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
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...

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.