Click to See Complete Forum and Search --> : send array of double trough a tcp connection


hadler
October 26th, 2009, 09:31 AM
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

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

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..." );

FilipDimitrov1967
October 30th, 2009, 09:47 AM
Basically at the client start a new thread that will handle all socket stuff, and send received data from this thread back to main thread or a window using custom WM_ message.
Or you can use async socket notifications, which are more complex than solution above.

hadler
October 31st, 2009, 10:53 AM
how i can use thread in my code?