I'm attempting to setup UDP for a multiplayer game I'm developing but I've been having lots of issue trying to get the UDP packets to send over to my clients as it seems my UDP listener (UDPClient) isn't setup correctly but I have no idea why. This is for an assignment and they didn't give us much help on it so I'm a little clueless to be honest which is likely why it isn't working. I'll go over what I've got currently.
Currently I've got it setup where my clients will have their own UDPSocket and UdpEndPoint(IPEndPoint). I initialize the Socket when creating the Client object, and then create the endpoint and connect the socket to it when the user connects to the server. The IP and Port numbers are 127.0.0.1 and 4444 respectively
public Client()
{
tcpClient = new TcpClient();
formatter = new BinaryFormatter();
udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}
public bool Connect(string ipAddress, int port)
{
try
{
tcpClient.Connect(ipAddress, port);
stream = tcpClient.GetStream();
IPAddress IP = IPAddress.Parse(ipAddress);
udpEndPoint = new IPEndPoint(IP, port);
udpSocket.Connect(udpEndPoint);
writer = new BinaryWriter(stream, Encoding.UTF8);
reader = new BinaryReader(stream, Encoding.UTF8);
}
catch
{
return false;
}
I then send a TCP packet to the server containing the endpoint of the client. Server side has it's own client objects which also have an endpoint so when the server receives the packets, I assign the endpoint to the corresponding client object on the server side.
case PacketType.CONNECTED:
ConnectedPacket connectedPacket = (ConnectedPacket)packet;
Console.WriteLine("Player " + connectedPacket.ID + " Has Connected!");
client.ID = connectedPacket.ID;
client.AttachUDPEndPoint(connectedPacket.udpEndPoint);
The Server has a UDPCLient which I set up when I create the server, along with connecting it to the same IP and Port I use for my TCP connection. Those being 127.0.0.1 for the IP and 4444 for the port.
try
{
IPAddress IP = IPAddress.Parse(ipAddress);
tcpListener = new TcpListener(IP, port);
udpListener = new UdpClient();
udpListener.Connect(IP, port);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Whenever I want to send a packet to a client, I just call the corresponding client's (on the server side) "SendUDP" function which uses a passed in UDPClient from the Server, the same UDPClient which is set in the code above (udpListener). I just pass that listener from the server whenever I call the function.
public void SendUDP(UdpClient listener, Packet packet)
{
MemoryStream memoryStream = new MemoryStream();
formatter.Serialize(memoryStream, packet);
Byte[] buffer = memoryStream.GetBuffer();
listener.Send(buffer, buffer.Length, udpEndPoint);
}
player2.SendUDP(udpListener, positionPacket);
Sadly whenever I sent a UDP packet and the client attempts to recieve it, I just get given an error like this:
I get this error on every client and it's coming from a while loop which I use to check for incoming packets:
int noOfIncomingBytes;
byte[] bytes = new byte[256];
while (!disconnected && (noOfIncomingBytes = udpSocket.Receive(bytes)) != 0)
{
bytes = reader.ReadBytes(noOfIncomingBytes);
MemoryStream memoryStream = new MemoryStream(bytes);
Packet packet = formatter.Deserialize(memoryStream) as Packet;
Console.WriteLine("Client UDP Packet Recieved");
switch (packet.type)
{
case PacketType.POSITIONCHANGE:
PlayerPositionPacket postionPacket = (PlayerPositionPacket)packet;
if (postionPacket.playerNumber == 1)
{
Vector2 newPos = new Vector2(postionPacket.newPosX, postionPacket.newPosY);
gameInfo.player1Pos = newPos;
}
else if (postionPacket.playerNumber == 2)
{
Vector2 newPos = new Vector2(postionPacket.newPosX, postionPacket.newPosY);
gameInfo.player2Pos = newPos;
}
break;
}
}
I know the socket can't be set up "that incorrectly" as I'm able to send packets to the server fine, just receiving them is the issue as it seems the listener on the server side isn't connected? When debugging, it's "active" variable is false I'm assuming is bad.
There is likely a obvious issue in my code somewhere though I'm not sure where (This is my first time ever doing networking). I hope that's enough info to go off of, I'd really appreciate some help.
EDIT: My UDP socket seems to be connected fine when I create my client but as soon as my while loop (The while loop I posted above) that checks to see if it's received any packets runs, it disconnects.