日期:2014-05-18  浏览次数:22199 次

c#写的socket程序总是卡死
最近要做c#课程设计,
实说c#没怎么学,就是对socket和多线程了解了一下。
现在在做练习,
写了示例程序,发现程序很慢,经常卡到死。
这是为什么呢??
服务端主要代码
C# code

   private void getipandhostname() {//获取本机的ip和hostname
            String hostInfo = Dns.GetHostName();
            System.Net.IPAddress addr; 
            addr = new System.Net.IPAddress(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address);
            this.ip = addr.ToString();
            this.hostname = Dns.GetHostName();
        }

        private void button2_Click(object sender, EventArgs e)//启动监听端口
        {
            this.getipandhostname();
            this.myip = IPAddress.Parse(this.ip);
            try
            {
                this.myserver = new IPEndPoint(myip, Int32.Parse("1111"));
                this.sesock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                this.sesock.Bind(this.myserver);//绑定端口
                this.listBox1.Items.Add("主机:"+this.hostname+";ip:"+this.ip+"开始监听端口1111");
                this.resock = this.sesock.Accept();//开始接受
                Thread thread = new Thread(new ThreadStart(targett));//启动另外一个线程来接受信息
                thread.Start();
            }
            catch (Exception te)
            {
                MessageBox.Show(te.Message);
            }
        }
        
        private void targett()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            if (this.resock.Connected)
            {
                this.listBox1.Items.Add("连接已建立!");
                while (flag)
                {
                    Byte[] r_data = new Byte[64];
                    this.resock.Receive(r_data, r_data.Length, 0);//接受数据
                    string str = System.Text.Encoding.BigEndianUnicode.GetString(r_data);
                    string temp=((IPEndPoint)resock.RemoteEndPoint).Address.ToString();
                    this.listBox1.Items.Add(temp+"\n");
                }
            }
        }


客户端主要代码,
服务端卡死,客户端也就连不上,这段代码算是没测试。
C# code

 private void button1_Click(object sender, EventArgs e)//建立连接
        {
            this.ip = this.textBox1.Text;
            this.myip = IPAddress.Parse(this.ip);
            this.myserver = new IPEndPoint(this.myip, Int32.Parse("22"));
            this.sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.sock.Connect(this.myserver);
            this.toolStripLabel1.Text = "与服务器建立联系";
            Thread thread = new Thread(new ThreadStart(targett));//启动另外一个线程来接受信息
            thread.Start();
        }

        private void targett() {//接受服务器返回的信息
            Control.CheckForIllegalCrossThreadCalls = true;
            while(flag){
                Byte[] r_data=new Byte[64];
                sock.Receive(r_data,r_data.Length,0);
                string str = System.Text.Encoding.BigEndianUnicode.GetString(r_data);
                this.toolStrip1.Text = str;
            }
        }

        private void button3_Click(object sender, EventArgs e)//发送消息
        {
            string first = "new.txt";
            string second = "contents";
            string thired = "end!";
            try { 
                Byte[] s_data=new Byte[64];
                string send = first;
                s_data = System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());
                sock.Send(s_data,s_data.Length,0);
            }catch(Exception te){
                MessageBox.Show(te.Message);
            }
        }


希望各位大神指点一二。

------解决方案--------------------