[英]How to listen socket in single ASP.NET page
我正在使用TCP Socekts和C#開發客戶端-服務器應用程序。 我需要將消息從控制台應用程序發送到ASP頁面。 我可以使用OnLoad事件從ASP頁面進行連接,有時在PageLoad事件中我只能接收多條消息。 我有2個問題:1.如何連續監聽服務器套接字並接收消息?
2.如何知道(用戶)關閉ASP.NET頁以斷開與服務器的連接?
服務器控制台應用程序:
static void StartServer()
{
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 1000);
serverSocket.Bind(ipEndPoint);
serverSocket.Listen(4);
ConsoleKeyInfo cki;
XmlDocument doc = new XmlDocument();
doc.Load("test.xml");
serverSocket.BeginAccept(new AsyncCallback(OnAccept), null);
Console.WriteLine("Server started " + DateTime.Now + Environment.NewLine);
Console.WriteLine("Press S to start Data Sharing or ESC key to quit" + Environment.NewLine);
do
{
cki = Console.ReadKey();
if (cki.Key == ConsoleKey.S)
{
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
try
{
_client = (ClientInfo)clientList[0];
if (((ClientInfo)clientList[0]).strName == node.Attributes["type"].InnerText)
{
SendRecord(node.Attributes["type"].InnerText + node.Attributes["value"].InnerText, (ClientInfo)clientList[0]);
clientList.Insert(clientList.Count, (ClientInfo)clientList[0]);
clientList.RemoveAt(0);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.message);
}
}
Console.WriteLine("Data sharing finished " + DateTime.Now);
}
} while (cki.Key != ConsoleKey.Escape);
}
static void SendRecord(string _msg, ClientInfo client)
{
Data msgToSend = new Data();
msgToSend.cmdCommand = Command.Message;
msgToSend.strMessage = _msg;
byte[] byteData = msgToSend.ToByte();
try
{
client.socket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), _client);
Thread.Sleep(3);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
ASP.NET客戶端:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Connect();
}
protected void Page_OnClose(object sender, EventArgs e)
{
Disconnect();
}
protected void updateEvent(object sender, EventArgs e)
{
if (msglist.Count > 0) lstRecords.Items.Add(msg);
lstRecords.Height = Unit.Percentage(100);
}
private void Connect()
{
Data msgToSend = new Data();
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 1000);
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
msgToSend.strType = strType;
msgToSend.strMessage = null;
msgToSend.cmdCommand = Command.List;
byteData = msgToSend.ToByte();
try
{
clientSocket.BeginConnect(ipEndPoint, new AsyncCallback(OnConnect), null);
clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
byteData = new byte[1024];
clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
}
catch (Exception ex)
{
lstRecords.Items.Add(ex.Message);
}
}
Server App正在發送到WinApp和ConsoleApp,沒有任何問題。 但是在ASP頁中,我只能正確接收“連接”消息,有時我只能接收1-2-3 msg。 我正在使用javascript setInterval("__doPostBack('upDynamicClock', '');", 1000);
刷新頁面
HTTP是無狀態的,ASP.NET WebForms僅嘗試使用viewstate等將其隱藏。 在瀏覽器中看到該頁面后,該頁面將不再有效,並且將不再調用OnSend
回調。 使用基於實時套接字的數據創建網頁中也對此進行了說明。
如果您的目標是使用服務器提供的數據更新HTML元素,請查看SignalR 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.