//IPAddress ipa = IPAddress.Parse(host);///将IP字符串转换为IP地址的实例
            IPEndPoint ipe = new IPEndPoint(IPAddress.Any, port);//将网络端点表示为ip地址和端口号
            so.Bind(ipe);
            so.Listen(10);
            while (true)
            {
                try
                {
                    // 线程定义
                    Thread td = new Thread(socthread);
                    td.SetApartmentState(ApartmentState.STA);
                    td.Start();
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.ToString ());
                    return;
                }
            }
        }
        public void socthread()
        {
 
                Socket newsoc = so.Accept();//为新连接创建新的socket
                string streceive = string.Empty;
                byte[] breceive = new byte[1024];
                int bytes;
                bytes = newsoc.Receive(breceive, breceive.Length, 0);
                streceive = Encoding.ASCII.GetString(breceive, 0, bytes);
                //string stsend = "yes";
                //byte[] bsend = Encoding.ASCII.GetBytes(stsend);
                //newsoc.Send(bsend, bsend.Length, 0);
                newsoc.Close();
                so.Close();
                if (streceive != "")
                {
                    try
                    {
                        button();
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.ToString());
                        return;
                    }
                }
        }帮帮忙啊,有人吗

解决方案 »

  1.   

    你的问题和 “Application.Run(new Form1());” 无关,请明确所贴代码的用意。
      

  2.   

    根据你提示的错误信息,你WinForm窗体下有Form1这个窗体没有?如果没有的话,你的Program.cs文件中 
    Application.Run(new Form1());就有问题了,找不着入口了。
      

  3.   

    我在弄一个socket  服务端。 在FORM1 弄了一个BUTTON 。然后线程里会调用BUTTON  。然后FORM2。消息框弹出
    namespace form_service
    {
        public partial class Form1 : Form
        {
            // socket 定义
            private int port = 9050;//端口
            //private string host = "192.168.30.111";
            Socket so = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//socket 定义
            private delegate void bt();
            public Form1()
            {
                InitializeComponent();
            }
            public void Form1_Load(object sender, EventArgs e)
            {
                //IPAddress ipa = IPAddress.Parse(host);///将IP字符串转换为IP地址的实例
                IPEndPoint ipe = new IPEndPoint(IPAddress.Any, port);//将网络端点表示为ip地址和端口号
                so.Bind(ipe);
                so.Listen(10);
                while (true)
                {
                    try
                    {
                        // 线程定义
                        Thread td = new Thread(socthread);
                        //td.SetApartmentState(ApartmentState.STA);
                        td.Start();
                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show(ex.ToString ());
                        return;
                    }
                }
            }
            public void socthread()
            {
                Socket newsoc = so.Accept();//为新连接创建新的socket
                string streceive = string.Empty;
                byte[] breceive = new byte[1024];
                int bytes;
                bytes = newsoc.Receive(breceive, breceive.Length, 0);
                streceive = Encoding.ASCII.GetString(breceive, 0, bytes);
                string stsend = "yes";
                byte[] bsend = Encoding.ASCII.GetBytes(stsend);
                newsoc.Send(bsend, bsend.Length, 0);
                newsoc.Close();
                so.Close();
                if (streceive != "")
                {
                    try
                    {
                        button();
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.ToString());
                        return;
                    }
                }
            }
            private static void tsend()
            { 
                
            }
            private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                exit();
            }
            public void button1_Click(object sender, EventArgs e)
            {
                Form2 form2 = new Form2();
                form2.HeightMax = 150;
                form2.WidthMax = 200;
                form2.ScrollShow();
            }
            private void exit()
            {
                if (MessageBox.Show("您确定要退出错误提示程序吗?", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    so.Close();
                    this.Close();
                }
            }
            // 委托
            private void button()
            {
                if (this.button1.InvokeRequired)
                {
                    bt bt1 = new bt(button);
                    button1.Invoke(bt1);
                }
                else
                {
                    this.button1.PerformClick();
                    System.Windows.Forms.Application.DoEvents();
                }
            }