-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System.Text; | ||
|
||
class UDPReceiver | ||
{ | ||
private UdpClient listener; | ||
private IPEndPoint senderEndPoint; | ||
private int expectedSequenceNumber = 0; | ||
|
||
public UDPReceiver(int listenPort) | ||
{ | ||
listener = new UdpClient(listenPort); | ||
senderEndPoint = new IPEndPoint(IPAddress.Any, 0); | ||
} | ||
|
||
public void Receive() | ||
{ | ||
while (true) | ||
{ | ||
byte[] packet = ReceivePacket(); | ||
int receivedSequenceNumber = BitConverter.ToInt32(packet, 0); | ||
byte[] data = new byte[packet.Length - sizeof(int)]; | ||
Buffer.BlockCopy(packet, sizeof(int), data, 0, data.Length); | ||
|
||
if (receivedSequenceNumber == expectedSequenceNumber) | ||
{ | ||
SendAck(expectedSequenceNumber); | ||
Console.WriteLine("Received message: " + Encoding.UTF8.GetString(data)); | ||
expectedSequenceNumber++; | ||
} | ||
else if (receivedSequenceNumber < expectedSequenceNumber) | ||
{ | ||
SendAck(receivedSequenceNumber); // Acknowledge the duplicate | ||
} | ||
else | ||
{ | ||
// Out-of-order packet received, don't send ACK | ||
} | ||
} | ||
} | ||
|
||
private byte[] ReceivePacket() | ||
{ | ||
return listener.Receive(ref senderEndPoint); | ||
} | ||
|
||
private void SendAck(int ackSequenceNumber) | ||
{ | ||
byte[] ackData = BitConverter.GetBytes(ackSequenceNumber); | ||
listener.Send(ackData, ackData.Length, senderEndPoint); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System.Text; | ||
|
||
class UDPSender | ||
{ | ||
private UdpClient client; | ||
private IPEndPoint serverEndPoint; | ||
private int sequenceNumber = 0; | ||
|
||
public UDPSender(string serverIp, int serverPort) | ||
{ | ||
client = new UdpClient(); | ||
serverEndPoint = new IPEndPoint(IPAddress.Parse(serverIp), serverPort); | ||
} | ||
|
||
public void Send(string message) | ||
{ | ||
byte[] data = Encoding.UTF8.GetBytes(message); | ||
byte[] packet = CreatePacket(data); | ||
|
||
while (true) | ||
{ | ||
SendPacket(packet); | ||
|
||
// Wait for acknowledgment (ACK) | ||
byte[] ackData = ReceiveAck(); | ||
int ackSequenceNumber = BitConverter.ToInt32(ackData, 0); | ||
|
||
if (ackSequenceNumber == sequenceNumber) | ||
{ | ||
Console.WriteLine("Message sent and acknowledged: " + message); | ||
sequenceNumber++; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private byte[] CreatePacket(byte[] data) | ||
{ | ||
byte[] sequenceBytes = BitConverter.GetBytes(sequenceNumber); | ||
byte[] packet = new byte[sequenceBytes.Length + data.Length]; | ||
|
||
Buffer.BlockCopy(sequenceBytes, 0, packet, 0, sequenceBytes.Length); | ||
Buffer.BlockCopy(data, 0, packet, sequenceBytes.Length, data.Length); | ||
|
||
return packet; | ||
} | ||
|
||
private void SendPacket(byte[] packet) | ||
{ | ||
client.Send(packet, packet.Length, serverEndPoint); | ||
} | ||
|
||
private byte[] ReceiveAck() | ||
{ | ||
return client.Receive(ref serverEndPoint); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters