Please take a look at the simple code segment below and advise me what is
wrong.
According to the help and examples I've seen it should work unless I
misunderstand
something.
The problem is that UdpClient.Recei ve method always throws following
exception, even though
I verified that message was successfuly received by devices and responses
were sent back:
System.Net.Sock ets.SocketExcep tion: A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond
at System.Net.Sock ets.Socket.Rece iveFrom(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sock ets.Socket.Rece iveFrom(Byte[] buffer, Int32 size,
SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sock ets.UdpClient.R eceive(IPEndPoi nt& remoteEP)
at Tester.CommonUI Tester.SendBroa dcastCommandTes t() in
c:\windows.net\ cmi.ui\tester\c ommonuitester.c s:line 162The program '[1428]
Tester.exe' has exited with code 0 (0x0).
The code sample:
############### #
public class MyUdpClient : UdpClient
{
public MyUdpClient() : base()
{
}
public MyUdpClient(int nPort) : base(nPort)
{
}
~MyUdpClient()
{
Close() ;
}
public void Initialize()
{
Socket socketObject = this.Client;
socketObject.Se tSocketOption(S ocketOptionLeve l.Socket,
SocketOptionNam e.ReceiveTimeou t, 5000 );
}
};
class CommonUITester
{
private void SendBroadcastCo mmandTest()
{
RandomNumberGen erator rmGen = RandomNumberGen erator.Create() ;
byte [] rndBytes = new byte[2] ;
rmGen.GetNonZer oBytes(rndBytes ) ;
// Choose the port the device will send response to
int nPortNum = Utils.MakeWord( rndBytes[0], rndBytes[1] ) % 16000 + 49155;
// port number is from 49155 to 65155
MyUdpClient objUdpClient1 = new MyUdpClient() ;
objUdpClient1.I nitialize() ;
// Replace 49154 with the port your device listen to.
IPEndPoint epBroadcastIP = new IPEndPoint(IPAd dress.Broadcast , 49154);
// Set byteCommandBuff er to the data which your device understand.
//############### ############### ############### ############### ###
byte [] byteCommandBuff er = new byte[11];
byteCommandBuff er[0] = 13 ;
byteCommandBuff er[1] = 13 ;
byteCommandBuff er[2] = 0 ;
byteCommandBuff er[3] = (byte)Utils.HiB yte(nPortNum);
byteCommandBuff er[4] = (byte)Utils.LoB yte(nPortNum);
//############### ############### ############### ############### ###
try
{
objUdpClient1.S end(byteCommand Buffer, byteCommandBuff er.Length,
epBroadcastIP ) ;
// The IPEndPoint will allow you to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoin t = new IPEndPoint(IPAd dress.Any, 0);
do
{
Thread.Sleep(0 ) ;
// Blocks until a message returns on this socket from a remote host.
byte[] receiveBytes = objUdpClient1.R eceive(ref RemoteIpEndPoin t);
}
while(true ) ;
}
catch(Exception e )
{
Debug.Write(e.T oString() ) ;
}
objUdpClient1.C lose() ;
}
[STAThread]
static void Main(string[] args)
{
CommonUITester cmnUITester = new CommonUITester( ) ;
cmnUITester.Sen dBroadcastComma ndTest() ;
}
}
wrong.
According to the help and examples I've seen it should work unless I
misunderstand
something.
The problem is that UdpClient.Recei ve method always throws following
exception, even though
I verified that message was successfuly received by devices and responses
were sent back:
System.Net.Sock ets.SocketExcep tion: A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond
at System.Net.Sock ets.Socket.Rece iveFrom(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sock ets.Socket.Rece iveFrom(Byte[] buffer, Int32 size,
SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sock ets.UdpClient.R eceive(IPEndPoi nt& remoteEP)
at Tester.CommonUI Tester.SendBroa dcastCommandTes t() in
c:\windows.net\ cmi.ui\tester\c ommonuitester.c s:line 162The program '[1428]
Tester.exe' has exited with code 0 (0x0).
The code sample:
############### #
public class MyUdpClient : UdpClient
{
public MyUdpClient() : base()
{
}
public MyUdpClient(int nPort) : base(nPort)
{
}
~MyUdpClient()
{
Close() ;
}
public void Initialize()
{
Socket socketObject = this.Client;
socketObject.Se tSocketOption(S ocketOptionLeve l.Socket,
SocketOptionNam e.ReceiveTimeou t, 5000 );
}
};
class CommonUITester
{
private void SendBroadcastCo mmandTest()
{
RandomNumberGen erator rmGen = RandomNumberGen erator.Create() ;
byte [] rndBytes = new byte[2] ;
rmGen.GetNonZer oBytes(rndBytes ) ;
// Choose the port the device will send response to
int nPortNum = Utils.MakeWord( rndBytes[0], rndBytes[1] ) % 16000 + 49155;
// port number is from 49155 to 65155
MyUdpClient objUdpClient1 = new MyUdpClient() ;
objUdpClient1.I nitialize() ;
// Replace 49154 with the port your device listen to.
IPEndPoint epBroadcastIP = new IPEndPoint(IPAd dress.Broadcast , 49154);
// Set byteCommandBuff er to the data which your device understand.
//############### ############### ############### ############### ###
byte [] byteCommandBuff er = new byte[11];
byteCommandBuff er[0] = 13 ;
byteCommandBuff er[1] = 13 ;
byteCommandBuff er[2] = 0 ;
byteCommandBuff er[3] = (byte)Utils.HiB yte(nPortNum);
byteCommandBuff er[4] = (byte)Utils.LoB yte(nPortNum);
//############### ############### ############### ############### ###
try
{
objUdpClient1.S end(byteCommand Buffer, byteCommandBuff er.Length,
epBroadcastIP ) ;
// The IPEndPoint will allow you to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoin t = new IPEndPoint(IPAd dress.Any, 0);
do
{
Thread.Sleep(0 ) ;
// Blocks until a message returns on this socket from a remote host.
byte[] receiveBytes = objUdpClient1.R eceive(ref RemoteIpEndPoin t);
}
while(true ) ;
}
catch(Exception e )
{
Debug.Write(e.T oString() ) ;
}
objUdpClient1.C lose() ;
}
[STAThread]
static void Main(string[] args)
{
CommonUITester cmnUITester = new CommonUITester( ) ;
cmnUITester.Sen dBroadcastComma ndTest() ;
}
}
Comment