Réseau

Simple Client/Serveur TCP

De Admin dans C#

Tags: Réseau

 5/27/2015

Voici les bases pour implémenter un client/serveur TCP en c# :

Le client :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
 
public class Client
{
    public static void Main(string[] args)
    {
        try
        {
            string adresseIp = "Votre IP";
            int port = 8001;
 
            TcpClient client = new TcpClient();
            Console.WriteLine("Connexion.....");
            client.Connect(adresseIp, port);
            Console.WriteLine("Connecté");
           
            string chaineAenvoyer = string.Empty;
            while (chaineAenvoyer.ToLower() != "quit")
            {
                Console.WriteLine("\nChaine a envoyer : ");
                chaineAenvoyer = Console.ReadLine();
 
                Stream stream = client.GetStream();
                ASCIIEncoding encoding = new ASCIIEncoding();
 
                byte[] byteAenvoyer = encoding.GetBytes(chaineAenvoyer);
                Console.WriteLine("Transmission.....");
                stream.Write(byteAenvoyer, 0, byteAenvoyer.Length);
                byte[] byteEnvoyes = new byte[100];
                int nbBytes = stream.Read(byteEnvoyes, 0, 100);
                for (int i =
...

Lire >>