本帖最后由 qcbf1 于 2011-04-26 15:59:22 编辑

解决方案 »

  1.   

    1.这样写只能用于1个客户端,1个服务器,多客户端就没办法了,这样改下(没有考虑服务器最大负载的情况下)
    while(!isstopped)
    {
    TcpClient client = tcpl.AcceptTcpClient();
    }
    2,这样只是接受了多客户端响应,还需要为每一个客户端分配一个线程,让他在线程中完成与客户端交互
    Thread user = new Thread(ClientStart);
    user.Start(client);
    3.读取客户端数据可以使用NetworkStream 的read方法,发送客户端信息可以使用NetworkStream 的write方法
    4.注意下,socket通讯需要自己定义好协议,芯跳协议也需要考虑和实现
    多读别人代码学习是个捷径,网上开源的socket服务器代码一大堆
      

  2.   

    去看看这个http://download.csdn.net/source/3037458服务端可设置同时并行处理线程数量,是我看过效率最高的UDP服务端代码了。
      

  3.   

    你可以去了解一下 完成端口 模型。直接用MSDN上的例子就非常好。
      

  4.   

    谢谢,哎,服务端和客户端都自己写感觉好那个什么.....
    现在还要找下flash安全策略方面的东西,写一个了安全策略文件但是还是无法和服务端交互..
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    //添加引用
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;
    namespace SocketServer
    {
        public partial class Form1 : Form
        {
            Thread mythread;//创建线程
            Socket socket;
            int port = 8000;//定义侦听端口号
            
            public Form1()//服务器端
            {
                InitializeComponent();        }       private void Form1_Load(object sender, EventArgs e)
            {
                //是跨线程操作
              Control.CheckForIllegalCrossThreadCalls = false;
            }        //获取本机IP地址
            public static IPAddress GetServerIP()
            {
                IPHostEntry ieh = Dns.GetHostEntry(Dns.GetHostName());
                return ieh.AddressList[0];
            }
            //监听
            private void BeginListen()
            {            
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                            
                IPAddress ServerIp = GetServerIP();
                IPEndPoint iep = new IPEndPoint(ServerIp, port);
                socket.Bind(iep);            toolStripStatusLabel1.Text = iep.ToString() + "正在监听...";
                byte[] byteMessage = new byte[100]; 
                while (true)
                {
                    try
                    {
                        socket.Listen(5);
                        Socket newSocket = socket.Accept();
                        newSocket.Receive(byteMessage);                    string sTime = DateTime.Now.ToShortTimeString();
                        string msg = sTime + "-" + "信息来自:";
                        msg += newSocket.RemoteEndPoint.ToString() +" "+ Encoding.Default.GetString(byteMessage).Trim(new char[] {'\0'});
                        this.richTextBox1.AppendText(msg + "\r\n");
                    }
                    catch (SocketException ex)
                    {
                        toolStripStatusLabel1.Text += ex.ToString();
                    }
                }            
            }
            private void btn_Listen_Click(object sender, EventArgs e)
            {
                try
                {
                    mythread = new Thread(new ThreadStart(BeginListen));
                    mythread.Start();
                }
                catch (System.Exception er)
                {
                    MessageBox.Show(er.Message, "完成", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            
        }
    }////////////////////////////////////////////////////////////////////////////////////
    //客户端
    using System;
    using System.Windows.Forms;
    //添加引用
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    namespace SocketClient
    {
        public partial class Form1 : Form
        {
            public Form1()//客户端
            {
                InitializeComponent();
            }       
            private void Form1_Load(object sender, EventArgs e)
            {
                //得到本机地址
                IPHostEntry ieh = Dns.GetHostEntry(Dns.GetHostName());
                txtIP.Text = ieh.AddressList[0].ToString();
            }
            private void btn_Send_Click(object sender, EventArgs e)
            {
                BeginSend();
            }
            //发送信息
            private void BeginSend()
            {
                string ip = txtIP.Text;
                string port = txtPort.Text;
                string msg = txtMsg.Text.Trim();
                
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            IPAddress serverIp = IPAddress.Parse(ip);
                int serverPort = Convert.ToInt32(port);
                IPEndPoint iep = new IPEndPoint(serverIp, serverPort);                        
                socket.Connect(iep);            byte[] byteMessage;
                byteMessage = Encoding.ASCII.GetBytes(msg);
                socket.Send(byteMessage);
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();            
            }        
        }
    }
      

  6.   

    嗯,flash 下使用 xmlsocket 是需要策略文件的
      

  7.   

    isstopped这个能说下吗?vs:不存在此名称