socket问题,请各位大侠帮忙,谢谢!!!(50分)

  • 主题发起人 主题发起人 mrdingsheng
  • 开始时间 开始时间
M

mrdingsheng

Unregistered / Unconfirmed
GUEST, unregistred user!
我的服务端程序如下:
Procedure TFileServerThread.ClientExecute;
Var
Data: Array[0..1023] Of char;
RecText: String;
SocketStream: TWinSocketStream;
Begin
While Not Terminated And ClientSocket.Connected Do
begin
Try
SocketStream := TWinSocketStream.Create(ClientSocket, 30000);
if SocketStream.WaitForData(60000) then
begin
Try
FillChar(Data, SizeOf(Data), 0);
If SocketStream.Read(Data, SizeOf(Data)) = 0 Then
Begin
ClientSocket.SendText('Timeout on Server'+#13#10);
sleep(1);
ClientSocket.Close;
Terminate;
End;
RecText := Data;
If Length(RecText) > 2 Then
Delete(RecText, Pos(#13#10, RecText), 2); // Delete #13#10
If ClientSocket.Connected Then
Begin
ClientSocket.SendText(RecText);
SendMessage(Form1.Listbox1.Handle, LB_ADDSTRING, 0, Integer(PChar(RecText)));
PostMessage(Form1.Handle, CM_INCCOUNT, 0, 0);
End;
Finally
SocketStream.Free;
End;
end;
Except
//HandleException;
//showmessage('ERROR!!!!!!!!');
ClientSocket.Close;
Terminate;
End;
end;
End;
但是当程序运行的时候,会在If SocketStream.Read(Data, SizeOf(Data)) = 0 Then这一句报错:指定的网路名不再可用。我看过原来的帖子说:把
Except
//HandleException;
//showmessage('ERROR!!!!!!!!');
ClientSocket.Close;
Terminate;
End
这一段程序中的HandleException去掉,我现在已经把它去掉了,但是还是在
If SocketStream.Read(Data, SizeOf(Data)) = 0这一句报同样的错误,是在不知道怎么回事了,请各位大侠帮忙,谢谢!!!
 
在SOCKET中尽量不要用If SocketStream.Read(Data, SizeOf(Data)) = 0这样的句子。

容易造成混乱。再把代码优化一下吧。
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1166187
 
后退
顶部