Re !
Bon bah, c'est cool j'ai terminé mon "apprentissage" des packets, je suis content c'est beaucoup plus simple que les sockets, alors le plus dur est fait ! :p (sans compter le bot que je dois faire :teeth:)
Screens :
Loading Image
Le code :
Bref, ducoup je partage le code le plus simple possible que j'ai pus faire pour recevoir / envoyer autant de packets que l'on veux ou on veux (J-K, abstient toi de dDos tes copains d'accord ? :)), et j'ai commenté vite fait le code mais bon c'est ultra basique donc y'a pas besoin de beaucoup de commentaires (ce partage s'adresse aux débutant qui souhaitent apprendre).
Receiver:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace UDPReceiver
{
internal class Program
{
private static void Main(string[] args)
{
// Définition des paramètres principaux :
Console.Title = "UDP Receiver";
IPHostEntry ihe = Dns.Resolve(Dns.GetHostName());
IPAddress ip = ihe.AddressList[0];
int port = 80;
IPEndPoint iep = new IPEndPoint(ip, port);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(iep); // Connexion.
IPEndPoint sender = new IPEndPoint(ip, port);
EndPoint ep = (EndPoint)iep;
byte[] buffer = new byte[1024]; // #1024BytesForEver! :p
int receivedData;
string data;
// Réception :
new Thread(() =>
{
while (true)
{
receivedData = socket.ReceiveFrom(buffer, ref ep);
data = Encoding.ASCII.GetString(buffer, 0, receivedData);
Console.WriteLine("Received from {0} : {1}", ep.ToString(), data);
}
}).Start();
Console.ReadKey(); // Je sais que ça ne sert à rien mais ça me stress sinon :'(
}
}
}
Sender :
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace UDPSender
{
internal class Program
{
private static void Main(string[] args)
{
// Définition des paramètres principaux :
Console.Title = "UDP Sender";
IPHostEntry ihe = Dns.Resolve(Dns.GetHostName());
IPAddress ip = ihe.AddressList[0];
int port = 80;
IPEndPoint iep = new IPEndPoint(ip, port);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// Envoie :
new Thread(() =>
{
while (true)
{
Console.WriteLine("Send to {0} >>", iep.ToString());
string msg = Console.ReadLine();
byte[] buffer = new byte[Encoding.ASCII.GetBytes(msg).Length];
buffer = Encoding.ASCII.GetBytes(msg);
socket.SendTo(buffer, iep);
}
}).Start();
Console.ReadKey();
}
}
}
Voilà, J'espère que ça serviras à quelqu'un :)