使用System.Net.Sockets;不支持在Windows商店应用程序架构,用什么代替

本文关键字:应用程序 什么 Sockets Net System 不支持 Windows 使用 | 更新日期: 2023-09-27 18:07:37

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

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

使用System.Net.Sockets;不支持在Windows商店应用程序架构,用什么代替

From Windows 8开发> How to> How tos (XAML)>连接到网络和web服务使用套接字连接:

在Windows运行时应用程序中使用Windows.Networking.Sockets命名空间中的功能使用TCP或UDP套接字发送和接收数据

您使用UDP,因此请参阅如何与使用Windows.Networking.Sockets.DatagramSocket类的数据报套接字(XAML)连接。

Windows.Networking.Sockets命名空间中关于如何处理这些差异的完整描述,请参阅。net for Windows Store Apps概述文章。