客户端是阻塞的!有一个线程专门接收服务端数据!用到的是clientsocket,具体的线程书写见http://expert.csdn.net/Expert/topic/2187/2187585.xml?temp=.3067896,现在我要用clientsocket向服务端发送数据,想问怎么才能实现这个功能,因为线程一直占用clientsocket这个资源,如果直接发送的话,就会出错!实在是对线程知道的不多,请大家帮帮我吧!

解决方案 »

  1.   

    这个帮助里 DEMO啊,可以看看,用SOCKETSTREAM来处理;
      

  2.   

    The following example shows a typical ClientExecute method for a custom descendant of TServerClientThread. It reads from the socket connection specified by ClientSocket using a thread-local instance of TWinSocketStream.procedure TMyServerThread.ClientExecute;var
      Stream : TWinSocketStream;
      Buffer : array[0 .. 9] of Char;
    begin
      { make sure connection is active }
      while (not Terminated) and ClientSocket.Connected do
      begin
        try
          Stream := TWinSocketStream.Create(ClientSocket, 60000);
          try
            FillChar(Buffer, 10, 0); { initialize the buffer }
            { give the client 60 seconds to start writing }
            if Stream.WaitForData(60000) then          begin
              if Stream.Read(Buffer, 10) = 0 then { if can抰 read in 60 seconds }
                ClientSocket.Close;               { close the connection }
              { now process the request }
              ...
            end
            else
              ClientSocket.Close; { if client doesn抰 start, close }
          finally
            Stream.Free;
          end;
        except
          HandleException;    end;
      end;
    end;
      

  3.   

    你可以另外创建一个clientsocket来发送数据嘛.
    这个接受数据,另外一个发送数据不就行了.
      

  4.   

    这个是客户端的THREAD;也就是CLIENTSOCKET阻塞方式需要的THREAD;
      

  5.   

    我并没有发现你怎么发给你的SERVERSOCKET的代码,在哪里?
      

  6.   

    按钮点击!
     FillChar( CApp_Head, SizeOf(VApp_Head), 0 );
              with CApp_Head do
              begin
                len:= htonl( 12 ); //longword; //消息的总长度(字节)
                CommandId := htonl(APP_ACTIVE); //longword; //命令ID
                seqno := htonl( CMPPLSH ); //longword; //序列号,循环使用,步长为1,范围1-0x7fffffff
              end;
               form1.clientsocket.socket.SendBuf(CApp_Head, SizeOf(VApp_Head));
      

  7.   

    我现在的做法是参照了http://expert.csdn.net/Expert/topic/1354/1354558.xml?temp=.3853266这个例子,但是我现在线程启了之后,再用socket向服务端发送数据的时候就会出错!不知道怎么处理!