delphi编程:用socket连接的问题
客户端:unitunit1;interfaceusesWindows,Messages,SysUtils,Classes,Graphics,Controls,Forms,...
客户端:
unit unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls, ExtCtrls,jpeg;
type
TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
Edit1: TEdit;
ClientSocket1: TClientSocket;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
c:longint;
m:tmemorystream;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
try
clientsocket1.Close;
clientsocket1.Host:=edit1.text;
clientsocket1.Open; //连接服务端
except
showmessage(edit1.text+#13#10+'未开机或未安装服务程序');
end;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
showmessage(edit1.text+'通讯成功!');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if button2.enabled then
begin
clientsocket1.Socket.SendText('gets');
end;
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
caption:='连接'+edit1.text+'失败';
showmessage(edit1.text+#13#10+'未开机或未安装服务程序');
errorcode:=0;
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
buffer:array [0..10000] of byte;
len:integer;
ll:string;
b:tbitmap;
j:tjpegimage;
begin
if c=0 then
begin
ll:=socket.ReceiveText;
c:=strtoint(ll);
clientsocket1.Socket.SendText('okok');
end else
begin
len:=socket.ReceiveLength;
socket.ReceiveBuf(buffer,len);
m.Write(buffer,len);
if m.Size>=c then
begin
m.Position:=0;
b:=tbitmap.Create;
j:=tjpegimage.Create;
try
j.LoadFromStream(m);
b.Assign(j); //将JPG转为BMP
Image1.Picture.Bitmap.Assign(b);
finally
b.free;
j.free;
clientsocket1.Active:=false;
clientsocket1.Active:=true;
m.Clear;
c:=0;
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
m:=tmemorystream.Create;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
m.free;
ClientSocket1.Close;
end;
end.
为什么点击“连接”按钮之后,没有出现“通讯成功”的提示框?哪里出了问题??谢谢!
连接已经弄好了。。但是截图不成功。。。为什么? 展开
unit unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls, ExtCtrls,jpeg;
type
TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
Edit1: TEdit;
ClientSocket1: TClientSocket;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
c:longint;
m:tmemorystream;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
try
clientsocket1.Close;
clientsocket1.Host:=edit1.text;
clientsocket1.Open; //连接服务端
except
showmessage(edit1.text+#13#10+'未开机或未安装服务程序');
end;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
showmessage(edit1.text+'通讯成功!');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if button2.enabled then
begin
clientsocket1.Socket.SendText('gets');
end;
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
caption:='连接'+edit1.text+'失败';
showmessage(edit1.text+#13#10+'未开机或未安装服务程序');
errorcode:=0;
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
buffer:array [0..10000] of byte;
len:integer;
ll:string;
b:tbitmap;
j:tjpegimage;
begin
if c=0 then
begin
ll:=socket.ReceiveText;
c:=strtoint(ll);
clientsocket1.Socket.SendText('okok');
end else
begin
len:=socket.ReceiveLength;
socket.ReceiveBuf(buffer,len);
m.Write(buffer,len);
if m.Size>=c then
begin
m.Position:=0;
b:=tbitmap.Create;
j:=tjpegimage.Create;
try
j.LoadFromStream(m);
b.Assign(j); //将JPG转为BMP
Image1.Picture.Bitmap.Assign(b);
finally
b.free;
j.free;
clientsocket1.Active:=false;
clientsocket1.Active:=true;
m.Clear;
c:=0;
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
m:=tmemorystream.Create;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
m.free;
ClientSocket1.Close;
end;
end.
为什么点击“连接”按钮之后,没有出现“通讯成功”的提示框?哪里出了问题??谢谢!
连接已经弄好了。。但是截图不成功。。。为什么? 展开
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询