Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In regard to the buffer problem try the following:</p> <p>Create a class called StateObject to store any data you want to have in your callback, with a buffer, also including the socket if you so need it (as I see that you are currently passing udpSock as your stateObject). Pass the newly created object to the async method and then you will have access to it in your callback.</p> <pre><code>public void Starter(){ StateObject state = new StateObject(); //set any values in state you need here. //create a new socket and start listening on the loopback address. Socket lSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); lSock.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345); EndPoint ncEP = new IPEndPoint(IPAddress.Any, 0); lSock.BeginReceiveFrom(state.buffer, 0, state.buffer.Length, SocketFlags.None, ref ncEP, DoReceiveFrom, state); //create a new socket and start listening on each IPAddress in the Dns host. foreach(IPAddress addr in Dns.GetHostEntry(Dns.GetHostName()).AddressList){ if(addr.AddressFamily != AddressFamily.InterNetwork) continue; //Skip all but IPv4 addresses. Socket s = new Socket(addr.AddressFamily, SocketType.Dgram, ProtocolType.Udp); s.Bind(new IPEndPoint(addr, 12345)); EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0); StateObject objState = new StateObject(); s.BeginReceiveFrom(objState.buffer, 0, objState.buffer.length, SocketFlags.None, ref newClientEP, DoReceiveFrom, objState); } } </code></pre> <p>In searching this question I found:</p> <p><a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.beginreceivefrom.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.beginreceivefrom.aspx</a></p> <p>You can then cast the StateObject from AsyncState as you are currently doing with udpSock and your buffer, as well as anyother data you need would be stored there.</p> <p>I suppose that now the only problem is how and where to store the data, but as I don't know your implementation I can't help there.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload