Y
Yann STEPHAN via .NET 247
I have a simple question:
Why a simple socket with IAsyncResult works fine on PC but not on Pocket PC. I have an exception given by EndReceive( ar );
Okay, I know that I can pass the socket as parameter of the IAsyncResult, but here is not the question: Why this code runs fine on PC but I have an exception on Pocket PC.
Here the code:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.Text;
namespace TestSocket
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox ctlTxtInformation;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Net.Sockets.Socket m_Socket;
private System.Net.IPEndPoint m_IPEndPoint;
private System.Net.EndPoint m_EndPoint;
private byte[] m_bBuffer = new byte[2048];
private System.Windows.Forms.Button ctlButtonExit;
private AsyncCallback m_AsyncCallback;
private ASCIIEncoding m_AsciiEncoding = new ASCIIEncoding();
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
FillIPAddress();
m_AsyncCallback = new AsyncCallback(this.OnUDPReceive);
m_Socket = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp );
m_IPEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.6"), 5000);
m_EndPoint = (EndPoint) m_IPEndPoint;
m_Socket.Bind(m_IPEndPoint);
m_Socket.BeginReceiveFrom(m_bBuffer, 0, m_bBuffer.Length, SocketFlags.None, ref m_EndPoint, m_AsyncCallback, this);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if (m_Socket.Connected == true)
{
m_Socket.Shutdown(SocketShutdown.Both);
m_Socket.Close();
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.ctlTxtInformation = new System.Windows.Forms.TextBox();
this.ctlButtonExit = new System.Windows.Forms.Button();
//
// ctlTxtInformation
//
this.ctlTxtInformation.AcceptsReturn = true;
this.ctlTxtInformation.AcceptsTab = true;
this.ctlTxtInformation.BackColor = System.Drawing.Color.Black;
this.ctlTxtInformation.ForeColor = System.Drawing.Color.White;
this.ctlTxtInformation.Multiline = true;
this.ctlTxtInformation.ReadOnly = true;
this.ctlTxtInformation.Size = new System.Drawing.Size(240, 232);
this.ctlTxtInformation.Text = "==> SOCKET TEST (ASYNC METHOD)\r\n";
//
// ctlButtonExit
//
this.ctlButtonExit.Location = new System.Drawing.Point(80, 232);
this.ctlButtonExit.Size = new System.Drawing.Size(72, 40);
this.ctlButtonExit.Text = "Exit";
this.ctlButtonExit.Click += new System.EventHandler(this.ctlButtonExit_Click);
//
// Form1
//
this.Controls.Add(this.ctlButtonExit);
this.Controls.Add(this.ctlTxtInformation);
this.Menu = this.mainMenu1;
this.Text = "Form1";
}
#endregion
public void OnUDPReceive( IAsyncResult ar )
{
int nBytesRec = 0;
try
{
nBytesRec = m_Socket.EndReceive( ar );
if (nBytesRec > 0)
{
ctlTxtInformation.Text += m_AsciiEncoding.GetString(m_bBuffer, 0, nBytesRec);
}
m_Socket.BeginReceiveFrom(m_bBuffer, 0, m_bBuffer.Length, SocketFlags.None, ref m_EndPoint, m_AsyncCallback, this);
}
catch(Exception e)
{
Console.WriteLine("==> [Form1/OnUDPReceive] - Exception: " + e.Message);
}
}
private void FillIPAddress()
{
String strHostName = Dns.GetHostName();
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
if (iphostentry.AddressList.Length != 0)
{
foreach(IPAddress ipaddress in iphostentry.AddressList)
{
ctlTxtInformation.Text += "==> IP:" + ipaddress + "\r\n";
}
}
else
{
ctlTxtInformation.Text += "==> NO IP AVAILABLE" + "\r\n";;
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
try
{
Application.Run(new Form1());
}
catch(Exception exception)
{
Console.WriteLine("==> Exception: " + exception.Message);
}
}
private void ctlButtonExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
Why a simple socket with IAsyncResult works fine on PC but not on Pocket PC. I have an exception given by EndReceive( ar );
Okay, I know that I can pass the socket as parameter of the IAsyncResult, but here is not the question: Why this code runs fine on PC but I have an exception on Pocket PC.
Here the code:
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.Net;
using System.Text;
namespace TestSocket
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox ctlTxtInformation;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Net.Sockets.Socket m_Socket;
private System.Net.IPEndPoint m_IPEndPoint;
private System.Net.EndPoint m_EndPoint;
private byte[] m_bBuffer = new byte[2048];
private System.Windows.Forms.Button ctlButtonExit;
private AsyncCallback m_AsyncCallback;
private ASCIIEncoding m_AsciiEncoding = new ASCIIEncoding();
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
FillIPAddress();
m_AsyncCallback = new AsyncCallback(this.OnUDPReceive);
m_Socket = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp );
m_IPEndPoint = new IPEndPoint(IPAddress.Parse("192.168.0.6"), 5000);
m_EndPoint = (EndPoint) m_IPEndPoint;
m_Socket.Bind(m_IPEndPoint);
m_Socket.BeginReceiveFrom(m_bBuffer, 0, m_bBuffer.Length, SocketFlags.None, ref m_EndPoint, m_AsyncCallback, this);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if (m_Socket.Connected == true)
{
m_Socket.Shutdown(SocketShutdown.Both);
m_Socket.Close();
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.ctlTxtInformation = new System.Windows.Forms.TextBox();
this.ctlButtonExit = new System.Windows.Forms.Button();
//
// ctlTxtInformation
//
this.ctlTxtInformation.AcceptsReturn = true;
this.ctlTxtInformation.AcceptsTab = true;
this.ctlTxtInformation.BackColor = System.Drawing.Color.Black;
this.ctlTxtInformation.ForeColor = System.Drawing.Color.White;
this.ctlTxtInformation.Multiline = true;
this.ctlTxtInformation.ReadOnly = true;
this.ctlTxtInformation.Size = new System.Drawing.Size(240, 232);
this.ctlTxtInformation.Text = "==> SOCKET TEST (ASYNC METHOD)\r\n";
//
// ctlButtonExit
//
this.ctlButtonExit.Location = new System.Drawing.Point(80, 232);
this.ctlButtonExit.Size = new System.Drawing.Size(72, 40);
this.ctlButtonExit.Text = "Exit";
this.ctlButtonExit.Click += new System.EventHandler(this.ctlButtonExit_Click);
//
// Form1
//
this.Controls.Add(this.ctlButtonExit);
this.Controls.Add(this.ctlTxtInformation);
this.Menu = this.mainMenu1;
this.Text = "Form1";
}
#endregion
public void OnUDPReceive( IAsyncResult ar )
{
int nBytesRec = 0;
try
{
nBytesRec = m_Socket.EndReceive( ar );
if (nBytesRec > 0)
{
ctlTxtInformation.Text += m_AsciiEncoding.GetString(m_bBuffer, 0, nBytesRec);
}
m_Socket.BeginReceiveFrom(m_bBuffer, 0, m_bBuffer.Length, SocketFlags.None, ref m_EndPoint, m_AsyncCallback, this);
}
catch(Exception e)
{
Console.WriteLine("==> [Form1/OnUDPReceive] - Exception: " + e.Message);
}
}
private void FillIPAddress()
{
String strHostName = Dns.GetHostName();
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
if (iphostentry.AddressList.Length != 0)
{
foreach(IPAddress ipaddress in iphostentry.AddressList)
{
ctlTxtInformation.Text += "==> IP:" + ipaddress + "\r\n";
}
}
else
{
ctlTxtInformation.Text += "==> NO IP AVAILABLE" + "\r\n";;
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
try
{
Application.Run(new Form1());
}
catch(Exception exception)
{
Console.WriteLine("==> Exception: " + exception.Message);
}
}
private void ctlButtonExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}