能不能给个成功的监听例子。
我对 win form 不熟。
谢谢

解决方案 »

  1.   

    System.Threading.Thread x = new Thread(new ThreadStart(Listen));
    x.Start();
      

  2.   

    哈哈哈哈哈!
    我成功了。
    shout
      

  3.   

    .net 的winform 还是旧式的消息循环,主消息队列一旦开始socket 监听就会被挂起,处于待被推动状态,所以建议把这些都移到子线程去完成,然后listen 之后的数据交互部分
      

  4.   

    然后listen 之后的数据交互部分 中的“然后”应为“包括”晕,老毛病见谅
      

  5.   

    BoardThread()
    {
      thread = new Thread(new ThreadStart(this,threadproc));
      thread->IsBackground = true;
      thread->Start();
    }
    void threadproc()
    {
    while(1)
    {
      Int32 port = 13000;
      IPAddress* localAddr = IPAddress::Parse(S"10.5.12.17");
      TcpListener* listenerServer = new TcpListener(localAddr,port+i16BoardNum);
      try
      {
    listenerServer->Start();
    TcpClient* client = listenerServer->AcceptTcpClient();
    NetworkStream* stream = client->GetStream();
    Byte buf[] = new Byte[10];
    while(1)
             {
    int i = stream->Read(buf,0,1);
    if(0 == i)
    throw new Exception("连接意外关闭");
    DataQueue->Enqueue(__box(buf[0]));
    }
        }
        catch(Exception* e)
       {}
      

  6.   

    using System;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;public class server
    {
        public static void Main() 
        {
            try 
            {
                // 把IP地址转换为IPAddress的实例
                IPAddress ipAd = IPAddress.Parse("127.0.0.1");
                
                // 初始化监听器, 端口为8001
                TcpListener myList=new TcpListener(ipAd,8001);
                
                // 开始监听服务器端口
                myList.Start();            // 输出服务器启动信息
                Console.WriteLine("在8001端口启动服务...");
                Console.WriteLine("本地节点为:" + myList.LocalEndpoint );
                Console.WriteLine("等待连接.....");
                
                // 等待处理接入连接请求
                // 新建立的连接用套接字s表示
                Socket s=myList.AcceptSocket();
                Console.WriteLine("连接来自 "+s.RemoteEndPoint);
                
                // 接收客户端信息
                byte[] b=new byte[100];
                int k=s.Receive(b);
                Console.WriteLine("已接收...");
                for (int i=0;i<k;i++)
                {
                    Console.Write(Convert.ToChar(b[i]));
                }
                
                // 处理客户端请求,给客户端回应
                ASCIIEncoding asen=new ASCIIEncoding();
                s.Send(asen.GetBytes("The string was recieved by the server."));
                Console.WriteLine("\n已发送回应信息");
                
                // 善后工作,释放资源
                s.Close();
                myList.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
        }
    }
      

  7.   

    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Net.Sockets;public class client 
    {
        public static void Main() 
        {
            try 
            {
                // 新建客户端套接字
    TcpClient tcpclnt = new TcpClient();
    Console.WriteLine("连接.....");

                // 连接服务器
    tcpclnt.Connect("127.0.0.1",8001);
    Console.WriteLine("已连接");
    Console.Write("请输入要传输的字符串 : ");

                // 读入字符串
    String str=Console.ReadLine();            // 得到客户端的流
    Stream stm = tcpclnt.GetStream();

                // 发送字符串
    ASCIIEncoding asen= new ASCIIEncoding();
    byte[] ba=asen.GetBytes(str);
    Console.WriteLine("传输中.....");
    stm.Write(ba,0,ba.Length);

                // 接收从服务器返回的信息
    byte[] bb=new byte[100];
    int k=stm.Read(bb,0,100);

                // 输出服务器返回信息
                for (int i=0;i<k;i++)
                {
                    Console.Write(Convert.ToChar(bb[i]));
                }

                // 关闭客户端连接
    tcpclnt.Close();
    }
    catch (Exception e) 
            {
    Console.WriteLine("Error..... " + e.StackTrace);
    }
    }
    }
      

  8.   

    http://expert.csdn.net/Expert/TopicView1.asp?id=1908930