Hi
To accomplish this you need to set the socket receive buffer to 0
before suspending reception of incoming packets. You can use the
SetSocketOption() method to force the receive buffer to 0:
sock.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveBuffer, 0);
I have already tried that yesterday (I was going through the entire
system.net.socket namespace to try and find a solution), but it didn't work
as it should. I've set the ReceiveBuffer to zero, and I've verified that the
buffer size has indeed by set to zero using
sock.GetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveBuffer), however despite it being zero, when I send
packets while the receiver is inactive, once I activate it again, I still
have 13 of the packets I sent during the inactive period. In fact, if I set
the buffer to zero and leave it like that during the whole program run, I
still receive incoming packets even when the receiver is active. I do no
longer get all of them, but shouldn't I get none if I have a buffer size
that doesn't even allow to store one single of my 750 byte packets (722 byte
payload plus IP and UDP headers)?
Here's the code snippet I'm using:
UdpClient receiver;
IPEndPoint remoteSender;
Byte[] packet;
public Thread thread;
bool listening;
//PacketHandler handler;
Timer tim;
TimerState s;
public int nbPackets;
int seqNo;
Socket sock;
public Receiver(int localPort) : base()
{
receiver = new UdpClient(localPort);
remoteSender = new IPEndPoint(IPAddress.Any,0);
sock = this.Client;
sock.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
int receiveBuffer = (int)sock.GetSocketOption(SocketOptionLevel.Socket ,
SocketOptionName.ReceiveBuffer);
sock.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveBuffer, 0);
int sent = receiver.Send(new byte[2], 2, "192.168.1.255", 14888);
packet = receiver.Receive(ref remoteSender);
//Object o = sock.GetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.PacketInformation);
//this.handler = handler;
listening = false;
nbPackets = 0;
s = new TimerState();
TimerCallback timerDelegate = new TimerCallback(this.timeToSleep);
tim = new Timer(timerDelegate, null, Timeout.Infinite, Timeout.Infinite);
//
ThreadStart startMethod = new ThreadStart(this.run);
thread = new Thread(startMethod);
thread.Start();
receiveBuffer = (int)sock.GetSocketOption(SocketOptionLevel.Socket ,
SocketOptionName.ReceiveBuffer);
thread.Suspend();
}
/**
* method that is running when the thread is active
* receives a packet from the network and processes it
*/
public void run()
{
while (true)
{
packet = receiver.Receive(ref remoteSender);
nbPackets++;
seqNo = BitConverter.ToInt16(packet, 0);
Console.Write(nbPackets.ToString() + ":" + seqNo.ToString() + " / " );
//o.ToString();
// spit out packet for processing handler.processPacket(packet);
}
}
I have a main program that suspends and resumes the thread every 10 seconds.
The timer will shut down the receiver after 5 seconds if nbPackets has not
been received (in other words: when there has been no new packet). I use the
same code to send packets like last time (maybe you remember, I sent you the
code because I was having a problem with UdpClient not receiving all packets
on my machine at work.. ).
Stephan