Click here to Skip to main content
15,916,941 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# webbrowser control focus issues Pin
Shameel20-Nov-09 2:24
professionalShameel20-Nov-09 2:24 
GeneralRe: C# webbrowser control focus issues Pin
Wheels01220-Nov-09 2:33
Wheels01220-Nov-09 2:33 
Questionusing DirectorySearcher to get Group members Pin
caiena17-Nov-09 7:37
caiena17-Nov-09 7:37 
QuestionTCPListener/Many TCPClients -> Performance Issues? Pin
softwarejaeger17-Nov-09 6:04
softwarejaeger17-Nov-09 6:04 
AnswerRe: TCPListener/Many TCPClients -> Performance Issues? Pin
Paulo Zemek17-Nov-09 7:38
Paulo Zemek17-Nov-09 7:38 
GeneralRe: TCPListener/Many TCPClients -> Performance Issues? Pin
Matt Meyer17-Nov-09 10:06
Matt Meyer17-Nov-09 10:06 
GeneralRe: TCPListener/Many TCPClients -> Performance Issues? Pin
Paulo Zemek17-Nov-09 13:27
Paulo Zemek17-Nov-09 13:27 
GeneralRe: TCPListener/Many TCPClients -> Performance Issues? Pin
Matt Meyer18-Nov-09 4:43
Matt Meyer18-Nov-09 4:43 
Here's a simple server program demonstrating how the ports work. It'll accept 10 connections, hang onto each one for 30 seconds, then shut itself down. Telnet into port 2323 with a command prompt to connect. If you connect 3 clients, netstat will yield a similar output to this (client ports are random, so it's not exact):
Proto  Local Address           Foreign Address        State
TCP    workstation:1161        localhost:2323         ESTABLISHED
TCP    workstation:1162        localhost:2323         ESTABLISHED
TCP    workstation:1163        localhost:2323         ESTABLISHED
TCP    workstation:2323        localhost:1161         ESTABLISHED
TCP    workstation:2323        localhost:1162         ESTABLISHED
TCP    workstation:2323        localhost:1163         ESTABLISHED

The client's ports are randomly assigned to a number over 1024, but note that the server will always use port 2323 for its communication. When a client connects, the server does not create a new connection on a secondary port for communications.

However, some protocols do work like you have described above, randomly using an additional port for a secondary connection (IE, FTP does for data transfers, which is why FTP servers behind firewalls are a pain in the butt Smile | :) ).

Server Code -
[STAThread]
public static void Main(string[] Args) {
  TcpListener listener = null;
  try {
    listener = new TcpListener(IPAddress.Loopback, 2323);
    listener.Start();
    int connection_count = 0;
    while(connection_count<10) {
      if(listener.Pending()) {
        TcpClient connection = listener.AcceptTcpClient();
        ThreadPool.QueueUserWorkItem(new WaitCallback(Communicate), connection);
        Console.WriteLine("Connection Accepted ({0})", connection_count++);
      }
      Thread.Sleep(1);
    }
    Console.WriteLine("Waiting 30 seconds for last connection to close.");
    Thread.Sleep(30005);
    Console.WriteLine("Shutting Down.");
  } finally {
    if(listener!=null) {
      listener.Stop();
    }
  }
}

private static void Communicate(object Connection) {
  if(Connection==null) return;
  TcpClient client = (TcpClient)Connection;
  StreamWriter writer = null;
  try {
    if(client.Connected) {
      writer = new StreamWriter(client.GetStream());
      writer.WriteLine("Connected.  You will be disconnected in 30 seconds.");
      writer.Flush();
      Thread.Sleep(30000);
      writer.WriteLine("Bye!");
      writer.Flush();
    }
  } catch {
    if(writer!=null) {
      writer.WriteLine("Connection error.");
      writer.Flush();
    }
  } finally {
    Console.WriteLine("Closing Client.");
    if(client.Connected) {
      client.Close();
    }
  }
}

GeneralRe: TCPListener/Many TCPClients -&gt; Performance Issues? Pin
Paulo Zemek18-Nov-09 11:18
Paulo Zemek18-Nov-09 11:18 
GeneralRe: TCPListener/Many TCPClients -&gt; Performance Issues? Pin
Matt Meyer18-Nov-09 11:33
Matt Meyer18-Nov-09 11:33 
Questionexport to excel in C# windows application Pin
FEMDEV17-Nov-09 4:01
FEMDEV17-Nov-09 4:01 
AnswerCross Post Pin
dan!sh 17-Nov-09 4:15
professional dan!sh 17-Nov-09 4:15 
Generalexcel Pin
FEMDEV17-Nov-09 4:17
FEMDEV17-Nov-09 4:17 
GeneralRe: Cross Post Pin
Abhishek Sur17-Nov-09 4:21
professionalAbhishek Sur17-Nov-09 4:21 
GeneralEXCEL Pin
FEMDEV17-Nov-09 4:22
FEMDEV17-Nov-09 4:22 
GeneralExcel Formatting Pin
FEMDEV20-Nov-09 4:17
FEMDEV20-Nov-09 4:17 
GeneralRe: Excel Formatting Pin
Abhishek Sur21-Nov-09 8:49
professionalAbhishek Sur21-Nov-09 8:49 
QuestionExecuteReader requires an open and available Connection. The connection's current state is closed. Pin
Vimalsoft(Pty) Ltd17-Nov-09 3:54
professionalVimalsoft(Pty) Ltd17-Nov-09 3:54 
AnswerRe: ExecuteReader requires an open and available Connection. The connection's current state is closed. Pin
dan!sh 17-Nov-09 4:24
professional dan!sh 17-Nov-09 4:24 
GeneralRe: ExecuteReader requires an open and available Connection. The connection's current state is closed. Pin
Vimalsoft(Pty) Ltd17-Nov-09 4:35
professionalVimalsoft(Pty) Ltd17-Nov-09 4:35 
GeneralRe: ExecuteReader requires an open and available Connection. The connection's current state is closed. Pin
Abhijit Jana17-Nov-09 4:48
professionalAbhijit Jana17-Nov-09 4:48 
GeneralRe: ExecuteReader requires an open and available Connection. The connection's current state is closed. Pin
Vimalsoft(Pty) Ltd17-Nov-09 4:50
professionalVimalsoft(Pty) Ltd17-Nov-09 4:50 
GeneralRe: ExecuteReader requires an open and available Connection. The connection's current state is closed. Pin
Abhijit Jana17-Nov-09 5:58
professionalAbhijit Jana17-Nov-09 5:58 
GeneralRe: ExecuteReader requires an open and available Connection. The connection's current state is closed. Pin
dan!sh 17-Nov-09 5:58
professional dan!sh 17-Nov-09 5:58 
AnswerRe: ExecuteReader requires an open and available Connection. The connection's current state is closed. Pin
Dave Kreskowiak17-Nov-09 4:51
mveDave Kreskowiak17-Nov-09 4:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.