I am trying to make a dedicated server for my new game using UDP sockets. I am using Socket.BeginSendTo()
and Socket.BeginReceiveFrom()
methods in order to send my data.
I am receiving the callbacks from both the methods, but when I am trying to Encoding.ASCII.GetString(buffer)
it is not showing me any strings. I am not sure if there is a problem while sending, or if there is a problem while receiving.
I know that UDP does not guarantee the transmission, that's why I tried to send my data more then twice more like 10 times. Each time, I receive a callback that the data is sent on the client side and the data is received on the server side. But, as I said, when I am trying to read the data using Encoding
, it shows nothing. I tried to do a string null or empty check while receiving the data. It said that the string is not empty, but when I try to Console.WriteLine()
, it showed nothing in the console.
Here is the client code, where I am sending the data to the server:
public void SendMessage(Activity activityCode, String inputedString) //I am changed this
{
byte[] data = readMessage.WriteData(activityCode, inputedString);
if(data != null)
{
Debug.Log(data.Length); //this returned 11
clientSocket.BeginSendTo(data, 0, data.Length, SocketFlags.None, serverEndpoint, new AsyncCallback(OnSend), null);
}
Array.Clear(readMessage.data, 0, readMessage.data.Length);
}
private void OnSend(IAsyncResult ar)
{
Debug.Log("sent"); //I am getting this callback in my console
}
Here is my server code, where I am receiving the data:
private void StartReceiveingData()
{
try
{
if (!serverSocket.IsBound)
{
serverSocket.Bind(localServerIPEndPoint);
}
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
//The epSender identifies the incoming clients
EndPoint epSender = (EndPoint)ipeSender;
serverSocket.BeginReceiveFrom(StaticTest.byteData, 0, StaticTest.byteData.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private void OnReceive(IAsyncResult ar)
{
StaticTest.printDData();
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
EndPoint epSender = (EndPoint)ipeSender;
serverSocket.EndReceiveFrom(ar, ref epSender);
StartReceiveingData();
}
And here is where I am reading the data:
static public class StaticTest
{
static public byte[] byteData = new byte[1024];
public static void printDData()
{
Console.WriteLine("Reding the received data" Encoding.UTF8.GetString(byteData));
}
}
UPDATE: I tried this code, but it still does not work:
private void StartReceiveingData()
{
try
{
if (!serverSocket.IsBound)
{
serverSocket.Bind(localServerIPEndPoint);
}
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
//The epSender identifies the incoming clients
EndPoint epSender = (EndPoint)ipeSender;
serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private void OnReceive(IAsyncResult ar)
{
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
EndPoint epSender = (EndPoint)ipeSender;
int numData = serverSocket.EndReceiveFrom(ar, ref epSender);
if (!clientList.ContainsKey(epSender))
{
Client client = new Client(epSender);
clientList.Add(epSender, client);
}
if(clientList.TryGetValue(epSender, out Client client1))
{
string _receivedData = Encoding.UTF8.GetString(byteData, 0, numData);
Console.WriteLine(_receivedData " Data that came");
//client1.RecieveDataFromMyGameObject(_receivedData);
}
StartReceiveingData();
}
And here is where I am reading the data:
public string TakeActions(string _recivedData)
{
int activityInput = Convert.ToInt32(_recivedData.Substring(0,1)); //there is an error
ActivityRequest activity = (ActivityRequest)activityInput;
Console.WriteLine("Current activity Number : " activity);
if(activity == ActivityRequest.SetupClient)
{
SetupClientDataActivity.setupClientData(client,_recivedData.Substring(1,_recivedData.Length-1));
}
else if(activity == ActivityRequest.MatchMaking)
{
Console.WriteLine("Asking Room manager to get into a room");
// roomManager.AddAClientToARoom(client);
}
return null;
}
CodePudding user response:
Your OnSend
callback needs to call Socket.EndSendTo()
to finish the send operation and tell you whether the operation was successful or not.
Your OnReceive
callback is calling Socket.EndReceiveFrom()
, but you are ignoring its result, and you are trying to print the byteData
before calling EndReceiveFrom()
to finish the read operation and tell you how many bytes were actually written into byteData
.
Try this instead:
public void SendMessage(Activity activityCode, String inputedString)
{
byte[] data = readMessage.WriteData(activityCode, inputedString);
if (data != null)
{
Debug.Log($"sending {data.Length} bytes");
try
{
clientSocket.BeginSendTo(data, 0, data.Length, SocketFlags.None, serverEndpoint, new AsyncCallback(OnSend), null);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Array.Clear(readMessage.data, 0, readMessage.data.Length);
}
private void OnSend(IAsyncResult ar)
{
try
{
int numBytes = clientSocket.EndSendTo(ar);
Debug.Log($"sent {numBytes} bytes");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private void StartReceiveingData()
{
try
{
if (!serverSocket.IsBound)
{
serverSocket.Bind(localServerIPEndPoint);
}
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
EndPoint epSender = (EndPoint)ipeSender;
serverSocket.BeginReceiveFrom(StaticTest.byteData, 0, StaticTest.byteData.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private void OnReceive(IAsyncResult ar)
{
try
{
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
EndPoint epSender = (EndPoint)ipeSender;
int numBytes = serverSocket.EndReceiveFrom(ar, ref epSender);
StaticTest.printDData(numBytes);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
StartReceiveingData();
}
static public class StaticTest
{
static public byte[] byteData = new byte[1024];
public static void printDData(int numBytes)
{
Console.WriteLine("Received data: {0}", Encoding.UTF8.GetString(byteData, 0, numBytes));
}
}