Multithreading TcpListener c#

venerdì 03 luglio 2009 - 12.50

Argosk Profilo | Newbie

Ciao a tutti...

ho necessità di fare un server multithreading utilizzando la classe TcpListener. Ho trovato un esempio nella MSDN con il seguente codice:

using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; class MyTcpListener { public static void Main() { TcpListener server = null; try { // Setto la Porta i ascolto Int32 port = 9999; // Setto l'IP del Server String IpLocal = "127.0.0.1"; IPAddress localAddr = IPAddress.Parse(IpLocal); // TcpListener server = new TcpListener(port); server = new TcpListener(localAddr, port); // Start listening for client requests. server.Start(); // Buffer for reading data Byte[] bytes = new Byte[256]; String data = null; // Enter the listening loop. while (true) { Console.Write("Waiting for a connection... "); // Perform a blocking call to accept requests. // You could also user server.AcceptSocket() here. TcpClient client = server.AcceptTcpClient(); Console.WriteLine("Connected!"); data = null; // Get a stream object for reading and writing NetworkStream stream = client.GetStream(); int i; // Loop to receive all the data sent by the client. while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { // Translate data bytes to a ASCII string. data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); Console.WriteLine("Received: {0}", data); // Process the data sent by the client. data = data.ToUpper(); byte[] msg = System.Text.Encoding.ASCII.GetBytes(data); // Send back a response. stream.Write(msg, 0, msg.Length); Console.WriteLine("Sent: {0}", data); } // Shutdown and end connection client.Close(); } } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } finally { // Stop listening for new clients. server.Stop(); } Console.WriteLine("\nHit enter to continue..."); Console.Read(); } }

Funziona correttamente solo posso collegare un client alla volta...come lo si può trasformare ad accettare più client esempio 50 ?

Jeremy Profilo | Guru

Ciao Marco.
Te lo scrivo in Vb per mia comodità ... pensaci tu alla conversione.
private ClientList as list(of TcpClient) Do If TuoListener.Pending Then Client = Listener.AcceptTcpClient ClientList.add(Client) etc.... etc..... End If loop

Eventualmente ... rimango a disposizione per eventuali info ....
facci sapere...
Ciao

Argosk Profilo | Newbie

Ciao grazie mille ci provo e ti faccio sapere

HavanaMan Profilo | Junior Member

Ciao

Rispondo a questa discussione perchè ci sto provando anche io ma mi sono un po' perso:

il mio codice VB.NET è:

Il codice sorgente non è stato renderizzato qui
perchè non c'è sufficiente spazio.
Clicca qui per visualizzarlo in una nuova finestra

riesco a connettermi ma non mi notifica la connessione dei client (mi provo a connettere tramite telnet)

qualche idea?
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5