Here is my code to create a TCP listener:
file name DataAdapterLaun cher.cs
public static TcpListener tcpl;
IPEndPoint ipe = new IPEndPoint(IPAd dress.Parse("0. 0.0.0"), 1024);//listen
on all local addresses
tcpl = new TcpListener(ipe );
tcpl.Stop();
tcpl.Start();
and that code handles data:
file name DataAdapterLaun cher.cs
private volatile bool monitorPort = true;
byte[] byteReadStream = null; // holds the data in byte buffer
TcpClient tcpc = DataAdapterLaun cher.tcpl.Accep tTcpClient(); //accept
connection
while (monitorPort)
{
byteReadStream = new byte[tcpc.Available]; //allocate space for data
tcpc.GetStream( ).Read(byteRead Stream, 0, tcpc.Available) ;
//read data into byte array
if (byteReadStream != null)
{
monitorPort = false;
}
}
Please note, I am not a .Net developer, so you might see something strange
in my code.
It does work though but with one problem, as soon as a client disconnects
from my listener, the CPU usage goes up 100% to my exe
and no connections can be made to it.
Any idea?
Thanks for any help. 9 3068
tcpl = new TcpListener(ipe );
tcpl.Stop();
Why the Stop?
tcpl.Start();
TcpClient tcpc = DataAdapterLaun cher.tcpl.Accep tTcpClient(); //accept
connection
How do you return to the above line?
If you never do, you can only create one connection
I posted code a time ago about how to get a multi connection server.
This is a part of that thread:
Its very easy, you have a thread just receiving connections in a loop
and
inserting the connections in a queue and then spawning a new thread
while(true)
{
Socket s = listener1.Accep tSocket();
syncedQueue.Enq ueue( s );
new Thread( new ThreadStart( workerMethod) ).Start();
}
workerMethod()
{
Socket s = syncedQueue.Deq ueue();
}
You have to use a synced queue though:
syncedqueue = Queue.Synchoniz e( new Queue() );
Like I said, I am not a c# programmer, but I need a little c# code for my
application.
I understand your commens though and will look into it.
But for now it's fine with one connection only however the following
>TcpClient tcpc = DataAdapterLaun cher.tcpl.Accep tTcpClient(); //accept connection
How do you return to the above line?
If you never do, you can only create one connection
is the reason of CPU going 100% after a client closes connection?
"Ignacio Machin ( .NET/ C# MVP )" <ig************ @gmail.comwrote in
message
news:63******** *************** ***********@s50 g2000hsb.google groups.com...
>
>tcpl = new TcpListener(ipe ); tcpl.Stop();
Why the Stop?
>tcpl.Start() ;
>TcpClient tcpc = DataAdapterLaun cher.tcpl.Accep tTcpClient(); //accept connection
How do you return to the above line?
If you never do, you can only create one connection
I posted code a time ago about how to get a multi connection server.
This is a part of that thread:
Its very easy, you have a thread just receiving connections in a loop
and
inserting the connections in a queue and then spawning a new thread
while(true)
{
Socket s = listener1.Accep tSocket();
syncedQueue.Enq ueue( s );
new Thread( new ThreadStart( workerMethod) ).Start();
}
workerMethod()
{
Socket s = syncedQueue.Deq ueue();
}
You have to use a synced queue though:
syncedqueue = Queue.Synchoniz e( new Queue() );
On Jul 18, 3:04*pm, "Markgoldin " <markgoldin_2.. .@yahoo.comwrot e:
Like I said, I am not a *c# programmer, but I need a little c# code formy
application.
I understand your commens though and will look into it.
But for now it's fine with one connection only however the following
*>TcpClient tcpc = DataAdapterLaun cher.tcpl.Accep tTcpClient(); //accept
connection
How do you return to the above line?
If you never do, you can only create one connection
is the reason of CPU going 100% after a client closes connection?
no, of course not, that line is executed AT the connection time, then
you enter in the while loop
do this, copy your code in a win or console project and see where it
gets stuck
On Fri, 18 Jul 2008 11:19:42 -0700, Markgoldin <ma************ *@yahoo.com>
wrote:
[...]
It does work though but with one problem, as soon as a client disconnects
from my listener, the CPU usage goes up 100% to my exe
and no connections can be made to it.
Any idea?
Not really. You didn't post a concise-but-complete code sample, so
there's no telling what in your code might be stuck in a loop.
That said, it's certainly possible that the loop you posted for some
reason turns out to be the problem. I admit, from the code you posted
it's not clear why your loop works at all (when would "byteReadStream "
ever _not_ be null?). But assuming there's some condition under which
you're not exiting the loop, it wouldn't surprise me to find that you wind
up consuming 100% CPU, because it's probably a scenario in which no
progress towards exiting the loop is made.
You shouldn't be using the Available property anyway. It's not a reliable
indicator of what you can read. But without a complete code sample, it's
not really possible to offer more detailed advice as to how to fix your
program.
Pete
Here is my modified code:
while (monitorPort)
{
byteReadStream = new byte[tcpc.Available]; //allocate space for data
tcpc.GetStream( ).Read(byteRead Stream, 0, tcpc.Available) ;
//read data into byte array
if (byteReadStream != null)
{
monitorPort = false;
}
System.Console. WriteLine("In the loop");
}
The change is
System.Console. WriteLine("In the loop");
When I send data to the listener I see "In the loop" two times, after
disconnecting in starts writing "In the loop" without stoping.
Any idea why it gets into the loop after disconnecting?
"Ignacio Machin ( .NET/ C# MVP )" <ig************ @gmail.comwrote in
message
news:dd******** *************** ***********@a1g 2000hsb.googleg roups.com...
On Jul 18, 3:04 pm, "Markgoldin " <markgoldin_2.. .@yahoo.comwrot e:
Like I said, I am not a c# programmer, but I need a little c# code for my
application.
I understand your commens though and will look into it.
But for now it's fine with one connection only however the following
TcpClient tcpc = DataAdapterLaun cher.tcpl.Accep tTcpClient(); //accept
connection
How do you return to the above line?
If you never do, you can only create one connection
is the reason of CPU going 100% after a client closes connection?
no, of course not, that line is executed AT the connection time, then
you enter in the while loop
do this, copy your code in a win or console project and see where it
gets stuck
Ok, valid point.
private volatile bool go = true;
private volatile bool monitorPort = true;
byte[] byteReadStream = null;
TcpClient tcpc = DataAdapterLaun cher.tcpl.Accep tTcpClient(); //accept
connection
while (go)
{
while (monitorPort)
{
byteReadStream = new byte[tcpc.Available]; //allocate space for data
tcpc.GetStream( ).Read(byteRead Stream, 0, tcpc.Available) ;
//read data into byte array
if (byteReadStream != null)
{
monitorPort = false;
}
System.Console. WriteLine("In the loop");
}
string str;
System.Text.ASC IIEncoding enc = new System.Text.ASC IIEncoding();
str = enc.GetString(b yteReadStream);
monitorPort = true;
}
It works fine as long as a client keeps connection.
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Fri, 18 Jul 2008 11:19:42 -0700, Markgoldin <ma************ *@yahoo.com>
wrote:
>[...] It does work though but with one problem, as soon as a client disconnects from my listener, the CPU usage goes up 100% to my exe and no connections can be made to it. Any idea?
Not really. You didn't post a concise-but-complete code sample, so
there's no telling what in your code might be stuck in a loop.
That said, it's certainly possible that the loop you posted for some
reason turns out to be the problem. I admit, from the code you posted
it's not clear why your loop works at all (when would "byteReadStream "
ever _not_ be null?). But assuming there's some condition under which
you're not exiting the loop, it wouldn't surprise me to find that you wind
up consuming 100% CPU, because it's probably a scenario in which no
progress towards exiting the loop is made.
You shouldn't be using the Available property anyway. It's not a reliable
indicator of what you can read. But without a complete code sample, it's
not really possible to offer more detailed advice as to how to fix your
program.
Pete
On Fri, 18 Jul 2008 13:07:41 -0700, Markgoldin <ma************ *@yahoo.com
wrote:
Ok, valid point.
private volatile bool go = true;
private volatile bool monitorPort = true;
byte[] byteReadStream = null;
TcpClient tcpc = DataAdapterLaun cher.tcpl.Accep tTcpClient(); //accept
connection
while (go)
{
while (monitorPort)
{
byteReadStream = new byte[tcpc.Available]; //allocate space for data
tcpc.GetStream( ).Read(byteRead Stream, 0, tcpc.Available) ;
//read data into byte array
if (byteReadStream != null)
{
monitorPort = false;
}
System.Console. WriteLine("In the loop");
}
string str;
System.Text.ASC IIEncoding enc = new System.Text.ASC IIEncoding();
str = enc.GetString(b yteReadStream);
monitorPort = true;
}
It works fine as long as a client keeps connection.
That's still not a complete code sample. And it raises more questions
than it answers. For example:
-- why do you have the "monitorPor t" loop at all? assuming that's the
literal code, "byteReadStream " is _never_ going to be null, and so that
loop will always execute the contained block exactly once before exiting
-- why do you decode the bytes you read? you never use the "str"
variable for anything once it's been assigned
-- how do you expect to exit out of the "go" loop? the variable "go"
is initialized to "true" and then never modified
-- in what context does that code appear? what do you expect to
happen when the connection is closed?
Also, as I mentioned before, you _really_ should not be using the
TcpClient.Avail able property. That said, if you insist, you definitely
should not be passing it as the length parameter for the Stream.Read()
method. It could change between the time you allocate the buffer and the
time you call the Read() method. Just pass "byteReadStream .Length"
instead.
Again, based on the code posted, I'm not surprised your program gets stuck
in an endless, non-yielding loop. That's just what the code you posted
looks like it would do. My guess is that if you fix your code to break
out of the loop when the connection is closed, your problem would go
away. But you would continue to have other the flaws in your code, and
you should really be looking to fix those too.
Pete
<TcpClient.Avai lable property
Yes, I am working on that and on other comments that have been made here.
< how do you expect to exit out of the "go" loop?
This loop is required because of the main purpose of the program I am trying
to adapt.
This is a push server. This product can communicate with .Net code and push
data to the browser from it.
Since my main coding environment is not .Net I am trying to talk to .Net
piece to provide data to be pushed.
One of ideas is to use TCP sockets for that.
So, I am using the push server product's .Net sample and trying to modify it
to aceept external data.
Like I said, it besically works. But now when I am testing the whole thing I
have noticed
that it only works untill I close connection to the c# listener.
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Fri, 18 Jul 2008 13:07:41 -0700, Markgoldin <ma************ *@yahoo.com>
wrote:
Ok, valid point.
private volatile bool go = true;
private volatile bool monitorPort = true;
byte[] byteReadStream = null;
TcpClient tcpc = DataAdapterLaun cher.tcpl.Accep tTcpClient(); //accept
connection
while (go)
{
while (monitorPort)
{
byteReadStream = new byte[tcpc.Available]; //allocate space for data
tcpc.GetStream( ).Read(byteRead Stream, 0, tcpc.Available) ;
//read data into byte array
if (byteReadStream != null)
{
monitorPort = false;
}
System.Console. WriteLine("In the loop");
}
string str;
System.Text.ASC IIEncoding enc = new System.Text.ASC IIEncoding();
str = enc.GetString(b yteReadStream);
monitorPort = true;
}
It works fine as long as a client keeps connection.
That's still not a complete code sample. And it raises more questions
than it answers. For example:
-- why do you have the "monitorPor t" loop at all? assuming that's the
literal code, "byteReadStream " is _never_ going to be null, and so that
loop will always execute the contained block exactly once before exiting
-- why do you decode the bytes you read? you never use the "str"
variable for anything once it's been assigned
-- how do you expect to exit out of the "go" loop? the variable "go"
is initialized to "true" and then never modified
-- in what context does that code appear? what do you expect to
happen when the connection is closed?
Also, as I mentioned before, you _really_ should not be using the
TcpClient.Avail able property. That said, if you insist, you definitely
should not be passing it as the length parameter for the Stream.Read()
method. It could change between the time you allocate the buffer and the
time you call the Read() method. Just pass "byteReadStream .Length"
instead.
Again, based on the code posted, I'm not surprised your program gets stuck
in an endless, non-yielding loop. That's just what the code you posted
looks like it would do. My guess is that if you fix your code to break
out of the loop when the connection is closed, your problem would go
away. But you would continue to have other the flaws in your code, and
you should really be looking to fix those too.
Pete
I didn't test it, but I believe the problem is an infinite loop without wait
state.
This code is an infinite loop of a call to a blocking Stream.Read()
(actually a TCP receive()). As long as the connection is alive, the Read()
shall block execution of yor application most of the time.
When the connection is closed, Read() returnes immediately, so the loop is
executed non-stop and occupies 100% CPU.
Boaz Ben-Porat
Milestone Systems A/S
Denmark
"Markgoldin " <ma************ *@yahoo.comskre v i en meddelelse
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
Ok, valid point.
private volatile bool go = true;
private volatile bool monitorPort = true;
byte[] byteReadStream = null;
TcpClient tcpc = DataAdapterLaun cher.tcpl.Accep tTcpClient(); //accept
connection
while (go)
{
while (monitorPort)
{
byteReadStream = new byte[tcpc.Available]; //allocate space for data
tcpc.GetStream( ).Read(byteRead Stream, 0, tcpc.Available) ;
//read data into byte array
if (byteReadStream != null)
{
monitorPort = false;
}
System.Console. WriteLine("In the loop");
}
string str;
System.Text.ASC IIEncoding enc = new System.Text.ASC IIEncoding();
str = enc.GetString(b yteReadStream);
monitorPort = true;
}
It works fine as long as a client keeps connection.
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
>On Fri, 18 Jul 2008 11:19:42 -0700, Markgoldin <ma*********** **@yahoo.comwro te:
>>[...] It does work though but with one problem, as soon as a client disconnects from my listener, the CPU usage goes up 100% to my exe and no connections can be made to it. Any idea?
Not really. You didn't post a concise-but-complete code sample, so there's no telling what in your code might be stuck in a loop.
That said, it's certainly possible that the loop you posted for some reason turns out to be the problem. I admit, from the code you posted it's not clear why your loop works at all (when would "byteReadStream " ever _not_ be null?). But assuming there's some condition under which you're not exiting the loop, it wouldn't surprise me to find that you wind up consuming 100% CPU, because it's probably a scenario in which no progress towards exiting the loop is made.
You shouldn't be using the Available property anyway. It's not a reliable indicator of what you can read. But without a complete code sample, it's not really possible to offer more detailed advice as to how to fix your program.
Pete
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Park Yeon Jo |
last post by:
About Error : ORA-12514: TNS:listener could not resolve SERVICE_NAME
given in connect descriptor
I installed Oracle 8.1.7 on Windows XP Professional.
and I wanto connect to that server from another client computer using
Net 8 Configuration.
But congiguration cannot be proceeded because above error .
|
by: Cherrish Vaidiyan |
last post by:
Hello all,
A warm Xmas greetings to all. I have a small problem with starting up
the database. Here my strategy.
I have installed Oracle 9i R 2 on Red Hat 9. i created two database on
this system. And the two datasbe is working perfectly. Now i needed
another RH9 system to work on Oracle 9i. So I was guided to copy the
oracle home folder and that the oracle will be up for work. I even got
such a response from Google Group. I copied...
|
by: Cherrish Vaidiyan |
last post by:
sir,
I have a small error in Listener configuration.I have two system with
a database in each. I am using Red Hat 9 and Oracle 9i. so i shall
anme the database and system.
system 1 - node2 system 2 - node3
database - apple database - intel
i have installed Oracle on 'node3' by copying the files and then
creating a new database in it. Now i want to configure listener. I
|
by: Axel Dachtler |
last post by:
Hi,
I have a listener problem. The listener cannot read SERVICE_NAME in
TNS-Descriptor. The service-name I specified in Oracle Net Manager for
this database is testdb as well.
(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=axel-0560nntbn1)
(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=testdb)))
It is a local database so I tried testdb.localhost, but this didn't
|
by: UNIXNewBie |
last post by:
I've just installed Oracle Standard on an XP Platform for demo. The listener
was not installed.
I have installed Oracle many times on 2000 server and never seen an case
where the listener was not installed.
How can I install the listener? It does not see to appear as an option to
install usising the installation tool.
Thanks
| |
by: Bill |
last post by:
When vb6 Winsock.RemoteHost is set to "127.0.0.1", c# socket listener cannot hear connect request (my old vb6 winsock listener could hear it...).
Why doesn't this work, and is there a work around I can make on the C# side to hear the connect request?
-Bill
(don't reply by e-mail, the address is a fake)
______________________________
Steps to reproduce:
Start the C# Listener
|
by: Steve Teeples |
last post by:
I have been perplexed by how to best treat an event that spans different
classes.
For example, I have a form which a user inputs data. I want to broadcast
that data via an event to another class (seen globally) having a data
structure which saves that form data to disk.
Whenever the form updates the data I'd like to broadcast the information and
have it saved in my global data structure. The perplexing thing for me
though is the...
|
by: Blake |
last post by:
I am trying to make a trace listener textbox that behaves like the
output window in VS, in that it shows trace messages from all trace
sources.
I created my own listener to write to a textbox and added this listener
to the trace.listeners collection but it will recieve messages sent via
the normal trace class.
Messages from other Tracesource objects are not received unless i
explicitly add my listener to their collections.
|
by: mivey4 |
last post by:
Hi,
First off, I am aware that this is a very heavily documented error and I have done my homework for throughly researching probable causes before deciding to post my problem here. At this point, I believe another set of eyes on the issue is merited.
I am a MSSQL DBA and somewhat new to ORACLE; but I have read the administrators manual having a basic thorough level of knowledge (Tho' I am still learning) and understanding of how to...
|
by: michael ngong |
last post by:
michael.john@gmx.at (Michael John) wrote in message news:<90cc4edd.0306230900.28075193@posting.google.com>...
MIchael
I you stated the OS and platform that could make it easier to address your issue
Michael Tubuo Ngong
|
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: 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: 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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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: 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: 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.
|
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...
| |