T
tomyang
Unregistered / Unconfirmed
GUEST, unregistred user!
to fatbaby:
sorry!由于原来的程序比较的复杂,为了简化,因此没有贴出来。您发现的是贴的一个纰漏
sorry again.修改ID2为空应当是在while的后面,而不是在循环体内。
同样的sorry to winnt,你首先提出,而我没有意识到。下面是实际的源码:
function TSocket.QuestService( aTxt:string;var aReturn:string):integer;
var
sId:string;
sTxt:string;
pstruSocketstruSocketContent;
iSocketIndex:longint;
// ThreadWait:TThreadWait;
begin
//如果没有连接,进行连接
if not(CSocket.Active) then
CSocket.Active := true;
while not(CSocket.Active) do
begin
Application.ProcessMessages;
end;
sId := rightstring('********' + inttostr(QuestTimesCount+1),8); //生成ID1
//inc Counter and generate id,like Sequence in Oracle
IncQuestTimes(); //不要管他,功能是inc(QuestTimesCount)
sTxt := sId + aTxt; //发出
//保存发出的socket记录,在client接收事件中写SocketReceive,并为socketsReceiveBuf
//赋值。
new(pstruSocket);
pstruSocket^.SocketId := sId;
pstruSocket^.SocketSend := sTxt;
pstruSocket^.SocketReceive := '';
SocketsWaiting.Add(pstruSocket);
CSocket.Socket.SendText(sTxt); //clientSocket发送Socket
while socketsReceiveBuf <> sid do //等待接收
application.ProcessMessages ;
// EvnRead.ResetEvent ; //用事件等待,但是Waitfor不释放主线程,不成功)
// if EvnRead.WaitFor(300000)=wrSignaled then
// ThreadWait := TThreadWait.create(sid); //同样Thread.waitfor一样不释放主线程,还要processMessage.
// while not(threadWait.fExeFinish) do
// begin
// application.ProcessMessages ;
// end;
// threadwait.free();
// threadWait := nil;
socketsReceiveBuf := '' ; //相当与id2:=''
//获得返回的数据,赋给aReturn
iSocketIndex := getWaitingSocket(pstruSocket^.SocketId);
if iSocketIndex >= 0 then
begin
aReturn := pstruSocketContent(SocketsWaiting.items[iSocketIndex]).SocketReceive;
aReturn := midstring(aReturn , 9,length(aReturn)-8);
end;
result := 1;
end;
//接收事件:
procedure TSocket.ClientSocketRead(Sender: TObject;Socket: TCustomWinSocket);
var
sReceive:string;
sid:string;
sIndex:longint;
pstruSocketContent;
begin
sReceive:=Socket.ReceiveText;
sid :=leftstring(sReceive,8);
CSocket.Socket.SendText(sid+'receive');//不要管此句
//CSocket.Socket.Lock;
//等待上此对SocketsReceivebuf的处理
while SocketsReceivebuf<> '' do
application.ProcessMessages ;
//收到Server的回应
SocketsReceivebuf := 'locked';
sIndex:=GetWaitingSocket(sid);
p := pstruSocketContent(SocketsWaiting.Items[sIndex]);
p^.SocketReceive := sReceive;
socketsReceivebuf := sid;
CSocket.Socket.SendText(sid+'finish'); //不要管此句
//CSocket.Socket.Unlock ;
//EvnRead.SetEvent;
end;
help!h......
sorry!由于原来的程序比较的复杂,为了简化,因此没有贴出来。您发现的是贴的一个纰漏
sorry again.修改ID2为空应当是在while的后面,而不是在循环体内。
同样的sorry to winnt,你首先提出,而我没有意识到。下面是实际的源码:
function TSocket.QuestService( aTxt:string;var aReturn:string):integer;
var
sId:string;
sTxt:string;
pstruSocketstruSocketContent;
iSocketIndex:longint;
// ThreadWait:TThreadWait;
begin
//如果没有连接,进行连接
if not(CSocket.Active) then
CSocket.Active := true;
while not(CSocket.Active) do
begin
Application.ProcessMessages;
end;
sId := rightstring('********' + inttostr(QuestTimesCount+1),8); //生成ID1
//inc Counter and generate id,like Sequence in Oracle
IncQuestTimes(); //不要管他,功能是inc(QuestTimesCount)
sTxt := sId + aTxt; //发出
//保存发出的socket记录,在client接收事件中写SocketReceive,并为socketsReceiveBuf
//赋值。
new(pstruSocket);
pstruSocket^.SocketId := sId;
pstruSocket^.SocketSend := sTxt;
pstruSocket^.SocketReceive := '';
SocketsWaiting.Add(pstruSocket);
CSocket.Socket.SendText(sTxt); //clientSocket发送Socket
while socketsReceiveBuf <> sid do //等待接收
application.ProcessMessages ;
// EvnRead.ResetEvent ; //用事件等待,但是Waitfor不释放主线程,不成功)
// if EvnRead.WaitFor(300000)=wrSignaled then
// ThreadWait := TThreadWait.create(sid); //同样Thread.waitfor一样不释放主线程,还要processMessage.
// while not(threadWait.fExeFinish) do
// begin
// application.ProcessMessages ;
// end;
// threadwait.free();
// threadWait := nil;
socketsReceiveBuf := '' ; //相当与id2:=''
//获得返回的数据,赋给aReturn
iSocketIndex := getWaitingSocket(pstruSocket^.SocketId);
if iSocketIndex >= 0 then
begin
aReturn := pstruSocketContent(SocketsWaiting.items[iSocketIndex]).SocketReceive;
aReturn := midstring(aReturn , 9,length(aReturn)-8);
end;
result := 1;
end;
//接收事件:
procedure TSocket.ClientSocketRead(Sender: TObject;Socket: TCustomWinSocket);
var
sReceive:string;
sid:string;
sIndex:longint;
pstruSocketContent;
begin
sReceive:=Socket.ReceiveText;
sid :=leftstring(sReceive,8);
CSocket.Socket.SendText(sid+'receive');//不要管此句
//CSocket.Socket.Lock;
//等待上此对SocketsReceivebuf的处理
while SocketsReceivebuf<> '' do
application.ProcessMessages ;
//收到Server的回应
SocketsReceivebuf := 'locked';
sIndex:=GetWaitingSocket(sid);
p := pstruSocketContent(SocketsWaiting.Items[sIndex]);
p^.SocketReceive := sReceive;
socketsReceivebuf := sid;
CSocket.Socket.SendText(sid+'finish'); //不要管此句
//CSocket.Socket.Unlock ;
//EvnRead.SetEvent;
end;
help!h......