套接字socket如何实现接收? ( 积分: 35 )

  • 主题发起人 主题发起人 nhlong320
  • 开始时间 开始时间
N

nhlong320

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠,我做的程序只能实现连接,就是不读卡;请大哥们开导开导,我就这些分了,大侠还是帮帮我
procedure TchatForm.connectItemClick(Sender: TObject);
var
Server:string;
ClickedOK: Boolean;
begin
if ClientSocket.Active then
ClientSocket.Active := False;
ClickedOK:=InputQuery('请输入主机名或IP','主机或IP',server);
if ClickedOK then
with ClientSocket do
begin
Host := Server;
Active := True;
ListenItem.Checked := False;
end;
end;//连接成功;
procedure TchatForm.FirmwareClick(Sender: TObject);
var
sData : array[0..4] of byte;
begin
sData[0]:=$5;
sData[1]:=$0;
sData[2]:=$0;
sData[3]:=216;
sData[4]:=147;
clientsocket.Socket.SendBuf(sData,5);
end;//给服务器发命令;(说明:服务器是一台读卡机)
procedure TchatForm.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo.Lines.Add(Socket.ReceiveText);
end;//接收数据;
可是不能接收数据,大侠帮帮我
 
各位大侠,我做的程序只能实现连接,就是不读卡;请大哥们开导开导,我就这些分了,大侠还是帮帮我
procedure TchatForm.connectItemClick(Sender: TObject);
var
Server:string;
ClickedOK: Boolean;
begin
if ClientSocket.Active then
ClientSocket.Active := False;
ClickedOK:=InputQuery('请输入主机名或IP','主机或IP',server);
if ClickedOK then
with ClientSocket do
begin
Host := Server;
Active := True;
ListenItem.Checked := False;
end;
end;//连接成功;
procedure TchatForm.FirmwareClick(Sender: TObject);
var
sData : array[0..4] of byte;
begin
sData[0]:=$5;
sData[1]:=$0;
sData[2]:=$0;
sData[3]:=216;
sData[4]:=147;
clientsocket.Socket.SendBuf(sData,5);
end;//给服务器发命令;(说明:服务器是一台读卡机)
procedure TchatForm.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo.Lines.Add(Socket.ReceiveText);
end;//接收数据;
可是不能接收数据,大侠帮帮我
 
两个问题,
一.open或Active:=true;并非立即就连接上了,
要么等一下,要么:
procedure TForm2.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo1.Lines.Add('connected successful');
end;

二.读卡机收到的是HEX码,发回的是TEXT吗?
SENDBUF/RECEIEVETEXT
 
谢谢大哥,大哥你会VB吗?这是VB的一段代码,可以实现读卡,可是我不会VB,你能不能给我改成delphi
Private Sub sckMpr_DataArrival(ByVal bytesTotal As Long)
'接收MPR2010返回数据
Dim sRec() As Byte
Dim nLen As Integer
Dim sRecStr As String
Dim i As Integer

sckMpr.GetData sRec
nLen = UBound(sRec)
For i = 0 To nLen
sRecStr = sRecStr & Hex(sRec(i))
Next i
txtGetData.Text = sRecStr
End Sub
谢谢大哥!!!万分感激
 
照VB的代码看来,返回的是十六进制代码,你改一下这个函数:
procedure TchatForm.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
var
p,p1:PByte;
n:integer;
s:string;
begin
p:=AllocMem(Socket.ReceiveLength);

//接收
Socket.ReceiveBuf(p,Socket.ReceiveLength);

s:='';
p1:=p;
for n:=0 to Socket.ReceiveLength-1 do
begin
s:=s+IntToHex(p1^,2)
Inc(p1);
end;
Memo.Lines.Add(s);
FreeMem(p);
end;//
 
这位大哥,您给我的代码真好,只是只有去掉
Inc(p1);
才能运行,再帮帮我处理一下,太感激你了!!
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部