日期:2014-05-16  浏览次数:21828 次

c# socket重复接收
Socket socket_s = null;
        Socket socket_c = null;
        IPEndPoint ipe = null;
        public Form1()
        {
            InitializeComponent();
            ipe = new IPEndPoint(IPAddress.Any, 25);
            socket_s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                socket_s.Bind(ipe);
                socket_s.Listen(7);
            }
            catch (SocketException ex) {
                MessageBox.Show(ex.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listView2.Items.Add(new ListViewItem(new string[2]{DateTime.Now.ToShortTimeString(),"**** PublicMail v1.0 Build20140303 starts up ****"}));
            Thread thread_start = new Thread(new ThreadStart(start));
            thread_start.Start();

        }
        void start() {
            while (true) {
                socket_c= socket_s.Accept();
                Debug.WriteLine("Connect from:"+socket_c.RemoteEndPoint.ToString());
                Thread th = new Thread(new ThreadStart(receive));
                th.Start();
            }
        }
        void receive() {
            bool keepConnect = true;
            Socket temp = socket_c;
            byte[] buff=new byte[1024];
            while (keepConnect) {
        &n