clientsocket 阻塞的,怎么发送和读取数据?(100分)

  • 主题发起人 主题发起人 zhangaihua
  • 开始时间 开始时间
Z

zhangaihua

Unregistered / Unconfirmed
GUEST, unregistred user!
用线程是可以实现的吧,可是怎么来实现呢?谁能给我个简单的例子吗?谢谢了!!
 
找以前的代码,就是WaitForData()以后发送和接收数据,
 
没有找到,我找了几天了,不好意思!能告诉我在哪里吗?
 
全文检索怎么不好用了!告诉我一下吧!
 
TtestsendThread = class(TThread)
private
FQuery: TADoQuery;
{ 线程中的查询组件 }
FSocket:TClientSocket;
protected
procedure Execute;override;
public
constructor Create(CreateSuspended: Boolean);
virtual;
{ 线程构造器 }
end;
TtestreadThread = class(TThread)
private
FSocket:TClientSocket;
protected
procedure Execute;override;
public
constructor Create(CreateSuspended: Boolean);
virtual;
{ 线程构造器 }
end;
constructor TtestsendThread.Create(CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
self.FreeOnTerminate:=true;
FSocket:=TClientSocket.Create(Application);
FSocket.Host:='127.0.0.1';
FSocket.Port:=8801;
FQuery:=TADOQuery.Create(Application);
Fquery.ConnectionString:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=mybase;Data Source=zhangaihua';
end;

constructor TtestreadThread.Create(CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
self.FreeOnTerminate:=true;
FSocket:=TClientSocket.Create(Application);
FSocket.Host:='127.0.0.1';
FSocket.Port:=8801;
end;

procedure TtestsendThread.Execute;
begin
if WaitForSingleObject(hMutex,INFINITE)=WAIT_OBJECT_0 then
begin
try
FSocket.Active:=true;
if Fsocket.Active then
begin
CoInitialize(Nil);
FQuery.SQL.Clear;
FQuery.SQL.Add('select * from cmppwait');
FQuery.Open;
if FQuery.RecordCount>0 then
begin
FSocket.Socket.SendText('好好!');
end;
end;
finally
CoUninitialize;
end;
end;
ReleaseMutex(hMutex);
end;

procedure TtestreadThread.Execute;
begin
if WaitForSingleObject(hMutex,INFINITE)=WAIT_OBJECT_0 then
begin
FSocket.Active:=true;
if Fsocket.Active then
begin
showmessage(Fsocket.Socket.ReceiveText);
end;
end;
ReleaseMutex(hMutex);
end;
 
这是我写的接收和发送,我参照其他人的代码写的,实在是不懂,大家告诉我什么地方错了,我写的东西不对的,连我自己写的服务端连不上!
 
为什么到哪里都找不到答案呢!
 
FSocket必须手动设置为组塞方式才行,而且在线程里不能调用ShowMessage,除非在
同步方法里才能调用ShowMessage。否则一个AV会警告你的。
 
谢谢,我先试试
 
给你个最简单的:
TReadThread = class(TThread)
protected
RcvBuf: string;
procedure NotifyRead;
procedure Execute;
override;
end;

procedure TReadThread.Execute;
var
l: Integer;
begin
setlength(Rcvbuf, 1);
while truedo
begin
if recv(form1.clientsocket1.socket.sockethandle, Rcvbuf[1], 1, 0) = SOCKET_ERROR then
break;
if ioctlsocket(form1.clientsocket1.socket.sockethandle, FIONREAD, l)=SOCKET_ERROR then
break;
setlength(Rcvbuf, l+1);
if recv(form1.clientsocket1.socket.sockethandle, Rcvbuf[2], l, 0)=SOCKET_ERROR then
break;
synchronize(notifyread);
end;
end;

procedure TReadThread.NotifyRead;
begin
form1.memo1.lines.add(Rcvbuf);
end;
 
还是不行没连上,不知道怎么搞的!再帮我看看吧
FSocket:=TClientSocket.Create(Application);
FSocket.Host:='127.0.0.1';
FSocket.Port:=8801;
Fsocket.ClientType:=ctBlocking;
在这里加了这个了,我的线程里有问题吗!那个showmessage()我放在Synchronize里了!
 
我就是想实现的是服务端发过的来的东西能马上就接收!然后如果客户端有东西发的时候可以发出去!
 
我自己又看了一下发现是
FSocket.ADress:='127.0.0.1';后来就连上了,试了一下还可以,呵呵!
给分了!
 
怎么给分啊?
 
多人接受答案了。
 
后退
顶部