给分了呀!关于ServerSocket,ClientSocket(20分)

  • 主题发起人 主题发起人 yangliang516
  • 开始时间 开始时间
Y

yangliang516

Unregistered / Unconfirmed
GUEST, unregistred user!
unit main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, StdCtrls, Buttons, ScktComp, ExtCtrls, ComCtrls;

type
TChatForm = class(TForm)
MainMenu1: TMainMenu;
File1: TMenuItem;
Exit1: TMenuItem;
FileConnectItem: TMenuItem;
FileListenItem: TMenuItem;
StatusBar1: TStatusBar;
Bevel1: TBevel;
Panel1: TPanel;
Memo1: TMemo;
Memo2: TMemo;
N1: TMenuItem;
SpeedButton1: TSpeedButton;
Disconnect1: TMenuItem;
ServerSocket: TServerSocket;
ClientSocket: TClientSocket;
procedure FileListenItemClick(Sender: TObject);
procedure FileConnectItemClick(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormCreate(Sender: TObject);
procedure ServerSocketError(Sender: TObject; Number: Smallint;
var Description: string; Scode: Integer; const Source,
HelpFile: string; HelpContext: Integer; var CancelDisplay: Wordbool);
procedure Disconnect1Click(Sender: TObject);
procedure ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
procedure ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
protected
IsServer: Boolean;
end;

var
ChatForm: TChatForm;
Server: String;

implementation

{$R *.DFM}

procedure TChatForm.FileListenItemClick(Sender: TObject);
begin
FileListenItem.Checked := not FileListenItem.Checked;
//反置状态
if FileListenItem.Checked then
//监听
begin
ClientSocket.Active := False;
//禁止本机方客户程序
ServerSocket.Active := True;
//激活本机方客户程序
Statusbar1.Panels[0].Text := 'Listening...';
end
else
begin
if ServerSocket.Active then
ServerSocket.Active := False;
//禁止本机方服务器程序
Statusbar1.Panels[0].Text := '';
end;
end;

procedure TChatForm.FileConnectItemClick(Sender: TObject);
begin
if ClientSocket.Active then ClientSocket.Active := False;
//如当前状态是连接,则断开
if InputQuery('Computer to connect to', 'Address Name:', Server) then
//用户输入服务器地址
if Length(Server) > 0 then
with ClientSocket do
begin
Host := Server; //指定服务器方
Active := True; //激活客户方模块
FileListenItem.Checked := False;
end;
end;

procedure TChatForm.Exit1Click(Sender: TObject);
begin
ServerSocket.Close;
//关闭服务器socket
ClientSocket.Close;
//关闭客户socket
Close;
//退出程序
end;

procedure TChatForm.Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_Return then
//用户按回车键则一次发送完毕
if IsServer then
ServerSocket.Socket.Connections[0].SendText(Memo1.Lines[Memo1.Lines.Count - 1])
//如果是服务器方,这由本机服务器方模块发送消息
else
ClientSocket.Socket.SendText(Memo1.Lines[Memo1.Lines.Count - 1]);
//如果是客户方,则由本机客户方模块发送信息
end;

procedure TChatForm.FormCreate(Sender: TObject);
begin
// FileListenItemClick(nil);
//程序开始时处于监听状态
end;

procedure TChatForm.ServerSocketError(Sender: TObject; Number: Smallint;
var Description: string; Scode: Integer; const Source, HelpFile: string;
HelpContext: Integer; var CancelDisplay: Wordbool);
begin
ShowMessage(Description);
//显示出错信息
end;

procedure TChatForm.Disconnect1Click(Sender: TObject);
begin
ClientSocket.Active := False;
//断开与客户方的连接
ServerSocket.Active := True;
//打开服务器方模块,执行监听
Statusbar1.Panels[0].Text := 'Listening...';
end;

procedure TChatForm.ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Statusbar1.Panels[0].Text := 'Connected to: ' + Socket.RemoteHost;
end;

procedure TChatForm.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Add(Socket.ReceiveText);
end;

procedure TChatForm.ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Add(Socket.ReceiveText);
end;

procedure TChatForm.ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
IsServer := True;
//作为服务器方
Statusbar1.Panels[0].Text := 'Connected to: ' + Socket.RemoteAddress;
end;

procedure TChatForm.ServerSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Clear;
end;

procedure TChatForm.ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
FileListenItemClick(nil);
//客户方断开后重新开始监听
end;

procedure TChatForm.ClientSocketError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
Memo2.Lines.Add('Error connecting to : ' + Server);
//memo中显示出错信息
ErrorCode := 0;
end;

procedure TChatForm.ServerSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Statusbar1.Panels[0].Text := 'Listening...';
end;

end.



以上的程序,小弟有三点不明白,请各位高手给指点一下,谢谢!!
一、程序中的IsServer怎么没有初值呀,即使后面有一处,那可是在ServerSocket控件做出反应后,IsServer的值就没有变化了呀,一直是True吗?
二、我完全按照上面的代码,运行后,我输入LocalHost作为服务器,可总触发procedure TChatForm.ClientSocketError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);不能正常运行。
三、程序中为什么有procedure TChatForm.ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);和procedure TChatForm.FileConnectItemClick(Sender: TObject);呀?这两个方法不重复吗
还有procedure TChatForm.Disconnect1Click(Sender: TObject);和procedure TChatForm.ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
 
一、按上面程序所看,IsServer在Accpet时为True,那肯定应该在Close时或者禁止ServerSocket时为False。
二、感觉你好像没有指定Port
三、自己找吧
 
这位大哥,这是Delphi自带的例子,Demos/Internet/Chat,我也不知道是不是例子有误呀!那您看,IsServer:=False;应该加在哪里呀!
另外,port怎么指定呀,请给详细说明一下,好吧!
小弟在这儿多谢了!!
 
后退
顶部