C#与Flash通信的服务端程序

来源:互联网 发布:如何登陆阿里云主机 编辑:程序博客网 时间:2024/05/06 00:40

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Collections;
using System.Threading;

namespace Win1
{
    public partial class Form1 : Form
    {
        private ArrayList clientArray;
        private Thread tThread;
        private Socket socket;       
        public Form1()
        {
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            IPEndPoint ipe = new IPEndPoint(ip, 5000);
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(ipe);
            socket.Listen(10);           
            clientArray = new ArrayList();
            InitializeComponent();
            tThread = new Thread(new ThreadStart(onThread));
            tThread.Start();
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (clientArray.Count == 0)
            {
                this.listBox1.Items.Add("没有客户端连接!");
            }
            else
            {
                string sendStr = textBox1.Text;
                sendStr = "服务器消息:" + sendStr;
                foreach (Socket sc in clientArray)
                {
                    try
                    {
                        byte[] bytes = Encoding.Default.GetBytes(sendStr);
                       
                        sc.Send(bytes);
                    }
                    catch (SocketException ex)
                    {
                        this.listBox1.Items.Add(ex.Message);
                    }
                }
                this.listBox1.Items.Add("<" + this.textBox1.Text + ">信息已群发!");
            }
                                
        }
        private void onThread()
        {
            try
            {
               
                while (true)
                {
                    Socket temp = socket.Accept();                   
                    string name;
                    if (temp != null)
                    {                                              
                        byte[] bytes=new byte[1024];
                        int i=temp.Receive(bytes);
                        name = Encoding.Default.GetString(bytes, 0, i);
                        if (name.IndexOf("<policy-file-request/>") >= 0)
                        {
                            string xml = "<cross-domain-policy> "
                                 + "<allow-access-from domain=/"*/" to-ports=/"5000/"/>"
                                 + "</cross-domain-policy>/0";
                            byte[] _bytes = Encoding.UTF8.GetBytes(xml);
                            temp.Send(_bytes);
                        }
                        else
                        {
                            clientArray.Add(temp);
                            Thread thread = new Thread(new ParameterizedThreadStart(_thread));
                            thread.Start(temp);
                            this.listBox1.Items.Add(name + "客户端连接进入!");
                            this.listBox2.Items.Add(name);
                        }
                    }
                   
                }
            }
            catch (SocketException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void _thread(object value)
        {
            Socket acceptSocket = (Socket)value;
            while (true)
            {
                byte[] buffer = new byte[1024];
                string host;
                try
                {
                    acceptSocket.Receive(buffer);
                    host = Encoding.Default.GetString(buffer);
                    host="客户端消息:"+host;
                    listBox1.Items.Add(host);
                    foreach (Socket item in clientArray)
                    {
                        item.Send(Encoding.Default.GetBytes(host));
                    }
                }
                catch
                {
                  
                }               
            }
        }
    }
}

原创粉丝点击