我现在在写一个关于TCP的程序,服务器端是一个嵌入式的系统,用的是44B0,客户端是一个C#程序,现在遇到了如果多个C#程序对服务器进行请求,服务器端需要同时处理多个客户端连接和命令,由于下位机资源有限,不能创建多个线程,服务器端该如何做,如有提供C#解决方案也可以!(两边都是使用SOCKET套接字)

解决方案 »

  1.   

    建议使用异步处理的方式,不要为每一个连接开启线程。但首先要知道的就是,你的Server对命令请求的实时性要求是多少,还有的就是你的Client的连接密度是多少?如果实时性不高,或者连接密度不高,采用异步等候的方式是比较好的,这是用时间换空间。
      

  2.   

    那就单线程也可以做,不过TCP连接就发挥不出优势了。
    看看MSDN在TCP异步连接(服务器)上的例程吧,很简单的说。
      

  3.   

    恩,用异步吧,用一个主套接字来调度服务.
    Socket clientSoc = _mainSoc.EndAccept(ar);
    //创建一个新的套接字clientSoc来处理传入连接 ......
    _mainSoc.BeginAccept(_acceptCallback,null);
    //置主套接字继续等待别的传入连接
      

  4.   

    http://dev.csdn.net/develop/article/65/65798.shtm
    用这个做.
      

  5.   

    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.IO;namespace Server
    {
    class Class1
    {
    [STAThread]
    static void Main(string[] args)
    {
    string line;
    IPEndPoint ipep=new IPEndPoint(IPAddress.Any,5000);
    TcpListener listener=new TcpListener(ipep);
    listener.Start();
    Console.WriteLine("server...."); TcpClient client=listener.AcceptTcpClient();
    NetworkStream ns=client.GetStream();
    StreamReader sr=new StreamReader(ns);
    StreamWriter sw=new StreamWriter(ns);
    sw.WriteLine("Welcom to my server");
    sw.Flush();
    while(true)
    {
    try
    {
    line=sr.ReadLine();
    Console.WriteLine(line);
    }
    catch(Exception e)
    {
    Console.WriteLine(e.Message);
    break;
    }
    sw.WriteLine(line);
    sw.Flush();
    }
    sw.Close();
    sr.Close();
    ns.Close();
    listener.Stop();
    }
    }
    }
      

  6.   

    http://dev.csdn.net/develop/article/65/65798.shtm