I have a C# application that receives UDP data from a remote host. I find that sometimes my socket receives nothing and I cant seem to find any clues as to why!
Wireshark tells me my data is being sent from the remote device correctly. I cant seem to get why sometimes I can receive fine, and sometimes I can't.
I don't get any exceptions, but OnRecieve never gets called
Here is my code in case it helps:
class cUdpRx
{
private Thread rxThread = null;
private Socket UdpSocket;
private IPEndPoint localEp;
byte[] byData;
//rxbytes event
public delegate void OnRxBytesEventHandler(byte[] rxBuf);
public event OnRxBytesEventHandler OnRxBytesEvent;
/// <summary>
/// Creates the udp socket
/// </summary>
/// <param name="Port"></param>
/// <returns></returns>
public bool CreateSocket(int Port)
{
try
{
byData = new byte[1500]; //create our buffer
UdpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
UdpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
localEp = new IPEndPoint(IPAddress.Any,Port);
UdpSocket.Bind(localEp);
UdpSocket.BeginReceive(byData,0,byData.Length, SocketFlags.None, new AsyncCallback(OnRecieve), UdpSocket);
return true; //seemed to work ok
}
catch
{
Dispose();
return false; //something went wrong, abort
}
}
private void OnRecieve(IAsyncResult iar)
{
byte[] rxData;
int nBytesRec = 0;
Socket socket = (Socket)iar.AsyncState;
try //in case something else has already disposed of the socket
{
nBytesRec = socket.EndReceive(iar);
}
catch
{
Debug.WriteLine("cant access udp rx socket");
}
try
{
if (nBytesRec > 0)
{
rxData = new byte[nBytesRec];
Array.Copy(byData, rxData, nBytesRec); //copy exact data into new array
OnRxBytesEvent(rxData);
}
if(!killThreads)
UdpSocket.BeginReceive(byData, 0, byData.Length, SocketFlags.None, new AsyncCallback(OnRecieve), UdpSocket);
}
catch(SocketException se)
{
Debug.WriteLine(se.ToString());
}
}
Any help would be really appreciated as its stopping me from going forward with my project. Thanks
UPDATE
It seems like using IPAdress.any is the problem. If I change:
localEp = new IPEndPoint(IPAddress.Any,Port);
to
localEp = new IPEndPoint(IPAddress.Parse("192.168.0.33"),Port);
where 192.168.0.33 is the IP address of my computer, it receives data every time.
Can anyone explain why?
It is quite useful to use IPAddress.any and receive from both wireless and wired connections.
IPAddress.any is equivalent to 0.0.0.0 and according to MSDN it should receive on all network interfaces.
The usual cause of such problems is not processing packets fast enough. They fill up socket receive buffer and kernel starts dropping them.
Increase socket receive buffer size to accommodate traffic bursts.
Remove everything non-essential from the fast socket reading path.
Consider simple iterative approach bellow instead of working against thread pool:
rxData = new byte[nBytesRec];
while ( !time_to_stop ) {
int len = socket.Receive( rxData );
OnRxBytesEvent( rxData, len );
}
Your code comments mention multicast. Note that you need to add explicit group membership for that, and the fact that you join a multicast group on an interface (either explicitly or as decided via routing table), so if you want to listen for multicast data on two interfaces you need two sockets.
I just got the same problem and resolve it by set timeTolive to big data.
_client = new UdpClient();
_client.Client.Bind(new IPEndPoint(IPAddress.Any, targetURI.Port));
_client.JoinMulticastGroup(IPAddress.Parse(targetURI.Host) , 250);
timeToLive = 250, it works.
timeToLive = 50 , works or not work alternate happened.
timeToLive = none or 0, not work.
Related
I have an embedded Ethernet interface (Lantronix XPort) that responds to a UDP broadcast with its identifying information.
I am able to multicast the "magic packet" and datagrams are received by the listener correctly, however I also need to find out what IP Address send that response datagram. If it were TCP, I would do socket.RemoteEndPoint, but that throws an exception when applied to a UDP socket.
public class Program
{
public static void Main(string[] args)
{
// magic packet
byte[] magicPacket = new byte[4] { 0, 0, 0, 0xf6 };
// set up listener for response
Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// EDIT: Also, had to add this to for it to broadcast correctly
sendSocket.EnableBroadcast = true;
IPEndPoint listen_ep = new IPEndPoint(IPAddress.Any, 0);
sendSocket.Bind(listen_ep);
// set up broadcast message
EndPoint send_ep = new IPEndPoint(IPAddress.Parse("192.168.255.255"), 30718);
sendSocket.SendTo(magicPacket, magicPacket.Length, SocketFlags.None, send_ep);
DateTime dtStart = DateTime.Now;
while (true)
{
if (sendSocket.Available > 0)
{
byte[] data = new byte[2048];
// throws SocketException
//IPEndPoint ip = sendSocket.RemoteEndPoint as IPEndPoint;
sendSocket.Receive(data, SocketFlags.None);
if (data.Length > 4)
{
PrintDevice(data);
}
}
if (DateTime.Now > dtStart.AddSeconds(5))
{
break;
}
Console.WriteLine(".");
Thread.Sleep(100);
}
// wait for keypress to quit
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
Any thoughts? Is there a better strategy to reading the response datagrams that would let me ascertain the Remote IP Address?
EDIT:
As is typical, the minute I post on SO, a moment of clarity hits me.
Turns out I can just do this:
EndPoint remote_ep = new IPEndPoint(IPAddress.Any, 0);
// Use ReceiveFrom instead of Receieve
//sendSocket.Receive(data, SocketFlags.None);
sendSocket.ReceiveFrom(data, ref remote_ep);
And remote_ep now contains the remote endpoint information!
Take a look at ReceiveFrom instead of Receive, it will let you pass in a reference to an Endpoint.
What about Asynchronous socket?I didn't find any way to get the remote IP address. (Asynchronous method ReceiveFromAsync is my only option in wp8)
EndPoint remote_ep = new IPEndPoint(IPAddress.Any, 0); // Use ReceiveFrom instead of
sendSocket.Receive(data, SocketFlags.None);
sendSocket.ReceiveFrom(data, ref remote_ep);
i think it works for IP but it fails for port number
if you would notice
try chaging 0 to something else like 6530 4expl
it system will would generate it's random port number
Any ideas to why is it ?
P.S. Any ideas how can i change my user name here .... ?
FOUND IT : the abstract class only needed for representation of port it is in value not out
since there's no bind done before hand this operation ref EndPoint needed to represent the sender. Meaning that it is there to show senders port and IP not to specify from where to get the communication. And EndPoint instantiation is really just a formality seems like it is overriden by system with senders address anyway. I think it has to do somethign with the way UDP protocol works.
But all in all the ref EndPoint is there only shows where u got the packet from and only it does not specify where u want u'r commuicatino to be from.
I've made a simple chat application in c#.net that sends and receives data between 2 computers.
So, I used this method to send the data:
int port = 11000;
private void send(string data, string ip)
{
Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint sending_end_point = null;
byte[] send_buffer = Encoding.ASCII.GetBytes(data);
sending_end_point = new IPEndPoint(IPAddress.Parse(ip), port);
try { sending_socket.SendTo(send_buffer, sending_end_point); }
catch { }
}
And to receive I used this:
string receiveddata = "";
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
UdpClient listener = new UdpClient(port);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, port);
byte[] receive_byte_array;
try
{
receive_byte_array = listener.Receive(ref groupEP);
receiveddata = Encoding.ASCII.GetString(receive_byte_array, 0, receive_byte_array.Length);
}
catch { }
listener.Close();
}
This works without any problems between 2 computers on a LAN, but I would like to know (if possible) how to do the same thing over the Internet.
From what I've searched on the Internet, it seems that I have to use port-forwarding in order to do that, so I already did that, but I don't know what should I do know.
So my question is, how should I change this code (if I have to) so I could send and receive data (UDP) over the internet, assuming I have port-forwarded correctly already and assuming I know the external IPs of both routers?
Thank you in advance.
This should work just fine, as long as your (public) IP-address is correct and the ports are forwarded correctly on your router (meaning, forwarded to the correct private IP, on the correct protocol, in your case UDP).
You are aware that this is UDP though, so it's not reliable data transfer.
I have a .NET Socket that listens to all TCP requests on the computer, and aggregates them into HTTP requests (where possible).
I have the following problem -
When I access a site (for example - stackoverflow.com) I see in WireShark that there are X (lets say - 12) TCP packets received from the site's host.
But in my code the Socket just stops receiving the messages before the end (after 10 messages)
I have no idea how to fix this, I hope it's something that is limiting the socket in his definition
Here is my code:
public void StartCapturing()
{
try
{
_chosenOutgoingAddress = UserChoosesIpCtrl();
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw,
ProtocolType.IP);
_socket.Bind(new IPEndPoint(_chosenOutgoingAddress, 0));
_socket.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.HeaderIncluded, true);
_socket.IOControl(IOControlCode.ReceiveAll, _bIn, _bOut);
thrStartCapturing = new Thread(StartReceiving);
thrStartCapturing.Name = "Capture Thread";
thrStartCapturing.Start();
}
catch (Exception ex)
{
//TODO: general exception handler
throw ex;
}
}
The StartCapturing method will initiate the Socket and start the receiving thread with the StartReceiving method (as below0
private void StartReceiving()
{
while (!_stopCapturing)
{
int size = _socket.ReceiveBufferSize;
int bytesReceived = _socket.Receive(_bBuffer,
0,
_bBuffer.Length,
SocketFlags.None);
if (bytesReceived > 0)
{
_decPackagesReceived++;
ConvertReceivedData(_bBuffer, bytesReceived);
}
Array.Clear(_bBuffer, 0, _bBuffer.Length);
}
}
What am I doing wrong?
Ok, I figured it out, so I'm posting here for anyone else who might need it in the future
The .NET Socket class has a property ReceiveBufferSize which determines what is the buffer that the Socket will allow.
My problem was that my code wasn't ASync, or fast enough to clean this buffer, so that the last TCP packets had no more buffer and were ignored.
Increasing the ReceiveBufferSize or make my code ASync (probably better :-)) will fix this.
I'm opening a UDP Socket for receiving udp packets. However sometimes It never gets to the point Do stuff with data.
Data is being received, I can see it on Wireshark:
but the callback only runs to close the socket when I run the Disconnect code.
private void OpenUDPSocket()
{
this.processDataSockets.Clear();
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
UPDData data = new UPDData();
data.Socket = new Socket(ip.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
data.Socket.Bind(new IPEndPoint(ip, 2222));
data.Socket.EnableBroadcast = true;
data.Buffer = new byte[512];
data.Socket.BeginReceive(data.Buffer, 0, 512, SocketFlags.None, this.ReceivedData, data);
this.processDataSockets.Add(data);
}
this.socketOpen = true;
}
private void ReceivedData(IAsyncResult ar)
{
UPDData data;
try
{
data = (UPDData)ar.AsyncState;
data.Socket.EndReceive(ar);
}
catch (ObjectDisposedException)
{
// The connection has been closed
return;
}
//... Do stuff with data
data.Socket.BeginReceive(data.Buffer, 0, 512, SocketFlags.None, this.ReceivedData, data);
}
When this happens I'm left stuck, restarting the application doesn't help. I need to reboot my machine for the callback to start working again.
I have no idea where to go from here or how to fix this.
Any Ideas what's happening?
In the end we could not resolve this issue. It was not limited to Asynchronous IO or completion ports. Finally we took the work around step of using PcapDotNet to pick up the packets directly, which worked.
Platform: Windows 2003 R2, C#
I have an application that sends UDP messages to other instances of itself, running on the same computer and on other computers. This is working fine. But, on some computers, the listener cannot hear messages that other thread/process on the same computer has transmitted. The message is broadcast ok, and other machines on the network hear the message, but a listener on the same machine cannot hear the message.
The weird part is that this happens on SOME machines in my test environment, but not all.
Edit: All machine that fail have the Check Point VPN-1 Securemote client software installed. I took a machine that was working, installed the VPN client, and now it does not work. Note that I am not connected to any VPN hosts, I just have the client installed.
All machines have a single network adapter, subnet mask of 255.255.255.0, and IP address of 10.3.10.xxx.
Here is a test class that demonstrates the problem. The user types some text, and it gets sent to 10.3.10.255. On some machines, the ReceiveFrom returns, and on others it does not. I am calling Controller("10.3.10.255",33333)
public class Controller
{
public Controller(IPAddress broadcastAddress, int port)
{
_broadcastAddress = broadcastAddress;
_port = port;
}
public void Start()
{
Socket s = null;
try
{
IPEndPoint _listenEndpoint = new IPEndPoint(IPAddress.Any, _port);
_broadcastEndpoint = new IPEndPoint(_broadcastAddress, _port);
s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 10);
s.EnableBroadcast = true;
s.Bind(_listenEndpoint);
SocketState receiveState = new SocketState();
receiveState.s = s;
receiveState.buf = new byte[1024];
EndPoint lep = (EndPoint)_broadcastEndpoint;
s.BeginReceiveFrom(receiveState.buf, 0, receiveState.buf.Length, SocketFlags.None, ref lep, new AsyncCallback(OnReceive), receiveState);
bool done = false;
while (!done)
{
string msg = Console.In.ReadLine();
byte[] msg_bytes = Encoding.ASCII.GetBytes(msg);
if (msg_bytes.Length == 0)
done = true;
else
{
Console.Out.WriteLine("---> {0}", msg);
s.SendTo(msg_bytes, msg_bytes.Length, SocketFlags.None, new IPEndPoint(_broadcastAddress, _port));
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (s != null)
s.Close();
}
}
internal void OnReceive(IAsyncResult ar)
{
SocketState state = ar.AsyncState as SocketState;
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);
EndPoint ep = (EndPoint)ipep;
int nRead = state.s.EndReceiveFrom(ar, ref ep);
IPEndPoint myipep = ep as IPEndPoint;
Console.WriteLine("<--- {0} {1}", myipep.Address.ToString(), System.Text.Encoding.ASCII.GetString(state.buf, 0, nRead));
EndPoint lep = (EndPoint)_broadcastEndpoint;
state.s.BeginReceiveFrom(state.buf, 0, state.buf.Length, SocketFlags.None, ref lep, new AsyncCallback(OnReceive), state);
}
IPAddress _broadcastAddress;
int _port = 0;
IPEndPoint _broadcastEndpoint;
}
internal class SocketState
{
internal Socket s;
internal byte[] buf;
}
What does the Check Point VPN-1 Securemote client software do? It sounds like it might do some sort of firewalling in which case it would be blocking data coming in on the specified port.
1) If you can configure it to allow data through that port, then you should be good to go.
2) Another less likely option is that maybe it is listening on the port that you are trying to listen to in which case it is receiving the udp packet as opposed to your application. I would expect you app to throw an error though in that case.
In the SecureClient settings (right click on the tray icon and select Settings), on the Security tab you can enable an option to "log all connections that are blocked by the desktop security policy".
You should then be able to see if that's indeed what is blocking your application - if it is, you'll have to get the default policy changed to allow your application. I'm not sure how to do that - I've never administered the server side of SecureRemote, but I'm sure Check Point can help you out if your network admins can't.