Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

C# Programmation Réseau: Accepter une demande de connexion en attente

 
 
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
 
public class TcpListenerSample
{
    static void Main(string[] args)
    {
            TcpListener server = new TcpListener(IPAddress.Any13000);
            server.Start();
            while (true)
            {
                byte[] bytes = new byte[1024];
                string data;
 
                TcpClient client = server.AcceptTcpClient();
                NetworkStream stream = client.GetStream();
                int i = stream.Read(bytes, 0, bytes.Length);
                while (!= 0)
                {
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    Console.WriteLine(String.Format("Received: {0}", data));
                    data = data.ToUpper();
                    byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
                    stream.Write(msg, 0, msg.Length);
                    Console.WriteLine(String.Format("Sent: {0}", data));
                    i = stream.Read(bytes, 0, bytes.Length);
                }
                client.Close();
            }
 
            Console.WriteLine("Hit enter to continue...");
            Console.Read();
    }
}
 
   
  

No comments yet.

Leave a Comment