2012-04-08 28 views
0

我正在編寫一個混合應用程序,該應用程序應該使用TCPClient連接到我的服務器,但在編譯時我得到了Type or namespace TcpClient does not exist...,儘管我知道我包含正確的庫(如這段代碼幾乎是從我的C#windows窗體客戶端直接複製的)。TcpClient在Blend中不可用

我可能會寫它在一個普通的套接字,但如果有人知道爲什麼這不顯示或我怎麼能做到這一點,這將使我的生活更輕鬆。

感謝;)

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Shapes; 
using System.Net.Sockets; 
using System.IO; 
using System.Net; 

namespace CCClient 
{ 
    public partial class CCClient 
    { 
     public CCConnection Connection = null; 

     public CCClient() 
     { 
      if ((this.Connection = new CCConnection(IPAddress.Parse("-_-"), 9001)) == null) 
      { 
       throw new Exception("Could not instantiate Client Connection to Server."); 
      } 
      else 
      { 
       this.Connection.WriteLine("Role: client"); 
       this.Connection.WriteLine("Stream: test"); 
      } 
     } 
    } 

    public class CCConnection 
    { 
     public TcpClient HostConnection = null; 
     public StreamWriter HostWriter = null; 
     public StreamReader HostReader = null; 

     public CCConnection(IPAddress Host, int Port) 
     { 
      if (Host == null || Port == 0) 
      { 
       throw new Exception("Could not instantiation CCConnection. Host or Port are invalid."); 
      } 
      else 
      { 
       try 
       { 
        this.HostConnection = new TcpClient(); 
        this.HostConnection.Connect(Host, Port); 
        this.HostWriter = new StreamWriter(this.HostConnection.GetStream()); 
        this.HostReader = new StreamReader(this.HostConnection.GetStream()); 
       } 
       catch (Exception e) 
       { 
        throw new Exception("Could not instantiate CCConnection. Exception encountered.\n" + e.Message); 
       } 
      } 
     } 

     public void WriteLine(String Argument) 
     { 
      if (!String.IsNullOrWhiteSpace(Argument)) 
      { 
       this.HostWriter.WriteLine(Argument); 
       this.HostWriter.Flush(); 
       return; 
      } 
     } 

     public String ReadLine() 
     { 
      return this.HostReader.ReadLine(); 
     } 
    } 
} 
+0

洛爾並在調查後,System.Net.Socket類只能異步使用? What's – DigitalJedi805 2012-04-08 07:25:16

+0

你的目標是什麼平臺/框架?如果你的目標是Silverlight,那麼是的:你需要使用異步 – 2012-04-08 07:56:22

回答

2

MSDN使它足夠了這個類是隻在.NET全/客戶端配置文件提供清晰,

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx

它不可在Silverlight或XNA因爲微軟認爲你永遠不需要它。

你可以參考Silverlight的文章,看到的是提供什麼樣的網絡支持,

http://msdn.microsoft.com/en-us/library/cc645032%28v=vs.95%29.aspx

,也許你可以使用套接字來達到同樣的目標,

http://msdn.microsoft.com/en-us/library/system.net.sockets.socket%28v=vs.95%29.aspx

相關問題