?
Solved

Good System::Net::Sockets tcpClient tutorial?

Posted on 2006-03-28
2
Medium Priority
?
2,271 Views
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?
0
Comment
Question by:bjqrn
2 Comments
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16310184
You may refer this..thou no. of tutorials here are in C# but these may help too :
http://www.dotnet247.com/247reference/System/Net/Sockets/TcpClient.aspx

for VC++.NET refer:
http://www.thecodeproject.com/managedcpp/get_the_ip_out_tcpclient.asp

MAHESH
0
 
LVL 2

Accepted Solution

by:
Sammo earned 2000 total points
ID: 16393370
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{
private:

    int targetPort;
    bool readData;
    bool DONE;

public:
    cTCPClient(){
            readData = false;
            DONE = false;
    }

      String* sendData(String* serverName, int port, String* data){
            String* dataReceived;
            try{
                  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);
                        tcpStream->Flush();
                  }
                  
                  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){
                     MessageBox::Show(e->ToString());
            }
            
            return dataReceived;
            
    }
   
};


The original code was in C# from here:
http://www.c-sharpcorner.com/Network/simpleTcpUdpServerClientPL2.asp

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!

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

850 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