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.
客户端:
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.