看見還有很多人在研究flash的socket,其中經常會出現一些問題,所以將我以前寫的一個程序代碼拿出來給大家參考...
這是c#的代碼,經過測試的,本來想把源程序都放上來,可以我用的是vs2005(而且現在又壞了,系統出問題了),下面是程序的主要源代碼,不包含一些自動生成的代碼.這些代碼是根據一個開源的C#socket程序改編的,而且我已經寫了比較詳細的注釋了,如果你看了這些代碼還是發現有問題,可以向我索取完整的源程序:
[1b]把源文件傳上來,大家可以下載(gmail又打不開了,不能給留email的同學發了,自己下載吧):

點擊下載此文件
[/1b]
//--------------------------------
//---------------------------------------------------------------------------------------------------------------
//form1.cs
using System;
using System.IO;
using System.Drawing;
using System.Collections;//ArrayList引用到這個命名空間的類
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ChatServer//服務器端命名空間
{
?///
?/// Form1 的摘要說明。
?///
?public class Form1 : System.Windows.Forms.Form
?{
??private int listenport = 9050;//監聽端口
??private TcpListener listener;//監聽者
??private ArrayList clients;//所有的client
??private Thread processor;//處理線程
??private Socket clientsocket;//client套接字
??private Thread clientservice;//client的服務
??private System.Windows.Forms.ListBox lbClients;
??private System.Windows.Forms.Label label1;//顯示在線人員的List控件
??private TcpClient tclient;
??private NetworkStream ns;
??private System.Windows.Forms.Button button1;
??///
??/// 必需的設計器變量。
??///
??private System.ComponentModel.Container components = null;
??public Form1()
??{
???//
???// Windows 窗體設計器支持所必需的
???//
???InitializeComponent();
???Thread.CurrentThread.IsBackground = true;
???//
???// TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
???//
???clients = new ArrayList();//新建一個ArrayList類型用以存儲所有的client
???processor = new Thread(new ThreadStart(StartListening));//新建一個處理線程
???processor.Start();//線程開始
???//
??}
??///
??/// 清理所有正在使用的資源。
??///
??protected override void Dispose( bool disposing )
??{
??
????int c = clients.Count;
????for(int n=0; n
{
?????components.Dispose();
????}
???}
???base.Dispose( disposing );
??}
??///
??/// 開始監聽
??///
??private void StartListening()
??{
???IPAddress ipAddress = Dns.Resolve(Dns.GetHostName()).AddressList[0];
???//IPAddress ipAddress = IPAddress.Parse("192.168.0.132");
???
???label1.Text=ipAddress.ToString();
???IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, listenport);
???listener = new TcpListener(ipLocalEndPoint);
???listener.Start();
???while (true)
本文轉自:http://www.5uflash.com/flashjiaocheng/Flashyingyongkaifa/869.html
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
