J'ai le code suivant (snips fourni ci-dessous) dans ma classe de base pour gérer un socket UDP. J'ajoute le support pour WinRT (contrôlé par le #define pour WINRT). J'ai tout pour travailler sur .Net, mais quelque chose ne va pas avec mon implémentation WinRT. Quand j'envoie les données, elles ne sont pas reçues côté serveur. Une idée de ce qui ne va pas? Je ne vois aucune erreur côté client, rien ne s'affiche sur le serveur. J'ai réussi à faire fonctionner ma classe de socket TCP, en utilisant le DataWriter d'une manière similaire.Utilisation correcte de UDP DatagramSocket
public UdpSocketClient(IPEndPoint remoteEndPoint, int localPort)
{
this.remoteEndPoint = remoteEndPoint;
#if WINRT
this.socket = new DatagramSocket();
this.socket.MessageReceived += ReceiveCallback;
// Bind to any port
Logger.Debug("{0}: UdpSocketClient created. Binding to port {1}.", this, (localPort == 0) ? "[any]" : localPort.ToString());
IAsyncAction bindAction = this.socket.BindEndpointAsync(new HostName("localhost"), localPort == 0 ? "0" : localPort.ToString());
bindAction.AsTask().Wait();
Logger.Trace("{0}: Bind Complete to port {1}", this, this.socket.Information.LocalServiceName);
// Get IOutputStream
Logger.Trace("{0}: Getting output stream to {1}.", this, remoteEndPoint);
IAsyncOperation<IOutputStream> asyncOutput = this.socket.GetOutputStreamAsync(remoteEndPoint.address, remoteEndPoint.port.ToString());
asyncOutput.AsTask().Wait();
Logger.Trace("{0}: Got output stream.", this);
// Create DataWriter
dataWriter = new DataWriter(asyncOutput.GetResults());
#else
...
#endif
}
public void SendBuffer(ByteBuffer buffer, int wait = 0)
{
#if WINRT
Logger.Trace("{0}: Sending buffer. Wait = {1}", this, wait);
for (int i = 0; i < buffer.WriteSize(); i++)
dataWriter.WriteByte(buffer.Buffer()[i]);
DataWriterStoreOperation op = dataWriter.StoreAsync();
if (wait != 0) op.AsTask().Wait(wait);
else op.AsTask().Wait();
Logger.Trace("{0}: Sending complete.", this);
#else
...
#endif
}
Quelques journaux pertinents:
04/23 19:08:57.504 DEBUG: Area Sync: UdpSocketClient created. Binding to port [any].
04/23 19:08:57.505 TRACE: Area Sync: Bind Complete to port 59518
04/23 19:08:57.506 TRACE: Area Sync: Getting output stream to 71.227.179.128:1302.
04/23 19:08:57.507 TRACE: Area Sync: Got output stream.
04/23 19:08:57.604 TRACE: Area Sync: Sending contact packet.
04/23 19:08:57.604 TRACE: Area Sync: Sending buffer. Wait = 0
04/23 19:08:57.605 TRACE: Area Sync: Sending complete.
Commencez par utiliser un renifleur de paquets pour voir si le client envoie réellement les paquets sur le réseau. –
Je suis à peu près sûr qu'il n'envoie pas de paquets. Je cours le même code .Net et cela fonctionne. –