IPAddress myIP = IPAddress.Parse("127.0.0.1");
直接报错
我用 这个自己的IP 地址报如下错误:
由于以前的函数求值超时,函数求值被禁用。必须继续执行才能重新启用函数求值。当我用别的IP时:
报参考的对象类型不支持尝试的操作。有谁遇到过的帮忙解决下啊?

解决方案 »

  1.   

    IPAddress myIP = IPAddress.Parse("127.0.0.1");这句本身没问题 , 你用在什么场景下?
      

  2.   

    IPAddress myIP = IPAddress.Parse("127.0.0.1"); 这句本身没问题 , 你用在什么场景下?
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;namespace SocketListin
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private IPEndPoint myServer;
            private IPAddress myIP = null;
            private Socket sock;
            private bool check = true;
            private Socket recSock;        /// <summary>
            /// 开始监听
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnStart_Click(object sender, EventArgs e)
            {
                //try
                //{
                   
                //}
                //catch
                //{
                //    MessageBox.Show("IP格式不正确,重新输入!");
                //    return;
                //}            try
                {
                    Thread thread = new Thread(new ThreadStart(Listen));
                    thread.Start();
                }
                catch (Exception ex)
                {                this.txtState.AppendText(ex.Message);
                }        }
            /// <summary>
            /// 发送信息
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnSend_Click(object sender, EventArgs e)
            {
                try
                {
                    Byte[] sendByte = new Byte[64];
                    string sendMsg = this.rchTxtSend.Text + "\r\n";
                    NetworkStream stream = new NetworkStream(recSock);
                    sendByte = System.Text.Encoding.BigEndianUnicode.GetBytes(sendMsg.ToCharArray());
                    stream.Write(sendByte, 0, sendByte.Length);
                }
                catch
                {
                    MessageBox.Show("连接尚未建立,无法发送!");
                    return;
                }
            }
            /// <summary>
            /// 停止监听
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnStop_Click(object sender, EventArgs e)
            {
                try
                {
                    sock.Close();
                    this.txtState.AppendText("主机" + this.txtAddress.Text + "\r\n端口" + this.txtPort.Text + "\r\n停止监听......\r\n");
                }
                catch
                {
                    MessageBox.Show("监听尚未开始,关闭无效!");
                }
            }
            /// <summary>
            /// 线程同步的方法
            /// </summary>
            private void Listen()
            {
                myIP = IPAddress.Parse("10.0.64.103");            myServer = new IPEndPoint(myIP,Int32.Parse(txtPort.Text));
                sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //myIP.s
                sock.Bind(myServer);
                sock.Listen(50);            this.txtState.AppendText("主机" + this.txtAddress.Text + " 端口" + this.txtPort.Text + " 开始监听......\r\n");            while (true)
                {
                    recSock = sock.Accept();
                    if (recSock.Connected)
                    {
                        this.txtState.AppendText("与客户建立连接\r\n信息\r\n");
                        Thread recThread = new Thread(new ThreadStart(Round));
                        recThread.Start();
                    }
                }
            }        /// <summary>
            /// 
            /// </summary>
            private void Round()
            {
                while (true)
                {
                    Byte[] rec = new Byte[64];
                    NetworkStream stream = new NetworkStream(recSock);
                    stream.Read(rec, 0, rec.Length);                string recMessage = System.Text.Encoding.BigEndianUnicode.GetString(rec);
                    this.txtState.AppendText(recMessage + "\r\n");
                }
            }
        }
    }
      

  4.   

    你在线程所启动的方法中直接使用txtPort,txtState控件就不可以啊,要使用delegate
      

  5.   

    除了委托 还有一种偷懒的办法
    在form1的代码中加上
    Control。CheckForIllegalCrossThreadCalls = false但不推荐这样子做 比较危险 线程容易混乱不易控制
      

  6.   

     学习。 为什么 你在线程所启动的方法中直接使用txtPort,txtState控件就不可以啊 控件为啥不能直接访问线程中的数据呢? 难道控件是属于主线程????
      

  7.   

    回去研究下!明天结帖! delegate是引用方法和事件的啊?难道写一个方法来获取控件中的值吗?
      

  8.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;namespace SocketListin
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private IPEndPoint myServer;
            private IPAddress myIP = null;
            private Socket sock;
            private bool check = true;
            private Socket recSock;        /// <summary>
            /// 开始监听
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnStart_Click(object sender, EventArgs e)
            {
                //try
                //{
                   
                //}
                //catch
                //{
                //    MessageBox.Show("IP格式不正确,重新输入!");
                //    return;
                //}            try
                {
                    Thread thread = new Thread(new ThreadStart(Listen));
                    thread.Start();
                }
                catch (Exception ex)
                {                this.txtState.AppendText(ex.Message);
                }        }
            /// <summary>
            /// 发送信息
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnSend_Click(object sender, EventArgs e)
            {
                try
                {
                    Byte[] sendByte = new Byte[64];
                    string sendMsg = this.rchTxtSend.Text + "\r\n";
                    NetworkStream stream = new NetworkStream(recSock);
                    sendByte = System.Text.Encoding.BigEndianUnicode.GetBytes(sendMsg.ToCharArray());
                    stream.Write(sendByte, 0, sendByte.Length);
                }
                catch
                {
                    MessageBox.Show("连接尚未建立,无法发送!");
                    return;
                }
            }
            /// <summary>
            /// 停止监听
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnStop_Click(object sender, EventArgs e)
            {
                try
                {
                    sock.Close();
                    this.txtState.AppendText("主机" + this.txtAddress.Text + "\r\n端口" + this.txtPort.Text + "\r\n停止监听......\r\n");
                }
                catch
                {
                    MessageBox.Show("监听尚未开始,关闭无效!");
                }
            }
            /// <summary>
            /// 线程同步的方法
            /// </summary>
            private void Listen()
            {
                myIP = IPAddress.Parse("10.0.64.103");            //myServer = new IPEndPoint(myIP,Int32.Parse(txtPort.Text));
                myServer = new IPEndPoint(myIP,Int32.Parse(30000));
                sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //myIP.s
                sock.Bind(myServer);
                sock.Listen(50);            //this.txtState.AppendText("主机" + this.txtAddress.Text + " 端口" + this.txtPort.Text + " 开始监听......\r\n");            while (true)
                {
                    recSock = sock.Accept();
                    if (recSock.Connected)
                    {
                        //this.txtState.AppendText("与客户建立连接\r\n信息\r\n");
                        Thread recThread = new Thread(new ThreadStart(Round));
                        recThread.Start();
                    }
                }
            }        /// <summary>
            /// 
            /// </summary>
            private void Round()
            {
                while (true)
                {
                    Byte[] rec = new Byte[64];
                    NetworkStream stream = new NetworkStream(recSock);
                    stream.Read(rec, 0, rec.Length);                string recMessage = System.Text.Encoding.BigEndianUnicode.GetString(rec);
                    //this.txtState.AppendText(recMessage + "\r\n");
                }
            }
        }
    }
    你要么在新建的线程当中不要直接操作界面上的控件,要么使用delegate进行操作啊参考
    http://topic.csdn.net/u/20071113/16/6CC43DD7-C876-47A9-9FB3-61BDE3F8354E.html
      

  9.   

    ls的兄弟,我是说 如果我这样 声明   private IPAddress myIP = new IPAddress("127.0.0.1")
    它这个时候已经报错了!
      

  10.   

            // 摘要:
            //     用指定为 System.Byte 数组的地址初始化 System.Net.IPAddress 类的新实例。
            //
            // 参数:
            //   address:
            //     IP 地址的字节数组值。
            public IPAddress(byte[] address);
            //
            // 摘要:
            //     用指定为 System.Int64 的地址初始化 System.Net.IPAddress 类的新实例。
            //
            // 参数:
            //   newAddress:
            //     IP 地址的长值。例如,Big-Endian 格式的值 0x2414188f 可能为 IP 地址“143.24.20.36”。
            public IPAddress(long newAddress);
            //
            // 摘要:
            //     用指定的地址和范围初始化 System.Net.IPAddress 类的新实例。
            //
            // 参数:
            //   scopeid:
            //     范围标识符的长值。
            //
            //   address:
            //     IP 地址的字节数组值。
            public IPAddress(byte[] address, long scopeid);
    IPAddress根本就没有传string的构造函数啊,你在对象浏览器中看看就知道了。
      

  11.   

    汗,不好意思,刚刚写错了
    应该是 private IPAddress myIP = IPAddress.Parese("127.0.0.1") ;
    这样的时候就报错了!
      

  12.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;namespace SocketListin
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private IPEndPoint myServer;
            private IPAddress myIP = IPAddress.Parse("127.0.0.1");//报错
            private Socket sock;
            private bool check = true;
            private Socket recSock;        /// <summary>
            /// 开始监听
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnStart_Click(object sender, EventArgs e)
            {
                //try
                //{
                   
                //}
                //catch
                //{
                //    MessageBox.Show("IP格式不正确,重新输入!");
                //    return;
                //}            try
                {
                    Thread thread = new Thread(new ThreadStart(Listen));
                    thread.Start();
                }
                catch (Exception ex)
                {                this.txtState.AppendText(ex.Message);
                }        }
            /// <summary>
            /// 发送信息
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnSend_Click(object sender, EventArgs e)
            {
                try
                {
                    Byte[] sendByte = new Byte[64];
                    string sendMsg = this.rchTxtSend.Text + "\r\n";
                    NetworkStream stream = new NetworkStream(recSock);
                    sendByte = System.Text.Encoding.BigEndianUnicode.GetBytes(sendMsg.ToCharArray());
                    stream.Write(sendByte, 0, sendByte.Length);
                }
                catch
                {
                    MessageBox.Show("连接尚未建立,无法发送!");
                    return;
                }
            }
            /// <summary>
            /// 停止监听
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnStop_Click(object sender, EventArgs e)
            {
                try
                {
                    sock.Close();
                    this.txtState.AppendText("主机" + this.txtAddress.Text + "\r\n端口" + this.txtPort.Text + "\r\n停止监听......\r\n");
                }
                catch
                {
                    MessageBox.Show("监听尚未开始,关闭无效!");
                }
            }
            /// <summary>
            /// 线程同步的方法
            /// </summary>
            private void Listen()
            {
                myIP = IPAddress.Parse("10.0.64.103");//报错,若继续执行则在Bind()处报错            myServer = new IPEndPoint(myIP,Int32.Parse(txtPort.Text));
                sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //myIP.s
                sock.Bind(myServer);
                sock.Listen(50);            this.txtState.AppendText("主机" + this.txtAddress.Text + " 端口" + this.txtPort.Text + " 开始监听......\r\n");            while (true)
                {
                    recSock = sock.Accept();
                    if (recSock.Connected)
                    {
                        this.txtState.AppendText("与客户建立连接\r\n信息\r\n");
                        Thread recThread = new Thread(new ThreadStart(Round));
                        recThread.Start();
                    }
                }
            }        /// <summary>
            /// 
            /// </summary>
            private void Round()
            {
                while (true)
                {
                    Byte[] rec = new Byte[64];
                    NetworkStream stream = new NetworkStream(recSock);
                    stream.Read(rec, 0, rec.Length);                string recMessage = System.Text.Encoding.BigEndianUnicode.GetString(rec);
                    this.txtState.AppendText(recMessage + "\r\n");
                }
            }
        }
    }
    这段代码很乱,暂时是没有意义的,但是我只是想指出错误的地方!
      

  13.   

    myIP = IPAddress.Parse("10.0.64.103");//ScopeId = “myIP.ScopeId”引发了“System.Net.Sockets.SocketException”类型的异常继续运行
    sock.Bind(myServer); //未处理SocketException   在其上下文中,该请求的地址无效。