急求socket问题(23分)

  • 主题发起人 主题发起人 cf83325
  • 开始时间 开始时间
C

cf83325

Unregistered / Unconfirmed
GUEST, unregistred user!
我在一个管理系统,里面加了一局域内部聊天程序~~~这是我第一次编socket.不能发消息发送.希望有人能帮我看看.错在哪里?????(服务端是好的,客户端老是出现'Asynchronoussocket error 10061 '的错误)
客户端:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp;

type
TMainForm = class(TForm)
ClientSocket1: TClientSocket;
Edit1: TEdit;
Button1: TButton;
Memo1: TMemo;
Edit2: TEdit;
Button2: TButton;
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
procedure Send_Message;
public
{ Public declarations }
end;

var
MainForm: TMainForm;

implementation

{$R *.dfm}

procedure TMainForm.Send_Message;
begin
ClientSocket1.Socket.SendText(Edit1.Text);//发送消息
Edit1.Text;
end;

procedure TMainForm.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
s:string;
begin
s:=ClientSocket1.Socket.ReceiveText;//接收主机消息;
Memo1.Lines.Add('服务器器返回的信息为:'+s);//将主机的消息赋给memo1.
end;

procedure TMainForm.Button2Click(Sender: TObject);
begin
if Button2.Caption='登录' then
begin
Button2.Caption:='断开';
ClientSocket1.Address:=Edit2.Text;
ClientSocket1.Active:=false;
ClientSocket1.Active:=true;
Button1.Enabled:=true;
Edit1.Enabled:=true;
end
else
begin
Button2.Caption:='登录';
ClientSocket1.Active:=false;
Button1.Enabled:=false;
Edit1.Enabled:=false;
end;
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
Send_Message;
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if ClientSocket1.Active then
ClientSocket1.Active:=False;
end;

服务端:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp;

type
TMainForm = class(TForm)
ServerSocket1: TServerSocket;
Button1: TButton;
Memo1: TMemo;
Label1: TLabel;
ServerSocket2: TServerSocket;
procedure ServerSocket2Listen(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket2ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket2ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
procedure UpdateState;//更新当前的连接信息

public
{ Public declarations }
end;

var
MainForm: TMainForm;

implementation

{$R *.dfm}

procedure TMainForm.UpdateState;
begin
label1.Caption:='在线人数为:'+IntToStr(ServerSocket1.Socket.ActiveConnections);
end;


procedure TMainForm.ServerSocket2Listen(Sender: TObject;
Socket: TCustomWinSocket);
begin
UpdateState;
end;

procedure TMainForm.ServerSocket2ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
UpdateState;
end;

procedure TMainForm.ServerSocket2ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
s:string;
begin
s:=Socket.ReceiveText;

Memo1.Lines.Add('从客户端接收的信息为:'+s);

Socket.SendText('接收信息成功!');
end;

procedure TMainForm.Button1Click(Sender: TObject);
begin

if Button1.Caption='启动' then

begin

Button1.Caption:='停止';

ServerSocket1.Active:=False;

ServerSocket1.Active:=True;

end else

begin

Button1.Caption:='启动';

ServerSocket1.Active:=False;

end;

end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if ServerSocket1.Active then
ServerSocket1.Active:=False;
end;

end.
 
建议使用indy
 
好心的人给个回答啊~~~~~
 
查查以前的资料好像是有
 
HWND FindWindow( LPCTSTR lpClassName,
LPCTSTR lpWindowName
);
第一个参数为窗口的类名;第二个参数为窗口的标题(Caption),也就是显示在窗口上的名字
lpWindowName
[in] Pointer to a null-terminated string that specifies the window name (the window's title). If this parameter is NULL, all window names match.
因此你的调用是有问题的
 
Findwindow的第二个参数是窗口的标题
 
窗口标题试试
 
handle:=Findwindow(nil,'QQ.exe'); //'QQ.exe'改为QQ.exe运行时的标题名
 
这么用更保险:

function FindQQ:Boolean;
var
FSnapshotHandle :THandle;
FProcessEntry32 :TProcessEntry32;
Ret :Boolean;
FHandel :THandle;
strProcessName :string;
begin
Result:=True;
try
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if FSnapshotHandle=0 then exit;
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
Ret:=Process32First(FSnapshotHandle,FProcessEntry32);

while Ret do
begin
if Win32Platform=VER_PLATFORM_WIN32_NT then // 如果为NT操作系统
begin
FHandel := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, FProcessEntry32.th32ProcessID);
if FHandel <> 0 then
begin
if GetModuleFileNameEx(FHandel, 0,FProcessEntry32.szExeFile,SizeOf(FProcessEntry32.szExeFile)) = 0 then
begin
StrPCopy(FProcessEntry32.szExeFile, '[Idle]');
end;
CloseHandle(FHandel);
end;
end;
strProcessName:=LowerCase(FProcessEntry32.szExeFile);

Result:=(AnsicompareText(ExtractFileName(strProcessName),'qq.exe')=0);
if Result then Break;

Ret:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;

CloseHandle(FSnapshotHandle);
except
On e:Exception do Result:=False;
end;
End;
 
楼主想写木马呀?西西呵呵[:D]
 
qq.exe是进程名,不是标题名。

你可以查

Game<-->QQ Exchange Dlg 或者 QQ2006 或者 TE-Oicq Receive Dlg
 
爱元元的哥哥写的很好~~~完全接受
 
谢谢各位,特别是
爱元元的哥哥的回答我很满意
 
后退
顶部