현재 제가 서버/클라이언트 프로그램을 만들어서 패킷을 주고 받는걸 구현해보고자하고 있습니다...근데 처음 커넥트부터 쉽지가 않네여...--;;;
나름대로 필요한 이론 공부를 하고서 애쓰고 있는데 잘 안되네여...흑...
제가 짜본 소스를 올립니다......많은 조언 부탁드립니다...
참 그리고 앞으로 어떻게 구상하면 좋을런지 굳아이디어도 부탁드립니당..^^
*****서버쪽...
unit USetupServer;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, IdBaseComponent, IdComponent, IdTCPServer;
type
TfrmSetupServer = class(TForm)
lbxSetupServer: TListBox;
Label1: TLabel;
TCPSetupServer: TIdTCPServer;
btnActive: TButton;
procedure FormCreate(Sender: TObject);
procedure btnActiveClick(Sender: TObject);
procedure TCPSetupServerConnect(AThread: TIdPeerThread);
procedure TCPSetupServerDisconnect(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmSetupServer: TfrmSetupServer;
implementation
{$R *.DFM}
procedure TfrmSetupServer.FormCreate(Sender: TObject);
begin
TCPSetupServer.Active:=true;
end;
procedure TfrmSetupServer.btnActiveClick(Sender: TObject);
begin
TCPSetupServer.DefaultPort:=8300;
TCPSetupServer.Active:=true;
end;
procedure TfrmSetupServer.TCPSetupServerConnect(AThread: TIdPeerThread);
begin
lbxSetupServer.Items.Add(AThread.Connection.Binding.PeerIP + ' 와 접속되었습니다.');
end;
procedure TfrmSetupServer.TCPSetupServerDisconnect(AThread: TIdPeerThread);
begin
lbxSetupServer.Items.Add(AThread.Connection.Binding.PeerIP + ' 와 접속이 끊겼습니다.');
end;
end.
*****클라이언트쪽
unit USetupClient;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, StdCtrls,
IdAntiFreezeBase, IdAntiFreeze, IdStackConsts, IdGlobal;
type
TMyThread = class(TThread)
private
Msg:string;
procedure view;
public
procedure Execute; override;
end;
TfrmSetupClient = class(TForm)
btnLogIn: TButton;
edtAddress: TEdit;
edtPassword: TEdit;
Label1: TLabel;
lbxSetupClient: TListBox;
Label2: TLabel;
Label3: TLabel;
TCPSetupClient: TIdTCPClient;
IdAntiFreeze1: TIdAntiFreeze;
btnConnect: TButton;
procedure btnConnectClick(Sender: TObject);
procedure TCPSetupClientConnected(Sender: TObject);
procedure TCPSetupClientDisconnected(Sender: TObject);
procedure edtAddressKeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
MyThread:TMyThread;
public
{ Public declarations }
end;
var
frmSetupClient: TfrmSetupClient;
implementation
{$R *.DFM}
procedure TMyThread.View;
begin
frmSetupClient.lbxSetupClient.Items.Add(Msg);
end;
procedure TMyThread.Execute;
begin
while not Terminated do
begin
try
Msg := frmSetupClient.TCPSetupClient.ReadLn;
Synchronize(View);
except
Terminate;
end;
end;
end;
procedure TfrmSetupClient.btnConnectClick(Sender: TObject);
var
szNumber: string;
begin
szNumber:=edtAddress.Text;
if szNumber='211.56.7.4' then
begin
TCPSetupClient.Port:=8300;
TCPSetupClient.Connect;
MyThread := TMyThread.Create(true);
MyThread.FreeOnTerminate := true;
MyThread.Resume;
end
else
ShowMessage('wrong IP!!');
end;
procedure TfrmSetupClient.TCPSetupClientConnected(Sender: TObject);
begin
lbxSetupClient.Items.Add('서버와 접속되었습니다.');
end;
procedure TfrmSetupClient.TCPSetupClientDisconnected(Sender: TObject);
begin
lbxSetupClient.Items.Add('서버와 접속이 끊겼습니다.');
end;
procedure TfrmSetupClient.edtAddressKeyPress(Sender: TObject;
var Key: Char);
begin
if (key in ['a'..'z', 'A'..'Z']) then
key := #0;
end;
end.
그거 보시면.. 도움이 되실듯..
그럼 ..이만. (--)(__)(--)