怎么给FLASH聊天室创建SOCKET服务器 本帖最后由 CHN_John 于 2010-04-20 19:40:07 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看这里吧,这里搞Flash的人很少。。http://www.dreaminginflash.com/wp-content/uploads/2008/05/socketexample.zip这个是一个Demo的下载地址。 c/s结构的语法我不是很明白,能介绍个B/S结构的吗?案例也可以的。 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高手看看,这个怎么改改呢- -有很大问题。。汗。提示:通常每个套接字地址(协议/网络地址/端口)只允许使用一次。 关于List小问题,高手们急求啊。。 c#本身有“连接池”功能,还需设计单例的连接对象吗? 通过按钮实现gridview的局部刷新 C# PictureBox内容清空问题 求图形拖动外接矩形放大缩小方法顶贴给分悬赏150分 有没有办法通过xaml直接创建窗口 SqlDataAdapter.Update(ds)添加数据的问题 动态控件回传留值的问题(有源代码),请求大侠的帮助 急,关于线程的问题 c# winform 重写button,让button变为椭圆的。 希望能推荐一本可以系统的自学C#的教材 TcpClient NetworkStream 中的TCP报文疑惑,请不吝指教
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高手看看,这个怎么改改呢- -
有很大问题。。汗。
提示:通常每个套接字地址(协议/网络地址/端口)只允许使用一次。