send array of double trough a tcp connection
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: send array of double trough a tcp connection

  1. #1
    Join Date
    Jun 2009
    Location
    Italy
    Posts
    41

    send array of double trough a tcp connection

    hi to all, i'm a beginning in visual c++ managed programming.
    i've a text file with 2000 double. my program read txt file and with a timer it create an array of 100 double to obtain a temporal signal.
    now i need to create the possibility to send these double in array between 2 program on 2 different pc in a lan or internet.
    so the server program have to accept connection from client program and send it all the 100 double and read other and other 100 double until the end of file...
    client software instead have to insert ip and port number of the server pc, connect to it and receive file....
    i need examples or link with very easy command to use...
    thank's to all!
    bye

  2. #2
    Join Date
    Jul 2002
    Posts
    2,514

    Re: send array of double trough a tcp connection

    You will not find useful examples in C++/CLI, because anyone doesn't write such things in this language. However, you can find a lot of networking examples in C# and VB .NET. Having these examples, translate them to C++/CLI, or better, use C# for this.

  3. #3
    Join Date
    Jun 2009
    Location
    Italy
    Posts
    41

    Re: send array of double trough a tcp connection

    yes i know that but i have to use visual c++...i've not found useful example in c# becouse them are too much different from my necessity...
    do you have some example in c# useful for my specific application?
    very thank's
    bye

  4. #4
    Join Date
    Jun 2009
    Location
    Italy
    Posts
    41

    Re: send array of double trough a tcp connection

    i've created a tcp connection between server and client in c# to send a simple text message...now i have to send array of data...how i can serialize them or how i can convert an object in binary and reconvert it?

  5. #5
    Join Date
    Jul 2002
    Posts
    2,514

    Re: send array of double trough a tcp connection

    See BitConverter.GetBytes Method (Double) and BitConverter.ToDouble Method. These methods allow to convert between double and byte array. Socket Send and Receive Methods use byte array.

  6. #6
    Join Date
    Jun 2009
    Location
    Italy
    Posts
    41

    Re: send array of double trough a tcp connection

    ok i'll se them...
    now i would try to explain better my problem
    hi i'm developing a software in visual c++ clr managed code...
    i have 12 List of double number....i need to send them to another program with a frequency...
    i've found some example of tcp client server connection, now i have to implement a code to connect between 2 program, send one after one all 12 double in 12 list and make that again for all double that are in every list.
    i need that software could work during trasferring so that i could use double received to display a graph with them...
    i've tried this 2 example that seems to work but the client program is blocked waiting for connection...how i can make it active and working waiting for a connection and displaing data?

    server code

    Code:
     try
       {
          Int32 port = 13000;
          IPAddress^ localAddr = IPAddress::Parse( "127.0.0.1" );
          TcpListener^ server = gcnew TcpListener( localAddr,port );
    
          server->Start();
    
          array<Byte>^bytes = gcnew array<Byte>(256);
          String^ data = nullptr;
    	 
          while ( true ) 
    
          {
     MessageBox::Show( "Waiting for a connection... " );
    
             TcpClient^ client = server->AcceptTcpClient();
             MessageBox::Show( "Connected!" );
             data = nullptr;
    
             NetworkStream^ stream = client->GetStream();
             Int32 i;
    
             // Loop to receive all the data sent by the client.
             for ( int x=0;x<4 ;++x )
             {
    			i = stream->Read( bytes, 0, bytes->Length);
                // Translate data bytes to a ASCII String*.
    			 data = System::Text::Encoding::ASCII->GetString( bytes, 0, i );
                MessageBox::Show( "Recevuto dal client: "+ data );
    
    
                
    			array<Byte>^msg = System::Text::Encoding::ASCII->GetBytes( data );
    
                // Send back a response.
                stream->Write( msg, 0, msg->Length );
               // MessageBox::Show( "Inviato al client  "+ data );
             }
    
             // Shutdown and end connection
             client->Close();
          }
       }
       catch ( SocketException^ e ) 
       {
          MessageBox::Show( "SocketException: "+ e );
       }
    
       MessageBox::Show( "\nHit enter to continue..." );client code
    Code:
      try
       {
    	  Int32 port = 13000;
          IPAddress^ localAddr = IPAddress::Parse( "127.0.0.1" );
    
    
          String^ server = "localhost";
          TcpClient^ client = gcnew TcpClient( server,port );
    
    
    
    	  double prova = 3.4;
    		  array<Byte>^data = System::Text::Encoding::ASCII->GetBytes(prova.ToString());
    
    
          NetworkStream^ stream = client->GetStream();
    	  for(int i = 0; i<4; ++i){
          // Send the message to the connected TcpServer. 
          stream->Write( data, 0, data->Length );
    
    	 // MessageBox::Show( "Sent: {0}"+ message );
    
          // Receive the TcpServer::response.
    
          // Buffer to store the response bytes.
          data = gcnew array<Byte>(256);
    
          // String to store the response ASCII representation.
          String^ responseData = String::Empty;
    
    
          Int32 bytes = stream->Read( data, 0, data->Length );
    	  responseData = System::Text::Encoding::ASCII->GetString( data, 0, bytes );
    
    	  }
    
          client->Close();
       }
       catch ( ArgumentNullException^ e ) 
       {
          MessageBox::Show( "ArgumentNullException: {0}"+ e );
       }
       catch ( SocketException^ e ) 
       {
          MessageBox::Show( "SocketException: {0}"+ e );
       }
       MessageBox::Show( "\n Press Enter to continue..." );

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center