D
Dave
I can't figure out why my code does not recieve the message. The only
message that it receives is the one going out from my machine. I am
suppose to send out a message to a server. Upon receiving that message
on the server side the server is suppose to send a message back. I
can't get my app to receive that message. It only receives the one
being sent out by my machine. I know that the server is sending out a
message because there is another app in vb that receives the message.
Here is my code:
private constructor()
{
iepSend = new IPEndPoint(IPAddress.Broadcast, UDP_PORT);
epSend = (EndPoint)iepSend;
iepRx = new IPEndPoint(IPAddress.Any, UDP_PORT);
epRx = (EndPoint)iepRx;
m_SocketUDP = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
m_SocketUDP.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
m_SocketUDP.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
m_SocketUDP.Connect(epSend);
udpRxSocket = new Socket(iepRx.AddressFamily, SocketType.Dgram,
ProtocolType.Udp);
udpRxSocket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
udpRxSocket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 5000);
udpRxSocket.Bind(iepRx);
SetupReceiveCallbackUDP()
}
private void SetupReceiveCallbackUDP()
{
AsyncCallback receiveData = new AsyncCallback(OnReceiveData);
udpRxSocket.BeginReceiveFrom(buffer, 0, buffer.Length,
SocketFlags.None, ref epRx, receiveData, udpRxSocket);
}
private void OnReceiveData(IAsyncResult ar)
{
Socket socket = (Socket)ar.AsyncState;
try
{
int nBytesRec = socket.EndReceiveFrom(ar, ref epRx);
if(nBytesRec > 0)
{
string Received = Encoding.ASCII.GetString(buffer, 0, nBytesRec);
//Received += socket.RemoteEndPoint.ToString();
WriteOutput("Received Data: " + Received);
SetupReceiveCallbackUDP();
}
}
catch(Exception e)
{
WriteOutput("Error. "+ e.Message);
}
}
Anyone know whats wrong?
message that it receives is the one going out from my machine. I am
suppose to send out a message to a server. Upon receiving that message
on the server side the server is suppose to send a message back. I
can't get my app to receive that message. It only receives the one
being sent out by my machine. I know that the server is sending out a
message because there is another app in vb that receives the message.
Here is my code:
private constructor()
{
iepSend = new IPEndPoint(IPAddress.Broadcast, UDP_PORT);
epSend = (EndPoint)iepSend;
iepRx = new IPEndPoint(IPAddress.Any, UDP_PORT);
epRx = (EndPoint)iepRx;
m_SocketUDP = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
m_SocketUDP.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
m_SocketUDP.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
m_SocketUDP.Connect(epSend);
udpRxSocket = new Socket(iepRx.AddressFamily, SocketType.Dgram,
ProtocolType.Udp);
udpRxSocket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1);
udpRxSocket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 5000);
udpRxSocket.Bind(iepRx);
SetupReceiveCallbackUDP()
}
private void SetupReceiveCallbackUDP()
{
AsyncCallback receiveData = new AsyncCallback(OnReceiveData);
udpRxSocket.BeginReceiveFrom(buffer, 0, buffer.Length,
SocketFlags.None, ref epRx, receiveData, udpRxSocket);
}
private void OnReceiveData(IAsyncResult ar)
{
Socket socket = (Socket)ar.AsyncState;
try
{
int nBytesRec = socket.EndReceiveFrom(ar, ref epRx);
if(nBytesRec > 0)
{
string Received = Encoding.ASCII.GetString(buffer, 0, nBytesRec);
//Received += socket.RemoteEndPoint.ToString();
WriteOutput("Received Data: " + Received);
SetupReceiveCallbackUDP();
}
}
catch(Exception e)
{
WriteOutput("Error. "+ e.Message);
}
}
Anyone know whats wrong?