T
tuer
Unregistered / Unconfirmed
GUEST, unregistred user!
我参考delphi列子做的一个程序,拿去参考:
///////////////////////////////////////////////////////////////////////////
// //
// Copyright ?1997-1998, NetMasters, L.L.C //
// - All rights reserved worldwide. - //
// Portions may be Copyright ?Inprise. //
// //
// UDP Demo Unit 1: (UNIT1.PAS) //
// //
// DESCRIPTION: //
// //
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY //
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE //
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR //
// PURPOSE. //
// //
///////////////////////////////////////////////////////////////////////////
//
// Revision History
//
// //
///////////////////////////////////////////////////////////////////////////
unit UDPDem;
interface
uses
WinSock, Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, NMUDP, ExtCtrls, Buttons;
type
TForm1 = class(TForm)
NMUDP1: TNMUDP;
Edit3: TEdit;
Bevel1: TBevel;
Panel2: TPanel;
Image1: TImage;
ComboBox1: TComboBox;
Memo2: TMemo;
Memo1: TMemo;
Label1: TLabel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
procedure NMUDP1Status(Sender: TComponent; status: String);
procedure NMUDP1DataSend(Sender: TObject);
procedure NMUDP1InvalidHost(var handled: Boolean);
procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
procedure ComboBox1Change(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure Memo2Click(Sender: TObject);
procedure CheckBox2Click(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
procedure Memo2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
h:string;
bj:string;
p2char;
implementation
{$R *.DFM}
procedure TForm1.NMUDP1Status(Sender: TComponent; status: String);
begin
// If StatusBar1 <> nil then
// StatusBar1.SimpleText := status;
end;
procedure TForm1.NMUDP1DataSend(Sender: TObject);
begin
// StatusBar1.SimpleText := 'Data Sent';
end;
procedure TForm1.NMUDP1InvalidHost(var handled: Boolean);
var
TmpStr: String;
begin
If InputQuery('Invalid Host!', 'Specify a new host:', TmpStr) then
Begin
NMUDP1.RemoteHost := TmpStr;
Handled := TRUE;
End;
end;
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream: TMemoryStream;
TmpStr: String;
begin
MyStream := TMemoryStream.Create;
try
try
NMUDP1.ReadStream(MyStream);
SetLength(TmpStr,NumberBytes);
MyStream.Read(TmpStr[1],NumberBytes);
if fromip='172.18.0.1' then bj:='jsj1说:';
if fromip='172.18.0.2' then bj:='jsj2说:';
if fromip='172.18.0.3' then bj:='jsj3说:';
if fromip='172.18.0.4' then bj:='jsj4说:';
if fromip='172.18.0.4' then bj:='jsj5说:';
Memo1.Lines.Add('[ '+bj+' ]');
Memo1.Lines.Add(TmpStr);
Memo1.Lines.Add('');
except
application.MessageBox('对方未开聊天程序!','注意',mb_ok);
end;
finally
MyStream.Free;
end;
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
if trim(combobox1.Text)='jsj1' then h:='172.18.0.1';
if trim(combobox1.Text)='jsj2' then h:='172.18.0.2';
if trim(combobox1.Text)='jsj3' then h:='172.18.0.3';
if trim(combobox1.Text)='jsj4' then h:='172.18.0.4';
if trim(combobox1.Text)='jsj5' then h:='172.18.0.5';
end;
procedure TForm1.FormShow(Sender: TObject);
begin
h:=strpas(p2);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
wVersionRequested : WORD;
wsaData : TWSAData;
p : PHostEnt;
s : array[0..128] of char;
OutPut:array[0..100] of char;
begin
{Start up WinSock}
wVersionRequested := MAKEWORD(1, 1);
WSAStartup(wVersionRequested, wsaData);
{Get the computer name}
GetHostName(@s, 128);
p := GetHostByName(@s);
{Get the IpAddress}
p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
StrPCopy(OutPut,'Hostname: '+Format('%s', [p^.h_Name])+#10#13+
'IP address: '+Format('%s',[p2])
);
WSACleanup;
// MessageBox(0,OutPut,'Get IP ! - this is your IP Address',mb_ok or mb_iconinformation);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
MyStream: TMemoryStream;
TmpStr: String;
i:integer;
Begin
if (trim(combobox1.Text)='') and (checkbox2.Checked=false) then
application.MessageBox('必须选择对谁说!','注意',mb_ok)
else
if trim(memo2.Text)='' then
application.MessageBox('必须输入要说的话!','注意',mb_ok)
else
begin
NMUDP1.ReportLevel := Status_Basic;
edit3.Text:='';
for i:=0 to memo2.lines.Count do
edit3.Text:=edit3.Text+memo2.lines;
TmpStr :=edit3.Text;
if checkbox2.Checked then
//群发
begin
NMUDP1.RemoteHost :='172.18.0.1';
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(Edit3.Text));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
NMUDP1.RemoteHost :='172.18.0.2';
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(Edit3.Text));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
NMUDP1.RemoteHost :='172.18.0.3';
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(Edit3.Text));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
NMUDP1.RemoteHost :='172.18.0.4';
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(Edit3.Text));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
NMUDP1.RemoteHost :='172.18.0.5';
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(Edit3.Text));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
end
else
//私聊
begin
NMUDP1.RemoteHost :=h;
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(Edit3.Text));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
end;
if h<>strpas(p2) then
begin
NMUDP1.RemoteHost :=strpas(p2);
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(Edit3.Text));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
end;
end;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
application.Terminate;
end;
procedure TForm1.Memo2Click(Sender: TObject);
begin
memo2.SelectAll;
end;
procedure TForm1.CheckBox2Click(Sender: TObject);
begin
if checkbox2.Checked then checkbox1.Checked:=false;
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
if checkbox1.Checked then checkbox2.Checked:=false;
end;
procedure TForm1.Memo2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if KEY=VK_MENU then
begin
Form1.BitBtn1Click(self);
memo2.SelectAll;
end;
end;
end.
///////////////////////////////////////////////////////////////////////////
// //
// Copyright ?1997-1998, NetMasters, L.L.C //
// - All rights reserved worldwide. - //
// Portions may be Copyright ?Inprise. //
// //
// UDP Demo Unit 1: (UNIT1.PAS) //
// //
// DESCRIPTION: //
// //
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY //
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE //
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR //
// PURPOSE. //
// //
///////////////////////////////////////////////////////////////////////////
//
// Revision History
//
// //
///////////////////////////////////////////////////////////////////////////
unit UDPDem;
interface
uses
WinSock, Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, NMUDP, ExtCtrls, Buttons;
type
TForm1 = class(TForm)
NMUDP1: TNMUDP;
Edit3: TEdit;
Bevel1: TBevel;
Panel2: TPanel;
Image1: TImage;
ComboBox1: TComboBox;
Memo2: TMemo;
Memo1: TMemo;
Label1: TLabel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
procedure NMUDP1Status(Sender: TComponent; status: String);
procedure NMUDP1DataSend(Sender: TObject);
procedure NMUDP1InvalidHost(var handled: Boolean);
procedure NMUDP1DataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
procedure ComboBox1Change(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure Memo2Click(Sender: TObject);
procedure CheckBox2Click(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
procedure Memo2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
h:string;
bj:string;
p2char;
implementation
{$R *.DFM}
procedure TForm1.NMUDP1Status(Sender: TComponent; status: String);
begin
// If StatusBar1 <> nil then
// StatusBar1.SimpleText := status;
end;
procedure TForm1.NMUDP1DataSend(Sender: TObject);
begin
// StatusBar1.SimpleText := 'Data Sent';
end;
procedure TForm1.NMUDP1InvalidHost(var handled: Boolean);
var
TmpStr: String;
begin
If InputQuery('Invalid Host!', 'Specify a new host:', TmpStr) then
Begin
NMUDP1.RemoteHost := TmpStr;
Handled := TRUE;
End;
end;
procedure TForm1.NMUDP1DataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
MyStream: TMemoryStream;
TmpStr: String;
begin
MyStream := TMemoryStream.Create;
try
try
NMUDP1.ReadStream(MyStream);
SetLength(TmpStr,NumberBytes);
MyStream.Read(TmpStr[1],NumberBytes);
if fromip='172.18.0.1' then bj:='jsj1说:';
if fromip='172.18.0.2' then bj:='jsj2说:';
if fromip='172.18.0.3' then bj:='jsj3说:';
if fromip='172.18.0.4' then bj:='jsj4说:';
if fromip='172.18.0.4' then bj:='jsj5说:';
Memo1.Lines.Add('[ '+bj+' ]');
Memo1.Lines.Add(TmpStr);
Memo1.Lines.Add('');
except
application.MessageBox('对方未开聊天程序!','注意',mb_ok);
end;
finally
MyStream.Free;
end;
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
if trim(combobox1.Text)='jsj1' then h:='172.18.0.1';
if trim(combobox1.Text)='jsj2' then h:='172.18.0.2';
if trim(combobox1.Text)='jsj3' then h:='172.18.0.3';
if trim(combobox1.Text)='jsj4' then h:='172.18.0.4';
if trim(combobox1.Text)='jsj5' then h:='172.18.0.5';
end;
procedure TForm1.FormShow(Sender: TObject);
begin
h:=strpas(p2);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
wVersionRequested : WORD;
wsaData : TWSAData;
p : PHostEnt;
s : array[0..128] of char;
OutPut:array[0..100] of char;
begin
{Start up WinSock}
wVersionRequested := MAKEWORD(1, 1);
WSAStartup(wVersionRequested, wsaData);
{Get the computer name}
GetHostName(@s, 128);
p := GetHostByName(@s);
{Get the IpAddress}
p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
StrPCopy(OutPut,'Hostname: '+Format('%s', [p^.h_Name])+#10#13+
'IP address: '+Format('%s',[p2])
);
WSACleanup;
// MessageBox(0,OutPut,'Get IP ! - this is your IP Address',mb_ok or mb_iconinformation);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
MyStream: TMemoryStream;
TmpStr: String;
i:integer;
Begin
if (trim(combobox1.Text)='') and (checkbox2.Checked=false) then
application.MessageBox('必须选择对谁说!','注意',mb_ok)
else
if trim(memo2.Text)='' then
application.MessageBox('必须输入要说的话!','注意',mb_ok)
else
begin
NMUDP1.ReportLevel := Status_Basic;
edit3.Text:='';
for i:=0 to memo2.lines.Count do
edit3.Text:=edit3.Text+memo2.lines;
TmpStr :=edit3.Text;
if checkbox2.Checked then
//群发
begin
NMUDP1.RemoteHost :='172.18.0.1';
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(Edit3.Text));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
NMUDP1.RemoteHost :='172.18.0.2';
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(Edit3.Text));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
NMUDP1.RemoteHost :='172.18.0.3';
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(Edit3.Text));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
NMUDP1.RemoteHost :='172.18.0.4';
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(Edit3.Text));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
NMUDP1.RemoteHost :='172.18.0.5';
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(Edit3.Text));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
end
else
//私聊
begin
NMUDP1.RemoteHost :=h;
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(Edit3.Text));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
end;
if h<>strpas(p2) then
begin
NMUDP1.RemoteHost :=strpas(p2);
MyStream := TMemoryStream.Create;
try
MyStream.Write(TmpStr[1], Length(Edit3.Text));
NMUDP1.SendStream(MyStream);
finally
MyStream.Free;
end;
end;
end;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
application.Terminate;
end;
procedure TForm1.Memo2Click(Sender: TObject);
begin
memo2.SelectAll;
end;
procedure TForm1.CheckBox2Click(Sender: TObject);
begin
if checkbox2.Checked then checkbox1.Checked:=false;
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
if checkbox1.Checked then checkbox2.Checked:=false;
end;
procedure TForm1.Memo2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if KEY=VK_MENU then
begin
Form1.BitBtn1Click(self);
memo2.SelectAll;
end;
end;
end.