- Home /
UdpClient EndRecieve recieves a list of null bytes
I'm writing a network manager for communicating between my server and client, but I'm receiving an odd issue when I attempted to start receiving asynchronously. That is, when I call EndRecieve on my UdpClient, 70-99.99% of the byte[] i get back are the correct length, but full of completely null bytes. Occasionally I will receive a proper message back from the server, but it is seemingly at random. I have triple checked that the server is sending the bytes properly, and have determined that it is indeed the client that is having the issue.
Here is my NetworkManager class as it is now:
using System;
using System.Net;
using System.Net.Sockets;
using Messaging;
namespace Network
{
public abstract class NetworkManager : IDisposable
{
private IPEndPoint serverEndpoint;
private static UdpClient clientUdp;
public NetworkManager(string serverIP, int serverPort)
{
clientUdp = new UdpClient();
clientUdp.Client.ReceiveTimeout = 5000;
serverEndpoint = new IPEndPoint(IPAddress.Parse(serverIP), serverPort);
StartRecieve();
}
public void StartRecieve()
{
clientUdp.BeginReceive(new AsyncCallback(OnUDPRecieve), clientUdp);
}
protected void OnUDPRecieve(IAsyncResult result)
{
UdpClient socket = result.AsyncState as UdpClient;
IPEndPoint source = new IPEndPoint(0, 0);
byte[] info = socket.EndReceive(result, ref source);
ManageResults(info);
socket.BeginReceive(new AsyncCallback(OnUDPRecieve), socket);
}
protected abstract void ManageResults(byte[] info);
public void SendUDP(Message m)
{
byte[] bytes = m.GetBytes();
clientUdp.Send(bytes, bytes.Length, serverEndpoint);
}
public virtual void Dispose()
{
if (clientUdp != null) clientUdp.Close();
}
}
}
And here is what I will generally get on my log output:
The first line in the log is the null result I'm talking about (The first value is the x value extracted, the second is the hex of the byte[] and the final value is the username, which is empty)
The other lines are the occasional time that I receive actual information.
I have absolutely no idea why this is happening. I've tried putting the receiving on a dedicated thread, but that resulted in not receiving any bytes at all. I read in a comment deep in a thread somewhere that it might be a bug with Mono, but even if that is what is is, I'm not sure what else I would do in this situation.
Does anybody have a solution?
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Does Unity Support TCP or only UDP? 0 Answers
UDP Server Or Master Server 1 Answer
Asynchronous Game server 2 Answers
What properties from a HLAPI connection can be used to create a LLAPI connection? 0 Answers