0

我正在编写一个应该使用 TCPClient 连接到我的服务器的混合应用程序,但是在编译时我得到了Type or namespace TcpClient does not exist...,即使我知道我包含了正确的库(因为这段代码几乎是直接从我的 C# windows 窗体复制的客户)。

我可能会在普通的 Socket 上写它,但如果有人知道为什么这不显示或者我该如何制作它,它会让我的生活更轻松。

谢谢 ;)

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();
        }
    }
}
4

1 回答 1

2

MSDN 清楚地表明,此类仅适用于 .NET 完整/客户端配置文件,

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

它在 Silverlight 或 XNA 中不可用,因为 Microsoft 认为您不需要它。

您可以参考 Silverlight 文章,了解提供了什么样的网络支持,

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

也许您可以使用 Socket 来实现相同的目标,

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

于 2012-04-08T08:10:07.287 回答