/*★ Tcp Client Program 前田 稔 ★*/
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Net;
using namespace System::Net::Sockets;
using namespace System::Text;
using namespace System::Threading;
void Connect( String^ server, String^ message )
{
try
{
// Create a TcpClient.
Int32 port = 13000;
TcpClient^ client = gcnew TcpClient( server,port );
array^data = Text::Encoding::UTF8->GetBytes( message );
// Get a client stream for reading and writing.
NetworkStream^ stream = client->GetStream();
// Send the message to the connected TcpServer.
stream->Write( data, 0, data->Length );
Console::WriteLine( "Sent: {0}", message );
// Buffer to store the response bytes.
data = gcnew array(256);
String^ responseData = String::Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream->Read( data, 0, data->Length );
responseData = Text::Encoding::UTF8->GetString( data, 0, bytes );
Console::WriteLine( "Received: {0}", responseData );
// Close everything.
client->Close();
}
catch ( ArgumentNullException^ e )
{
Console::WriteLine( "ArgumentNullException: {0}", e );
}
catch ( SocketException^ e )
{
Console::WriteLine( "SocketException: {0}", e );
}
Console::WriteLine( "\n Press Enter to continue..." );
Console::Read();
}
void main()
{
//Connect("127.0.0.1", "Hello Network");
Connect("127.0.0.1", "始めまして!");
}
|