当前位置:编程学习 > C#/ASP.NET >>

VC++2010编写数据采集软件,怎么使用TcpClient类啊????

怎么添加使用TcpClient类啊?
急急急
研究好几天了 --------------------编程问答-------------------- 引入using System.Net.Sockets;
TcpClient tcpclient = new TcpClient("180.213.5.100", 8000);//ip地址 端口号 --------------------编程问答-------------------- 引入using namespace System::Net::Sockets;
TcpClient^ tcpclient = gcnew TcpClient("180.213.5.100", 8000);//ip地址 端口号 --------------------编程问答--------------------

void Connect( String^ server, String^ message )
{
   try
   {
      // Create a TcpClient.
      // Note, for this client to work you need to have a TcpServer 
      // connected to the same address as specified by the server, port
      // combination.
      Int32 port = 13000;
      TcpClient^ client = gcnew TcpClient( server,port );
      
      // Translate the passed message into ASCII and store it as a Byte array.
      array<Byte>^data = Text::Encoding::ASCII->GetBytes( message );
      
      // Get a client stream for reading and writing.
      //  Stream stream = client->GetStream();

      NetworkStream^ stream = client->GetStream();
      
      // Send the message to the connected TcpServer. 
      stream->Write( data, 0, data->Length );

      Console::WriteLine( "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;
      
      // Read the first batch of the TcpServer response bytes.
      Int32 bytes = stream->Read( data, 0, data->Length );
      responseData = Text::Encoding::ASCII->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();
}


msdn上的例子。 --------------------编程问答-------------------- 是不是需要托管啊?
--------------------编程问答-------------------- 是不是需要托管啊?
--------------------编程问答--------------------
引用 4 楼  的回复:
是不是需要托管啊?

tcpclient是托管类,当然要用托管
--------------------编程问答-------------------- 怎么进行托管啊?是不是使用clr啊?
使用clr后出现很多错误啊,怎么回事啊?
还有不能使用using  system*  怎么回事?
在项目中添加TcpClient类啊 只是引用命名空间就可以了吗?
谢谢 高手 --------------------编程问答-------------------- 引入using namespace System::Net::Sockets;
TcpClient^ tcpclient = gcnew TcpClient("180.213.5.100", 8000);//ip地址 端口号 --------------------编程问答--------------------  error C2653: “System”: 不是类或命名空间名称
 error C2871: “Sockets”: 具有该名称的命名空间不存在
???????? --------------------编程问答-------------------- 你还是用非托管吧,托管的调试麻烦 --------------------编程问答--------------------

http://msdn.microsoft.com/zh-cn/library/system.net.sockets.tcpclient.aspx

MSDN很详细的说明和Demo,这个不需要花这么大力气去研究吧
补充:.NET技术 ,  VC.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,