通过TCP/IP的client端口向server端口传送数据后如何接收到server端的反馈信息? 小弟现在正在做一个用HTTP协议向SERVER服务器端口传数据的程序,为了测试需要得到SERVER服务器端的反馈信息以查看发送数据是否成功,即是通过TCP/IP的client端口向server端口传送数据后如何接收到server端的反馈信息?望各位大侠帮助指教,万分感谢!!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你用什么方法去发数据包的呀?是API,还是什么控件。要用HTTP,有很多封装HTTP协议的控件可用。 TcpClient(sockets)和TcpServer(sockets) TcpClientSocket 有OnRead事件,有数据过来会触发,ClientSocket1.Socket.ReceiveBuf() 用indy组件logininfo:=TStringlist.Create;logininfo.Add('action="UserLogin.aspx"');logininfo.Add('CSDNUserLogin:tb_UserName=abcd');logininfo.Add('password=abcd');logininfo.Add('submit=登录');IdHTTP1.Post('http://www.csdn.net',logininfo,Response);Memo1.Lines.Text:=Response.DataString;Response就是返回的数据 如果你想用SOCKET,去学习http协议吧 我就是用的http协议,数据可以发出去,但接收不到反馈包,不知中间哪步出了问题。 在TClientSocket的OnRead事件接收返回的数据。 给你一个例子(未对返回值进行完善的处理):unit Unit1;//Powered By Jadeluo, 2005/03/09interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ScktComp, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; ClientSocket1: TClientSocket; procedure Button1Click(Sender: TObject); procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket); procedure ClientSocket1Connect(Sender: TObject; Socket: TCustomWinSocket); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);begin ClientSocket1.OnConnect := ClientSocket1Connect; ClientSocket1.OnRead := ClientSocket1Read; Memo1.Lines.Clear;end;procedure TForm1.Button1Click(Sender: TObject);begin ClientSocket1.Host := 'www.csdn.net'; ClientSocket1.Port := 80; ClientSocket1.Active := True;end;procedure TForm1.ClientSocket1Connect(Sender: TObject; Socket: TCustomWinSocket);var sURL : String; iSendLength: Integer; bOutput : array of Byte; iLoop : Integer;begin sURL := 'GET http://www.csdn.net/index.htm HTTP/1.0'#$0D#$0A#$0D#$0A; iSendLength := Length(sURL); SetLength (bOutput, iSendLength); for iLoop := 0 to iSendLength - 1 do bOutput[iLoop] := ord(sURL[iLoop + 1]); try Socket.SendBuf(bOutput[0], iSendLength); except end;end;procedure TForm1.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);var iCount : Integer; bBuf : array of Byte; sResult: String; iLoop : Integer;begin iCount := Socket.ReceiveLength(); if iCount > 0 then begin SetLength (bBuf, iCount); try Socket.ReceiveBuf (bBuf[0], iCount); except end; sResult := ''; for iLoop := 0 to iCount - 1 do sResult := sResult + chr(bBuf[iLoop]); Memo1.Lines.Add(sResult); end;end;end. 请教关于类型对应问题。 fastreport的打印问题(分组并分栏)急急!!! 一个Gauge1进度条的问题? 使用ADO访问SQLServer问题,大家可以试试看。 在线等,谢谢 各位大虾,帮我看看这个函数有什么错误啊? 关于Delphi的异常处理问题 在注册表Run中启动的监控程序不起作用 请高手估算一下此软件的开发周期,高分送上 wwDBGrid 在编辑状态不能修改特定的记录 ExpressPrinting System高手请进!帮顶有份,不够另贴加分 IntraWeb全程制作BBS,提供所有资源及源代码!
logininfo:=TStringlist.Create;
logininfo.Add('action="UserLogin.aspx"');
logininfo.Add('CSDNUserLogin:tb_UserName=abcd');
logininfo.Add('password=abcd');
logininfo.Add('submit=登录');
IdHTTP1.Post('http://www.csdn.net',logininfo,Response);
Memo1.Lines.Text:=Response.DataString;Response就是返回的数据
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
ClientSocket1: TClientSocket;
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
ClientSocket1.OnConnect := ClientSocket1Connect;
ClientSocket1.OnRead := ClientSocket1Read;
Memo1.Lines.Clear;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ClientSocket1.Host := 'www.csdn.net';
ClientSocket1.Port := 80;
ClientSocket1.Active := True;
end;procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
var
sURL : String;
iSendLength: Integer;
bOutput : array of Byte;
iLoop : Integer;
begin
sURL := 'GET http://www.csdn.net/index.htm HTTP/1.0'#$0D#$0A#$0D#$0A;
iSendLength := Length(sURL);
SetLength (bOutput, iSendLength);
for iLoop := 0 to iSendLength - 1 do
bOutput[iLoop] := ord(sURL[iLoop + 1]);
try
Socket.SendBuf(bOutput[0], iSendLength);
except
end;
end;procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
iCount : Integer;
bBuf : array of Byte;
sResult: String;
iLoop : Integer;
begin
iCount := Socket.ReceiveLength();
if iCount > 0 then
begin
SetLength (bBuf, iCount);
try
Socket.ReceiveBuf (bBuf[0], iCount);
except
end;
sResult := '';
for iLoop := 0 to iCount - 1 do
sResult := sResult + chr(bBuf[iLoop]);
Memo1.Lines.Add(sResult);
end;
end;end.