Question by
JaimeBGarcia · Dec 10, 2020 at 11:06 AM ·
serverclientudp
Successfully connected to Multicast UDP?
I have this code in the Client, but I don't know if I am well connected or joined to the multicast group. I have this Client code:
public void UDPConnect()
{
socket = new UdpClient(25504);
try
{
endPoint = new IPEndPoint(IPAddress.Parse(ip), 2600);
socket.JoinMulticastGroup(IPAddress.Parse(multicastIP));
socket.Connect(endPoint);
byte[] sendBytes = Encoding.ASCII.GetBytes("Hello, from the client");
socket.Send(sendBytes, sendBytes.Length);
ConnectToServer();
}
catch (Exception e)
{
print("Exception thrown " + e.Message);
}
}
public void ConnectToServer()
{
socket.BeginReceive(ReceiveCallback, null);
}
public void ReceiveCallback(IAsyncResult _result)
{
try
{
byte[] _data = socket.EndReceive(_result, ref endPoint);
socket.BeginReceive(ReceiveCallback, null);
if (_data.Length < 4)
{
Instance.Disconnect();
return;
}
HandleData(_data);
}
catch
{
Disconnect();
}
}
And this is my Server sending code, too simple. I just want to test if I can send to the Client and receive from the Client. As you can see, I have put the multicast IP that I use (231.1.1.10) in the Send method. When I send a message from the Client, this Server answers by sending the same message to the Client. My Client, in Unity, detects this message. It means that the multicast connection is successful?
//sending msg
server.send(msg,info.port,'231.1.1.10',function(error){
if(error){
client.close();
}else{
console.log('Data sent !!!');
}
});
});
Comment