用一个容器,比如Vector,作为服务器类的一个成员变量,以记录服务器所生成的对应于每个客户的监听socket对象,这些监听socket对象在单独的线程中运行,服务器类实现信息分派。为了实现多线程,设计一个线程类,将socket监听对象放到该线程中运行。

解决方案 »

  1.   

    // server
    main thread:
      monitor incoming connection
      allocate buff and create/start new communicating threads
    communicating thread_read:
      monitor incoming message(input = userx:message)
      based on userx, write to buff[x]
      buff[x].notify
    communicating thread_write
      buff[?].wait()
      write(buff[?])// client
    write:
      get user input
      write to socket
    read:
      get socket input
      update UI.
      

  2.   

    设计一个类似连接表一样的东西,里面放入可用线程,有用户socket就掉一个,并将head指向下一个,当用完后将其重新存入表里,这是书上的一种方法。
      

  3.   

    jackal81(热带北极熊):哪本书有这样的解决方案?