我正在开发使用TCP Socekts和C#的客户端 - 服务器应用程序。我需要从控制台应用程序发送消息到ASP页面。我可以使用OnLoad事件从ASP页连接,有时在PageLoad事件中,我只能收到几条消息。我有两个问题: 1.如何连续监听服务器套接字和接收消息?
2.如何知道ASP.NET页面何时关闭(由用户)与服务器断开连接?如何在单个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);
}
}
服务器应用程序被发送到WinApp和ConsoleApp没有任何问题。但对于ASP页面,我只能正确接收连接消息,有时我只能收到1-2-3消息。我正在使用javascript setInterval("__doPostBack('upDynamicClock', '');", 1000);
刷新页面
提供目前为止您已在各方开发的代码的一些亮点,以及一些有效的示例和不适用的示例。 – J0e3gan
实际上,Server App发送到WinApp和ConsoleApp没有任何问题。但对于ASP页面,我只能正确接收连接消息,有时我只能收到1-2-3消息。我正在使用setInterval(“__ doPostBack('upDynamicClock','');”,1000);也要做刷新。此外,我有使用OnClose事件关闭页面上的断开连接的问题。 –
ASP .NET页面生命周期有几个阶段。我很困惑哪一个我必须用来有争议地接收数据并且同时更新控制权?或者这是不可能的? –