请教:
 我在线程类中动态生成一个clientsocket,其Onread事件指向我自己定义的函数;
 大概代码如下:
***********************************************************************************
procedure TSPConn.Execute;
begin
  SubmitFlag := false;
  if OpenSocket then
   begin
     SubmmitLoginMSG;
   end;
  while true do
   begin
     if SubmitFlag then
        SendMSG;
     application.ProcessMessages;
     sleep(10);
   end;
end;//打开socket连接
function TSPConn.OpenSocket: boolean;
var
  ETime : TDateTime;
begin
  SPClientSock := TClientSocket.Create(nil);
  SPClientSock.Address := mPara.ISMG.ISMGAddress;
  SPClientSock.Port := mPara.ISMG.ISMGPort;
  SPClientSock.Name := mPara.CSocketName;
  SPClientSock.OnError := MySocketError;
  SPClientSock.OnRead :=  MySocketRead;
  ETime := now;
  try
   SPClientSock.Open;
   while (now-ETime)< 5/86400 do
    begin
        if SPClientSock.Active then
          break;
        application.ProcessMessages;
    end;
  except
   on e: exception do
    begin
       DisplayMSG('Socket exception'+#13+#10+'    Open failed!!');
        exit;
    end;
  end;
  if SPClientSock.Active then
    result := true
  else
    result := false;end;
//自定义ClientSocket的Onread事件
procedure TSPConn.MySocketRead(Sender: TObject;  Socket: TCustomWinSocket);
var
    ReadBufSize: integer;
    
begin
    
   {数据处理}
 
end;
********************************************************************************************当我连接由delphi编写的server端程序时,clientsocket收发数据一切正常;当我连接由其他语言(VC++等)编写的server端程序时,clientsocket发送数据正常,而无法接收数据。
百思不得其解,请各位高手帮忙!!!!!!

解决方案 »

  1.   

    那可能是其它语言的Server程序的问题, 我的客户端和Linux下面的服务器链接都没有问题, 看你的代码好像是连接移动的短信网关
      

  2.   

    to postren:
     是的!
     但如果我把clientsocket放到主线程中就一切正常,很奇怪!
      

  3.   

    CSDN好几个贴子都问的是把在非主线程中使用非阻塞方式CLIENTSOCKET。。TCLIENTSOCKET是工作在WINDOWS异步选择IO方式下,它是要依赖WINDOWS消息的。
    WINDOWS消息是在同一线程中传递的(一般只是在主线程处理)。
      

  4.   

    在线程里最好不要用ctNonBlocking,
    Set ClientType to ctBlocking to force all reading and writing to occur synchronously. It is a good idea to include the client socket object in a thread if the ClientType is ctBlocking, so that I/O does not block all execution within the client application. When ClientType is ctBlocking, use a TWinSocketStream object for reading and writing. TWinSocketStream prevents the application from hanging indefinitely if a problem occurs while reading or writing. It also can wait for the socket connection to indicate its readiness for reading.