Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用System.Net.Sockets;windows应用商店应用程序体系结构中不支持,改用什么?_C#_Windows Store Apps - Fatal编程技术网

C# 使用System.Net.Sockets;windows应用商店应用程序体系结构中不支持,改用什么?

C# 使用System.Net.Sockets;windows应用商店应用程序体系结构中不支持,改用什么?,c#,windows-store-apps,C#,Windows Store Apps,我在windows窗体中创建了一个简单的客户端和服务器应用程序,现在我想在windows应用商店应用程序框架中创建它。使用System.Net.Sockets;这里不支持。我应该如何处理这个问题?我应该使用什么类?以下是winforms c#中的代码:非常感谢 using System.Net; using System.Net.Sockets; using System.Net.NetworkInformation; using System.Media; namespace ChatCli

我在windows窗体中创建了一个简单的客户端和服务器应用程序,现在我想在windows应用商店应用程序框架中创建它。使用System.Net.Sockets;这里不支持。我应该如何处理这个问题?我应该使用什么类?以下是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.Networking.sockets
命名空间中的功能,在Windows运行时应用程序中使用TCP或UDP套接字发送和接收数据


您使用UDP,因此可以查看哪个使用该类。

它位于
Windows.Networking.Sockets
命名空间中 有关如何处理这些差异的完整描述,请参阅本文