I am developing client-server application using TCP Socekts and C#. I need to send message from console application to ASP page. I can connect from ASP page using OnLoad event and sometimes on PageLoad event I can receive several messages only. I have 2 questions:
1. How to listen continuously the server socket and receive messages?
2. How to know when the ASP page closed (by user) to disconnect from server?
Console ServerApp - 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 Client side: - 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 is sending to WinApp and ConsoleApp without any problem. But with ASP page, I can correctly receive only Connect message and sometimes i can receive 1-2-3 msg only. I am using javascript - setInterval("__doPostBack('upDynamicClock', '');", 1000);
to do refresh.
ASP .NET page life-cycle has several stages. I am confused which one i have to use to receive data contentiously and update control at the same time? Or it's impossible?
3 1903
To understand the ASP.NET life cycle you should probably read the documentation provided by Microsoft on the topic. You can find this documentation in the MSDN Library. For example, this is a good over view for the ASP.NET Life Cycle.
Anyways, you should probably consider creating a web service application instead of an ASP.NET Forums application.
Solved.
HTTP is stateless, ASP.NET WebForms only tries to hide this with viewstate and such. As soon as you see the page in your browser, the page isn't alive anymore and your OnSend callback won't be called anymore. This is also explained in Create web page with live socket based data.
If your goal is to update an HTML element with server-fed data, take a look at SignalR. http://stackoverflow.com/questions/2...33934_21873956[^]
I'm glad you figured it out!
Thanks for sharing your solution.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: iMedia User |
last post by:
I have a site where I want to use the Web form validators in two separate
forms on a single page. One form allows existing users to log in while the
second one allows new users to register. The...
|
by: Adrijan Josic |
last post by:
I have this idea, I need to know if it is possible and how.
Let's say you have a content managed site with all its structure and content - everything in a relational database
And a "blank" page...
|
by: Ben Fidge |
last post by:
Is is possible to make just a single page within an ASP.NET application use
SSL?
I've written an e-commerce application and want to make just the checkout
page use SSL for secure transmission.
...
|
by: Keith Patrick |
last post by:
I have a web app that consists of a single page. It works a lot like
starter kit examples of menu/content apps, and it has a sitemap associated
with it. However, my app doesn't go from page to...
|
by: arteezan |
last post by:
Mabuhay! I'm new in PHP and i want to create a single PHP page where
the links are linked to contents within that single page. can anyone
please help me with this? thanks in advance to anyone who...
|
by: google |
last post by:
Hi
I'm looking for some advice, so if anyone wants to contribute I would
be grateful.
I'm trying to create a web application. By application I mean
something that is like a Windows...
|
by: Andrew Burton |
last post by:
I'm poking at a small "single page application" (SPA), ala TiddlyWiki,
to act as kind of a local, single-user version of Twitter (no real
application, except to familiarize myself with JavaScript)....
|
by: =?Utf-8?B?RGFu?= |
last post by:
is anyone know why and how to overcome this problem discussed on this thread.
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1984652&SiteID=1
|
by: timsamshuijzen |
last post by:
The website I am developing is implemented as a Single Page Interface
(SPI). During a visitor's session all new content gets loaded by
JavaScript (Ajax), no new pages get loaded and the URL in the...
|
by: NawazAhmed |
last post by:
Hi,
I know this is a very simple question....But I tried every possible way I know to answer this question and failed everytime.
This is the scenario:
I am using Visual Studio 2008 and Sql 2005....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |