473,729 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TCPClient App Question

I wonder if anyone could give me a suggestion on how to best handle this
scenario.

I wish to have an aspx page contact a server and receive messages.
Initially, a login messge is sent, then after being validated, a second
message is sent which contains data to complete a stock market trade. The
server then should respond not only once, but with several return messages.

So far all I have found are examples that send, receive, and disconnect,
which I have working. However, since all returning data to the client should
be in response to the order submitted, I cannot simply continue to resend
the original data for the trade. I need to keep the connection open and the
grab all incoming messages until the client quits.

Could anyone suggest how I should modfy the following code to keep the
connection open and continue to rceive subsequent messages?

static void Connect(String server, String message)
{
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 13000;
TcpClient client = new TcpClient(serve r, port);

// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Enc oding.ASCII.Get Bytes(message);

// Get a client stream for reading and writing.
// Stream stream = client.GetStrea m();

NetworkStream stream = client.GetStrea m();

// Send the message to the connected TcpServer.
stream.Write(da ta, 0, data.Length);

Console.WriteLi ne("Sent: {0}", message);

// Receive the TcpServer.respo nse.

// Buffer to store the response bytes.
data = new Byte[256];

// String to store the response ASCII representation.
String responseData = String.Empty;

// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(dat a, 0, data.Length);
responseData = System.Text.Enc oding.ASCII.Get String(data, 0, bytes);
Console.WriteLi ne("Received: {0}", responseData);

// Close everything.
client.Close();
}
catch (ArgumentNullEx ception e)
{
Console.WriteLi ne("ArgumentNul lException: {0}", e);
}
catch (SocketExceptio n e)
{
Console.WriteLi ne("SocketExcep tion: {0}", e);
}

Console.WriteLi ne("\n Press Enter to continue...");
Console.Read();
}
Dec 19 '05 #1
1 1126
try doing something like this:

while(true)
{

NetworkStream stream = client.GetStrea m();
....
....
....

Console.WriteLi ne("Received: {0}", responseData);

}

client.Close();

Dec 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1634
by: Evan Freeman[C++ Samuri] | last post by:
Recently I found a question online that originated from this group, and those who responded to it were of no help to the poster. So I am sorry that I was not watching this group before, but will from now on and will offer my help as follows to anser the original question. **************************************************************************** ******************************************* hi, I work on a computer that is part of a...
1
4535
by: hamil | last post by:
I am having trouble using the TcpListener and TcpClient classes. At the end of this post is server code that runs, and a class whose purpose is described below. I need to know when the client closes the connection so I can exit my listener code. Here is what I have tried. First I asked MSDN and they said..
6
3588
by: Bjoern Schliessmann | last post by:
Hello, I'm currently trying to implement a simulation program with Kamaelia and need a reliable TCP connection to a data server. From Twisted, I know that a method is called if the connection fails by whatever reason. I tried to get the same results with Kamaelia's TCPClient component. If I start up the component and try to connect to a closed TCP port it fails and sends a message out of the signal box, that's okay.
11
2580
by: batmanfreeze | last post by:
I am using C# in .Net 1.1, and need to access the 'Client' Property of TcpClient, so I created a derived class to do this based upon the Microsoft Sample, located at: http://msdn2.microsoft.com/en-us/library/system.net.sockets.tcpclient.client(VS.71).aspx The only problem is that in my main app, when I try to do the following, I get an Invalid Cast Exception: int PortNum = 10000; TcpListener objTcpListen = new...
5
7301
by: puzzlecracker | last post by:
It looks like i need to get IPEndPoint first, but I cannot figure out from msdn the eventual obtainment of machine name and port number. Please suggest a solution. Thanks
0
8917
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9281
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.