?
=?ISO-8859-1?Q?Linus_R=F6rstad?=
Hi!
I'm having a problem using asynchronous socket communication over UDP to
connect to a NTP server. The problem I'm having is if I use synchronous
communication to the server all is fine but if I switch over to
asynchronous communication nothing is received.
private void Start(string host)
{
try
{
IPHostEntry hostadd = Dns.Resolve(host);
IPEndPoint EPhost = new IPEndPoint(hostadd.AddressList[0], 123);
TimeSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
TimeSocket.Blocking = false;
//Connect the time server
timeoutTimer = new Timer(new TimerCallback(OnTimedOut), null,
timeout*1000, System.Threading.Timeout.Infinite);
TimeSocket.BeginConnect(EPhost, new AsyncCallback(OnConnect),
TimeSocket);
while(connectionStatus != ConnectionStatus.Connected)
{
if(connectionStatus == ConnectionStatus.Disconnected)
throw new SocketException();
}
// Initialize data structure
Initialize();
// Initialize the transmit timestamp
transmitTimestamp = DateTime.Now;
SocketStateObject so2 = new SocketStateObject(TimeSocket);
timeoutTimer.Change(timeout*1000, System.Threading.Timeout.Infinite);
TimeSocket.BeginSend(data, 0, data.Length, SocketFlags.None, new
AsyncCallback(OnSent), so2);
while(connectionStatus != ConnectionStatus.DataSent)
{
if(connectionStatus == ConnectionStatus.TimedOut)
throw new SocketException();
}
so2 = new SocketStateObject(TimeSocket);
timeoutTimer.Change(timeout*1000, System.Threading.Timeout.Infinite);
TimeSocket.BeginReceive(data, 0, data.Length, 0, new
AsyncCallback(OnReceived), so2);
while(connectionStatus != ConnectionStatus.DataReceived)
{
if(connectionStatus == ConnectionStatus.TimedOut)
{
throw new SocketException();
}
}
}
catch(SocketException e)
{
throw new SocketException(e.Message);
}
}
public void OnConnect(IAsyncResult iar)
{
timeoutTimer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
Socket sock = (Socket) iar.AsyncState;
sock.EndConnect(iar);
Console.WriteLine("Connected=" + sock.Connected.ToString());
if(sock.Connected == true)
{
connectionStatus = ConnectionStatus.Connected;
}
else
connectionStatus = ConnectionStatus.Disconnected;
}
public void OnSent(IAsyncResult iar)
{
timeoutTimer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
SocketStateObject so = (SocketStateObject)iar.AsyncState;
Socket s = so.WorkSocket;
try
{
if (s == null || !s.Connected)
return;
int send = s.EndSend(iar);
connectionStatus = ConnectionStatus.DataSent;
Console.WriteLine(iar.ToString());
}
catch {}
}
public void OnReceived(IAsyncResult iar)
{
timeoutTimer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
SocketStateObject so = (SocketStateObject)iar.AsyncState;
Socket s = so.WorkSocket;
if (s == null || !s.Connected)
return;
int read = s.EndReceive(iar);
if (read > 0)
{
string msg = Encoding.ASCII.GetString(data, 0, read);
connectionStatus = ConnectionStatus.DataReceived;
}
else
{
connectionStatus = ConnectionStatus.TimedOut;
}
}
public void OnTimedOut(Object o)
{
timeoutTimer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
connectionStatus = ConnectionStatus.TimedOut;
}
I'm I doing something wrong?
Thanks
Linus
I'm having a problem using asynchronous socket communication over UDP to
connect to a NTP server. The problem I'm having is if I use synchronous
communication to the server all is fine but if I switch over to
asynchronous communication nothing is received.
private void Start(string host)
{
try
{
IPHostEntry hostadd = Dns.Resolve(host);
IPEndPoint EPhost = new IPEndPoint(hostadd.AddressList[0], 123);
TimeSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
TimeSocket.Blocking = false;
//Connect the time server
timeoutTimer = new Timer(new TimerCallback(OnTimedOut), null,
timeout*1000, System.Threading.Timeout.Infinite);
TimeSocket.BeginConnect(EPhost, new AsyncCallback(OnConnect),
TimeSocket);
while(connectionStatus != ConnectionStatus.Connected)
{
if(connectionStatus == ConnectionStatus.Disconnected)
throw new SocketException();
}
// Initialize data structure
Initialize();
// Initialize the transmit timestamp
transmitTimestamp = DateTime.Now;
SocketStateObject so2 = new SocketStateObject(TimeSocket);
timeoutTimer.Change(timeout*1000, System.Threading.Timeout.Infinite);
TimeSocket.BeginSend(data, 0, data.Length, SocketFlags.None, new
AsyncCallback(OnSent), so2);
while(connectionStatus != ConnectionStatus.DataSent)
{
if(connectionStatus == ConnectionStatus.TimedOut)
throw new SocketException();
}
so2 = new SocketStateObject(TimeSocket);
timeoutTimer.Change(timeout*1000, System.Threading.Timeout.Infinite);
TimeSocket.BeginReceive(data, 0, data.Length, 0, new
AsyncCallback(OnReceived), so2);
while(connectionStatus != ConnectionStatus.DataReceived)
{
if(connectionStatus == ConnectionStatus.TimedOut)
{
throw new SocketException();
}
}
}
catch(SocketException e)
{
throw new SocketException(e.Message);
}
}
public void OnConnect(IAsyncResult iar)
{
timeoutTimer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
Socket sock = (Socket) iar.AsyncState;
sock.EndConnect(iar);
Console.WriteLine("Connected=" + sock.Connected.ToString());
if(sock.Connected == true)
{
connectionStatus = ConnectionStatus.Connected;
}
else
connectionStatus = ConnectionStatus.Disconnected;
}
public void OnSent(IAsyncResult iar)
{
timeoutTimer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
SocketStateObject so = (SocketStateObject)iar.AsyncState;
Socket s = so.WorkSocket;
try
{
if (s == null || !s.Connected)
return;
int send = s.EndSend(iar);
connectionStatus = ConnectionStatus.DataSent;
Console.WriteLine(iar.ToString());
}
catch {}
}
public void OnReceived(IAsyncResult iar)
{
timeoutTimer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
SocketStateObject so = (SocketStateObject)iar.AsyncState;
Socket s = so.WorkSocket;
if (s == null || !s.Connected)
return;
int read = s.EndReceive(iar);
if (read > 0)
{
string msg = Encoding.ASCII.GetString(data, 0, read);
connectionStatus = ConnectionStatus.DataReceived;
}
else
{
connectionStatus = ConnectionStatus.TimedOut;
}
}
public void OnTimedOut(Object o)
{
timeoutTimer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
connectionStatus = ConnectionStatus.TimedOut;
}
I'm I doing something wrong?
Thanks
Linus