请帮忙看看这个段代码 这个是服务端的侦听部分
现在遇到的问题是
 
A客户端连接后 通信正常 然后不关闭程序A 连接也没Close
B客户端连接后 通信也正常 同样不关闭程序B 连接也没Close然后继续发送数据的话服务端就只能接收到B客户端的了 A客户端发送的数据接收不到了 但是A客户端确实是把数据发送出去了public static void Main (string[] args)
{
Console.WriteLine(ServerName);
Console.WriteLine(ip);
Console.WriteLine(port);

client = new TcpClient();

theadLisener=new Thread(new ThreadStart(ServerStart));
theadLisener.Start();
}

public static void ServerStart()
{
listener =new TcpListener(ip,port);
listener.Start();
Console.WriteLine("服务正在监听...");
while(true)
{
if (listener.Pending()) 
 {
client = listener.AcceptTcpClient();
// client= listener.AcceptSocket();
clientservice= new Thread(new ThreadStart(Clients));
clientservice.Start();
 }
}
}

public static void Clients()
{
Newonline = new RemoteClient(client);
}

解决方案 »

  1.   

    唉!那个共享的client变量是经不起稍微一点并发考验的。实际上你做任何并发程序,都应该对这类使用莫名奇妙共享变量的代码非常忌讳的!
      

  2.   

    也就是说 是共享的client 这个出问题了?
    难道 client 不能用来处理并发程序?还是别的?
      

  3.   

    就按照你的代码逻辑来说,我给你稍微展开一点说。你的client 应该是在while循环循环里定义的局部变量,为线程传送这个局部变量,而不能是一个定位在class范畴的共享变量。你应该确保线程方法执行时它得到的client变量值是上面 
        client = listener.AcceptTcpClient()
    执行时得到的局部对象,而不会因为线程启动的先后延迟而张冠李戴地把别的client当作自己的clieng。
      

  4.   

    但是我即使是用私有的 client
    这个问题也依然存在.
    本人新手.还请多见谅.请说的透彻一点
      

  5.   

    public static void Main (string[] args)
    {
    Console.WriteLine(ServerName);
    Console.WriteLine(ip);
    Console.WriteLine(port);

    theadLisener=new Thread(new ThreadStart(ServerStart));
    theadLisener.Start();
    }

    public static void ServerStart()
    {
    listener =new TcpListener(ip,port);
    listener.Start();
    Console.WriteLine("服务正在监听...");
    while(true)
    {
    if (listener.Pending()) 
     {
    TcpClient client = new TcpClient();
    client = listener.AcceptTcpClient();
    // client= listener.AcceptSocket();
    Newonline = new RemoteClient(client);
     }
    }
    }这样是我最开始的设计.因为也出这个问题.所以我才又改成那样
      

  6.   

    {
    TcpClient clientxxxxxx = listener.AcceptTcpClient();把clientxxxxxx 传给新线程   你的 Clients  也是全局的  也要改
      

  7.   

    你的代码随意变来变去,就很难确定你自己是否还调试过了。你的Main方法不会立刻结束吗?进程不会立刻结束吗?
      

  8.   

    没用过这个,就用过socket类的。不知道和这个有什么区别
      

  9.   

    +1 socket类很强大 我的tcp udp都是用socket写的
      

  10.   

    最开始我也准备用 socket 来写
    但是后来有人说 TcpCline 这个好用.我才决定用他的还是出现和先前同样的问题啊..求助
      

  11.   

    代码仅供参阅:1 我主攻的是vb.net 2 很多地方没有优化,程序比较粗糙 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Net.Sockets;namespace ConsoleApplication1
    {
        public class Program
        {
            static string ServerName = "TEST ServerName";
            static System.Net.IPAddress IPAddress = System.Net.IPAddress.Parse("127.0.0.1") ;
            static int PORT = 8182;
            static Thread theadLisener;
            static TcpListener listener;
            public static void Main(string[] args)
            {
                Console.WriteLine(ServerName);
                Console.WriteLine(IPAddress);
                Console.WriteLine(PORT);
                theadLisener = new Thread(ServerStart);
                //theadLisener.IsBackground = true;
                theadLisener.Priority = ThreadPriority.Highest;
                theadLisener.Start();
            }        public static void ServerStart()
            {
                listener = new TcpListener(IPAddress, PORT);
                listener.Start();
                Console.WriteLine("服务正在监听...");
                while (true)
                {
                    try
                    {
                        System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(AcceptTcpClient), listener.AcceptTcpClient());
                        
                    }
                    catch
                    {                }
                }
            }
            // <summary>
         // 返回发送字节
         // </summary>
        // <param name="type"></param>
         // <param name="datastr"></param>
        // <returns></returns>
         // <res></res>
        protected static Byte[] GetHTTPBytes(string data){
            System.Text.Encoding  encoder = System.Text.Encoding.UTF8;
            return encoder.GetBytes(String.Format("HTTP/1.1 200 OK{0}Date: {4}{0}Content-Length: {2}{0}Content-Type: {3}; charset=UTF-8{0}Server: Rayyu/{6}{0}Cache-Control: no-cache{0}Connection: {5}{0}{0}{1}", "\n", data, encoder.GetByteCount(data), "text/html", DateTime.Now.ToString("r"), "close", "1.0.0.1"));
        }
            public static void AcceptTcpClient(object state)
            {
                try
                {
                    Console.WriteLine("监听到请求,加入线程池...");
                    TcpClient client = (TcpClient)state;
                    string clientStr = "-";
                   using (NetworkStream clientStream = client.GetStream()){
                       //把字节流读入字节数组  
                       byte[] buffer = new byte[10240];
                       int length = clientStream.Read(buffer, 0, 10240);
                       clientStr = System.Text.Encoding.ASCII.GetString(buffer, 0, length);
                       //向客户端发送数据  
                       buffer = GetHTTPBytes("Hello Word!<br />Now:" + DateTime.Now.ToString());
                       clientStream.Write(buffer, 0, buffer.Length);
                       clientStream.Close();
                   }
                   Console.WriteLine("读到数据:" + clientStr);
                }
                catch 
                {
                }
            }
        }
    }
      

  12.   

    http://blog.csdn.net/zhaohad/article/details/6542335