使用 System.Net.Sockets; windows 商店应用架构不支持,应该使用什么?

using System.Net.Sockets; not supported in windows store app architecture, what to use instead?

我在 windows 表单中创建了一个简单的客户端和服务器应用程序,现在我想在 windows 商店应用程序框架中创建它。使用 System.Net.Sockets;那里不支持。我应该如何处理这个问题以及我应该使用什么 class ?以下是winforms c#中的代码:非常感谢

using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Media;

namespace ChatClient
{
    public partial class Form1 : Form
    {

        Socket sck;

        EndPoint epLocal, epRemote;
        public Form1()
        {
            InitializeComponent();

          //what would I use instead in windows store app, using System.Net.Sockets; namespace isnt supported ?
            sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        }


        //Getting local IP address through code
        private string GetLocalIP()
        {
            IPHostEntry host;
            string localIP = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    localIP = ip.ToString();
                    break;
                }
            }
            return localIP;
        }

        private void MessageCallBack (IAsyncResult aResult)
        {
            try
            {

                int size = sck.EndReceiveFrom(aResult, ref epRemote);
                if (size>0)
                {
                    byte[] receivedData = new byte[1464];
                    receivedData = (byte[])aResult.AsyncState;
                    ASCIIEncoding eEncoding = new ASCIIEncoding();
                    string receiveMessage = eEncoding.GetString(receivedData);
                    listMessage.Items.Add("Friend : "+receiveMessage);


                }

                byte[] buffer = new byte[1500];
                sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());

            }

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            groupBox1.Text = Environment.UserName;

            textBox1.Text = GetLocalIP().ToString();

            btnSend.Enabled = false;

        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {

                if (CheckConnection() == 1)
                {
                    btnStart.Text = "Please Wait...";
                    Thread.Sleep(2000);
                    epLocal = new IPEndPoint(IPAddress.Parse(textBox1.Text), Convert.ToInt32(comboBox1.Text));
                    sck.Bind(epLocal);
                    epRemote = new IPEndPoint(IPAddress.Parse(fndIP.Text), Convert.ToInt32(comboBox2.Text));
                    sck.Connect(epRemote);
                    byte[] buffer = new byte[1500];
                    sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);

                    btnStart.Enabled = false;
                    btnStart.Text = "Connected";

                    btnStart.BackColor = Color.LightGreen;
                    btnStart.ForeColor = Color.White;
                    btnSend.Enabled = true;
                    textBox5.Focus();

                    button1.Enabled = true;
                }

                else
                {

                    MessageBox.Show("Check Your Internet connection");

                }


            }
            catch(Exception ex1)
            {
                MessageBox.Show(ex1.ToString());

            }

        }

        private void btnSend_Click(object sender, EventArgs e)
        {

            try
            {

                System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                byte[] msg = new byte[1500];
                msg = enc.GetBytes(textBox5.Text);
                new SoundPlayer(Properties.Resources.chat).Play();
                sck.Send(msg);
                listMessage.Items.Add("Me :   "+textBox5.Text);
                new SoundPlayer(Properties.Resources.chat).Play();
                textBox5.Clear();
            }
            catch(Exception ex)
            {

                MessageBox.Show(ex.ToString());

            }


        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private int CheckConnection ()
        {

            Ping myPing = new Ping();
            String host = "74.125.20.147";
            byte[] buffer = new byte[32];
            int timeout = 1000;
            PingOptions pingOptions = new PingOptions();
            PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
            if (reply.Status == IPStatus.Success)
            {
                return 1;
            }


            else
            {

                return 0;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            sck.Close();

        }

    }
}

来自Windows 8 development > How to > How tos (XAML) > Connecting to networks and web services Connecting with sockets

Send and receive data with TCP or UDP sockets in your Windows Runtime app using features in the Windows.Networking.Sockets namespace.

您使用 UDP,因此请参阅 How to connect with a datagram socket (XAML) which uses the class Windows.Networking.Sockets.DatagramSocket

它在 Windows.Networking.Sockets 命名空间中 有关如何处理这些差异的完整说明,请参阅 .Net for Windows Store Apps overview 文章。