本帖最后由 CHN_John 于 2010-04-20 19:40:07 编辑

解决方案 »

  1.   

    看这里吧,这里搞Flash的人很少。。http://www.dreaminginflash.com/wp-content/uploads/2008/05/socketexample.zip这个是一个Demo的下载地址。
      

  2.   

    c/s结构的语法我不是很明白,能介绍个B/S结构的吗?案例也可以的。
      

  3.   


    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;public partial class FTactivity_FTServer : System.Web.UI.Page
    {
        #region 字段定义    /// <summary>
        /// 服务器程序使用的端口,默认为2900
        /// </summary>
        private int _port = 2900;    /// <summary>
        /// 接收数据缓冲区大小64K 
        /// </summary>
        private const int _maxPacket = 64 * 1024;    /// <summary>
        /// 服务器端的监听器
        /// </summary>
        private TcpListener _tcpl = null;    /// <summary>
        /// 保存所有客户端会话的哈希表
        /// </summary>
        private Hashtable _transmit_tb = new Hashtable();    #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                StartUP();
            }
            catch (Exception b)
            {
                Response.Write("\n服务器发生异常,消息:" + b.Message);
                Close();
            }
        }
            #region 服务器方法
            /// <summary>
            /// 启动监听
            /// </summary>
            public string StartUP()
            {
                IPAddress _ip = Dns.GetHostAddresses(Dns.GetHostName())[0];//可能有多个,此时的IP是本地IP
                _tcpl = new TcpListener(_ip, _port);
                _tcpl.Start();//开始侦听传入的连接请求。
                //提示
                //Response.Write("服务器已启动,正在监听...\n");
                //Response.Write(string.Format("服务器IP:{0}\t端口号:{1}\n", _ip, _port));
                while (true)
                {
                    byte[] packetBuff = new byte[_maxPacket];// 接收数据缓冲区大小64K
                    Socket newClient = _tcpl.AcceptSocket();
                    newClient.Receive(packetBuff);
                    string createname = Encoding.Unicode.GetString(packetBuff).TrimEnd('\0');
                    _transmit_tb.Add(createname, newClient);
                    Thread clientThread = new Thread(new ParameterizedThreadStart(ThreadFunc));
                    clientThread.Start(createname);
                }
            }
            /// <summary>
            /// 线程执行体,转发消息-------------------
            /// </summary>
            /// <param name="obj">传递给线程执行体的用户名,用以与用户通信</param>
            private void ThreadFunc(object obj)
            {
                //通过转发表得到当前用户套接字
                Socket clientSkt = _transmit_tb[obj] as Socket;
                while (true)
                {
                    try
                    {
                        byte[] _msgBuff = new byte[_maxPacket];
                        clientSkt.Receive(_msgBuff);
                        foreach (DictionaryEntry de in _transmit_tb)
                        {
                            string _clientName = de.Key as string;
                            Socket _clientSkt = de.Value as Socket;
                            if (!_clientName.Equals(obj))
                            {
                                _clientSkt.Send(_msgBuff);
                            }
                        }
                    }
                    catch (SocketException)
                    {
                        _transmit_tb.Remove(obj);
                        Thread.CurrentThread.Abort();
                    }
                }
            }
            /// <summary>
            /// 关闭监听器并释放资源
            /// </summary>
            public void Close()
            {
                if (_tcpl != null)
                {
                    _tcpl.Stop();//如果不为空 就停止 tcplistener
                }
                //关闭客户端连接并清理资源
                if (_transmit_tb.Count != 0)//客户端会话的哈希表
                {
                    foreach (Socket session in _transmit_tb.Values)
                    {
                        session.Shutdown(SocketShutdown.Both);
                    }
                    _transmit_tb.Clear();
                    _transmit_tb = null;
                }
            }
            #endregion高手看看,这个怎么改改呢- -
    有很大问题。。汗。
    提示:通常每个套接字地址(协议/网络地址/端口)只允许使用一次。