Z
zoran
Does anybody know how to write an Asynchronous UDP sever, because I
think my code is correct but for some reason I do not get the data
back, and I know the client is sending everything fine because if I
replace the funciton so it would not be asynchronous it works. Here is
the code any help would be greatly appriciated I have been dealing
with this for weeks. (the code is in C#)
private Socket m_udpServer
private ManualResetEvent allDone;
public void createUDPServer(...)
{
m_udpServer = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
IPEndPoint clientIPEndPoint =new IPEndPoint(IPAddress.Any, 0);
EndPoint clientEndPoint = (EndPoint)clientIPEndPoint;
StateObject state = new StateObject();
state.endPoint = clientEndPoint;
state.readBytes = state.getByteReceiveBuffer(m_nServerBufferSize);
m_udpServer.Bind(localIpEndPoint);
while(true)
{
allDone.Reset();
m_udpServer.BeginReceiveFrom(state.readBytes, 0,
m_nServerBufferSize, SocketFlags.None, ref clientEndPoint, new
AsyncCallback(this.udpAcceptCallback), state);
allDone.WaitOne();
}
}
public void udpAcceptCallback(IAsyncResult ar)
{
allDone.Set();
StateObject state = (StateObject)ar.AsyncState;
Socket sock = state.workSocket;
IPEndPoint clientIPEndPoint = new IPEndPoint(IPAddress.Any, 0);
EndPoint clientEndPoint = (EndPoint)clientIPEndPoint;
int nRead = sock.EndReceiveFrom(ar,ref clientEndPoint);
string strData = Encoding.ASCII.GetString(state.readBytes, 0,
nRead);
}
state.readBytes which is just an array of type byte[] and it is
properly allocated does not get anything. If I replace the
BeginReceiveForm with RecieveForm I am getting the right result but I
want my server to be asynchronous. Please help this is driving me
nuts!!!
Zoran
think my code is correct but for some reason I do not get the data
back, and I know the client is sending everything fine because if I
replace the funciton so it would not be asynchronous it works. Here is
the code any help would be greatly appriciated I have been dealing
with this for weeks. (the code is in C#)
private Socket m_udpServer
private ManualResetEvent allDone;
public void createUDPServer(...)
{
m_udpServer = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
IPEndPoint clientIPEndPoint =new IPEndPoint(IPAddress.Any, 0);
EndPoint clientEndPoint = (EndPoint)clientIPEndPoint;
StateObject state = new StateObject();
state.endPoint = clientEndPoint;
state.readBytes = state.getByteReceiveBuffer(m_nServerBufferSize);
m_udpServer.Bind(localIpEndPoint);
while(true)
{
allDone.Reset();
m_udpServer.BeginReceiveFrom(state.readBytes, 0,
m_nServerBufferSize, SocketFlags.None, ref clientEndPoint, new
AsyncCallback(this.udpAcceptCallback), state);
allDone.WaitOne();
}
}
public void udpAcceptCallback(IAsyncResult ar)
{
allDone.Set();
StateObject state = (StateObject)ar.AsyncState;
Socket sock = state.workSocket;
IPEndPoint clientIPEndPoint = new IPEndPoint(IPAddress.Any, 0);
EndPoint clientEndPoint = (EndPoint)clientIPEndPoint;
int nRead = sock.EndReceiveFrom(ar,ref clientEndPoint);
string strData = Encoding.ASCII.GetString(state.readBytes, 0,
nRead);
}
state.readBytes which is just an array of type byte[] and it is
properly allocated does not get anything. If I replace the
BeginReceiveForm with RecieveForm I am getting the right result but I
want my server to be asynchronous. Please help this is driving me
nuts!!!
Zoran