全部代码如下,运行
void button1_Click_1(object sender, EventArgs e)
后,程序会死掉.
请问是什么原因?
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.Threading;
using System.Net.Sockets;namespace Win_Talk
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }        #region//定义变量
        IPAddress HostIP = IPAddress.Parse("10.58.1.181");
        IPEndPoint point;
        Socket socket;
        bool flag = true;
        Socket acceptedSocket;
        #endregion        #region//声名委托
        delegate void SetTextCallback(string text);
        private void SetText(string text)
        {
            textBox2.AppendText(text + "\r\n");
        }
        #endregion        #region//进程方法
        private void Proccess()
        {
            if (acceptedSocket.Connected)
            {
                while (flag)
                {
                    byte[] receiveByte = new byte[64];
                    acceptedSocket.Receive(receiveByte, receiveByte.Length, 0);
                    string strInfo = Encoding.BigEndianUnicode.GetString(receiveByte);
                    this.Invoke(new SetTextCallback(SetText), new object[] { strInfo });                }
            }
        }
        #endregion        private void button3_Click(object sender, EventArgs e)
        {
            socket.Close();
            acceptedSocket.Close();
        }        private void button2_Click_1(object sender, EventArgs e)
        {
            try
            {
                Byte[] sendByte = new Byte[64];
                string sendStr = this.textBox1.Text + ":" + this.textBox3.Text + "\r\n";
                sendByte = Encoding.BigEndianUnicode.GetBytes(sendStr.ToCharArray());
                acceptedSocket.Send(sendByte, sendByte.Length, 0);
            }
            catch { }
        }        private void button1_Click_1(object sender, EventArgs e)
        {
            HostIP = IPAddress.Parse("10.58.1.181");
            try
            {
                point = new IPEndPoint(HostIP, Int32.Parse("11000"));
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Bind(point);
                socket.Listen(100);
                acceptedSocket = socket.Accept();        //*******************就是这一句有问题********************
                Thread thread = new Thread(new ThreadStart(Proccess));
                thread.Start();
                MessageBox.Show("success");
            }
            catch (Exception ey)
            {
                MessageBox.Show(ey.Message);
            }
        }
    }
}

解决方案 »

  1.   

    acceptedSocket = socket.Accept();        一直等到有连接近来以后程序才会有反映,否则一直阻塞到超时.
    改用异步或者后台线程吧.参考这个贴中,最后三贴我的回复
    http://topic.csdn.net/u/20080217/12/9f552fa5-66aa-4b2a-9595-297ba022d9fb.html
      

  2.   


    acceptedSocket = socket.Accept();
    if(acceptSocket == Null)
    {
      return;
    }
      

  3.   

    用异步调用啊!
    非异步调用的线程会一直等待cli_sSocket.BeginAccept(new AsyncCallback(AcceptConnection), cli_sSocket);然后自写AcceptConnection事件