Good System::Net::Sockets tcpClient tutorial?

Posted on 2006-03-28
Last Modified: 2007-12-19
Can anyone please provide me with a good System::Net::Sockets TcpClient tutorial for VC.Net 2003 and .NET 1.1?
Question by:bjqrn
    LVL 22

    Expert Comment

    You may refer this..thou no. of tutorials here are in C# but these may help too :

    for VC++.NET refer:

    LVL 2

    Accepted Solution

    Im currently writing an application in VC++ .Net and happen to have a single header file for a TCPClient class:

    using namespace System;
    using namespace System::Net;
    using namespace System::Net::Sockets;

    __gc class cTCPClient{

        int targetPort;
        bool readData;
        bool DONE;

                readData = false;
                DONE = false;

          String* sendData(String* serverName, int port, String* data){
                String* dataReceived;
                      TcpClient* tcpClient = new TcpClient(serverName,port);
                      //Create a NetworkStream for this tcpClient instance.
                      //This is only required for TCP stream.
                      NetworkStream* tcpStream = tcpClient->GetStream();
                      if (tcpStream->CanWrite){
                            Byte inputToBeSent[] = System::Text::Encoding::ASCII->GetBytes(data->ToCharArray());
                            tcpStream->Write(inputToBeSent, 0, inputToBeSent->Length);
                      while (tcpStream->CanRead && !DONE){
                            //We need the DONE condition here because there is possibility that
                            //the stream is ready to be read while there is nothing to be read.
                            if (tcpStream->DataAvailable){

                                  Byte received[] = new Byte[512];

                                  int nBytesReceived = tcpStream->Read(received, 0, received->Length);

                                  dataReceived = System::Text::Encoding::ASCII->GetString(received);

                                  DONE = true;
            }catch (Exception* e){
                return dataReceived;

    The original code was in C# from here:

    I simply converted it to C++ and made a few small changes. The sendData function sends a string to the specified IP address and returns a string containing the response.

    Hope this helps!


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now