TCPClient Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank

    #1

    TCPClient 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();
    }


  • Kevin Spencer

    #2
    Re: TCPClient Question

    Just a thought, but it is not necessary to maintain an open connection, if
    you are writing both the server and the client. The server could be a Web
    Services app. Web Services also support Application and Session State. In
    fact, I have worked on an app that has a client-side SWF which makes Web
    Service calls to a Web Service periodically, and the Web Service maintains
    most of the data on the server side, either in Application or Session State.

    If you were to do this, you could simply have a Web Service method that the
    client Page could call at intervals to check whether or not data was
    available, and receive it if it is.

    HTTP is a very reliable protocol, and maintaining an open TCP/IP connection
    is going to be problematic.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    You can lead a fish to a bicycle,
    but it takes a very long time,
    and the bicycle has to *want* to change.

    "Frank" <fkaesser@pfmai l.com> wrote in message
    news:O8eK%23MMB GHA.4084@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    >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();
    > }
    >
    >[/color]


    Comment

    Working...