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

Using TCP Listener In C#

I am working to write a client server program, that relies on threads.
because of the way the program works the clients that connect to the server connect synchronously, but when I try to reopen the TCP Listener after using the close command on the server. Is there a way to force the port to empty when i close it, or a different way of calling it i guess. I have tried all I think, if I'm missing something obvious id be glad of the help (id rather not have to use asynchronous sockets if i don't have to.)

Expand|Select|Wrap|Line Numbers
  1. class ThreadedTcpSrvr
  2.     {
  3.         private TcpListener client = new TcpListener(9050);
  4.  
  5.         public ThreadedTcpSrvr(string yesno)
  6.         {
  7.             client.Start(10);
  8.  
  9.             if (yesno == "no")
  10.             {
  11.                 Console.WriteLine("Waiting for clients...");
  12.  
  13.                 while (!client.Pending())
  14.                 {
  15.                     Thread.Sleep(1000);
  16.                 }
  17.  
  18.                 ConnectionThread newconnection = new ConnectionThread();
  19.                 newconnection.threadListener = this.client;
  20.                 Thread newthread = new Thread(new ThreadStart(newconnection.HandleConnection));
  21.                 newthread.Start();
  22.  
  23.             }
  24.                 else if (yesno == "yes")
  25.                 {
  26.                     Console.WriteLine("thread aborted");
  27.                     client.Stop();
  28.                 }
  29.         }
  30.  
  31.         public static void StartServer()
  32.         {
  33.             ConnectionThread.yesno = "no";
  34.             ThreadedTcpSrvr server = new ThreadedTcpSrvr(ConnectionThread.yesno);
  35.         }
  36.         public static void Main()
  37.        {
  38.             Console.WriteLine("Hi press enter to continue");
  39.             Console.ReadLine();
  40.             StartServer();
  41.         }
  42.     }
  43.  
Mar 27 '08 #1
1 1617
Ok my own bad, the close never got called so it never closed :)
Mar 28 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
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. ...
6
by: Erik Steffl | last post by:
I am trying to create a simple custom web spider using mozilla and javascript, the basic functionality is to open a website and then manipulate it using DOM (possibly opening links etc.). it...
0
by: bharat | last post by:
Hi, I want to connect to SQL Server 2000 at remote site in python using SQLRelay at local machine. When i run my query, the following message comes: Debugging...
6
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...
0
by: Kamilche | last post by:
''' event.py An event manager using publish/subscribe, and weakrefs. Any function can publish any event without registering it first, and any object can register interest in any event, even...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...

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.