标题:求助:Socket多线程问题 急!!!
取消只看楼主
zzc0816
Rank: 1
等 级:新手上路
帖 子:61
专家分:7
注 册:2006-9-14
结帖率:0
 问题点数:0 回复次数:0 
求助:Socket多线程问题 急!!!
代码如下:
    为什么我的代码只能监听到一个端口,不能同时监听两个或者多个端口,谢谢!


using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Bosch.OpCon.DDL.Client.Xml;
using Bosch.OpCon.DDL.Xml;
using Bosch.
using Bosch.
using
using
using System.Threading;
using

namespace opconxml
{
    public partial class Form1 : Form
    {
        Thread myThread;
        Thread dataThread;

        public Form1()
        {
            InitializeComponent();
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            myThread = new Thread(new ThreadStart(Listen));
        }

        #region 监听
        public void Listen()
        {
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("10.172.6.147"), int.Parse("2005"));
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);   
            server.Bind(ipep);  
            server.Listen(10);   

             Socket client = server.Accept();
             IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
             byte[] data = new byte[1024];
             int SendedCount = 0;  
             while (true)  
             {  
                 client.Receive(data);
                 //string content = System.Text.Encoding.UTF8.GetString(data);
                 if (data.Length == 0)
                 {
                     break;
                 }
                 else
                 {
                     SendedCount++;
                     this.listView1.Items.Add("已发送包个数" + SendedCount.ToString());
                 }
             }
         }
        #endregion

         private void button1_Click(object sender, EventArgs e)
        {
            myThread.Start();
            button1.Enabled = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            myThread.Abort();
            button1.Enabled = true;
            button2.Enabled = false;
        }
    }
}
搜索更多相关主题的帖子: Socket 线程 
2010-12-10 08:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-328581-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.511694 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved