473,394 Members | 2,020 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,394 software developers and data experts.

TcpListener how to abort?

hi, i have a simple question; Suppose that followig code is a thread;

/************************************************** */
listener = new TcpListener( IP, PORT);

listener.Start();
while(running) {

sock = listener.AcceptSocket(); // this is blocking... suppose no client
connects

/*bla bla bla*/

}

/************************************************** */

How can i release(dipose, free) the listener socket. i have tried
"listener.Stop()" , aborting thread etc....

i just want to break it even by creating exception or by normal way(i don
kno how).

Best regards...
Nov 17 '05 #1
2 16988
Hi Coder, (<-- would use your full name)

call the stop method to close the TcpListener to stop listening (and free
resources). The Stop method does not close any accepted connections. You are
responsible for closing these separately. You can do that by calling the
Close on the TcpClient.

<code>
listener.Start();

while(running)
{

TcpClient client = listener.AcceptTcpClient();
// do some work with the TcpClient here

client.Close();
}

listener.Stop();
</code>

Cheers,
Neno

--
Neno Loje [MVP C#]
http://www.dotnet-online.com
"Coder" <oa***@st.cs.deu.edu.tr> schrieb im Newsbeitrag
news:e%****************@tk2msftngp13.phx.gbl...
hi, i have a simple question; Suppose that followig code is a thread;

/************************************************** */
listener = new TcpListener( IP, PORT);

listener.Start();
while(running) {

sock = listener.AcceptSocket(); // this is blocking... suppose no client
connects

/*bla bla bla*/

}

/************************************************** */

How can i release(dipose, free) the listener socket. i have tried
"listener.Stop()" , aborting thread etc....

i just want to break it even by creating exception or by normal way(i don
kno how).

Best regards...

Nov 17 '05 #2
> <code>
listener.Start();

while(running)
{

TcpClient client = listener.AcceptTcpClient();
// do some work with the TcpClient here

client.Close();
}
//-----suppose that above code is running in a thread

i use listener.Stop();

while it is still in listenning mode. I closed all the accepted sockets. But
altough i stop listener (listener.Stop() ) ,thread still hangs... (i see the
processes running on the tast manager).

1 year ago i wrote an mutlithreaded application like this one, may now im
forgetting something.

10x for the answer, Cheers..
Nov 17 '05 #3

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

Similar topics

1
by: zZ | last post by:
Hi All, I'm building a TCPListener as the server to accept TCPClient connections, using TCPListener.AcceptTcpClient. I'd like to retrieve client's IP after connected, but found no way to do...
2
by: Keith Langer | last post by:
I have a TCPListener which needs to listen on my LAN IP as well as the loopback address. In VS.Net 2002 the TCPListener constructor could accept the port as an argument and it automatically bound...
1
by: Doug Wyatt | last post by:
So I'll preface this with the fact that I'm a UNIX developer by training and have just recently gotten in to C# development on Windows. I'm basically running in to a problem whereby I suspect...
4
by: Rob White | last post by:
OK, so I have a TcpListener that is waiting for sockets, this piece of code: IPAddress localAddress = Dns.GetHostByName(Dns.GetHostName()).AddressList; IPEndPoint localEP = new...
2
by: Dave Coate | last post by:
Hi, I am working on a client-server app. I can get two applications to talk to each other on the same machine using 127.0.0.1, but as soon as I try it using a computer name or actual IP address...
3
by: Bjørn Eliasen | last post by:
Hi, I have an application running on all pc's in our company. Basically it is a TCPListener awaiting for sockets to connect and on connection performs the required tasks. The app works fine, but...
4
by: Thos | last post by:
I have a TcpListener that gets started from a GUI button event handler: TcpListener listener; listener = new TcpListener(IPAddress.Any, 1234); listener.Start(backLog); As soon as the third...
1
by: Darwin | last post by:
Setting a server to listen on 8080 for incoming connections. Written in VS2005 on a Multi-homed machine. I get the warning: Warning 1 'System.Net.Sockets.TcpListener.TcpListener(int)' is obsolete:...
1
by: secutos | last post by:
When a TcpListener accepts a connection, it returns a TcpClient, which allows you to recieve data. But what about TcpListener.Server.Receive? Does that also allow to receive data in the same way? Or...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.