S
streetlamp
Unregistered / Unconfirmed
GUEST, unregistred user!
小弟闲来无事,用Fastnet中的NMUDP写了一个小小的通讯工具,但是接受的信息却是乱码,代码如下,望各位仁兄帮忙。
unit ClientA;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, NMUDP;
type
TMenuForm = class(TForm)
IPaddress: TLabeledEdit;
Button1: TButton;
GetInfo: TMemo;
Button2: TButton;
Button3: TButton;
UDP: TNMUDP;
SendInfo: TLabeledEdit;
Port: TLabeledEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure UDPDataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MenuForm: TMenuForm;
implementation
{$R *.dfm}
procedure TMenuForm.Button1Click(Sender: TObject);
begin
If Trim(IPaddress.Text)='' then
begin
Showmessage('输入对方IP');
exit;
end;
If Trim(Port.Text)='' then
begin
Showmessage('输入对方Port');
exit;
end;
IPaddress.Enabled := False;
Port.Enabled := False;
end;
procedure TMenuForm.Button2Click(Sender: TObject);
var
S : String;
Buf: Array[0..200] of Char;
begin
UDP.RemoteHost := Trim(IPaddress.Text);
UDP.RemotePort := StrtoInt(Port.Text);
S := Sendinfo.Text;
StrCopy(Buf, PChar(S));
UDP.SendBuffer(Buf, Length(S));
Getinfo.Lines.Add(S);
end;
procedure TMenuForm.Button3Click(Sender: TObject);
begin
Getinfo.Clear;
end;
//这是接受
procedure TMenuForm.UDPDataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
S : Array [ 0..200 ] of char;
Len : integer;
begin
UDP.ReadBuffer(s,len);
if Len > 0 then
Getinfo.Lines.add(Copy(S,1,Len));
end;
end.
unit ClientA;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, NMUDP;
type
TMenuForm = class(TForm)
IPaddress: TLabeledEdit;
Button1: TButton;
GetInfo: TMemo;
Button2: TButton;
Button3: TButton;
UDP: TNMUDP;
SendInfo: TLabeledEdit;
Port: TLabeledEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure UDPDataReceived(Sender: TComponent; NumberBytes: Integer;
FromIP: String; Port: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MenuForm: TMenuForm;
implementation
{$R *.dfm}
procedure TMenuForm.Button1Click(Sender: TObject);
begin
If Trim(IPaddress.Text)='' then
begin
Showmessage('输入对方IP');
exit;
end;
If Trim(Port.Text)='' then
begin
Showmessage('输入对方Port');
exit;
end;
IPaddress.Enabled := False;
Port.Enabled := False;
end;
procedure TMenuForm.Button2Click(Sender: TObject);
var
S : String;
Buf: Array[0..200] of Char;
begin
UDP.RemoteHost := Trim(IPaddress.Text);
UDP.RemotePort := StrtoInt(Port.Text);
S := Sendinfo.Text;
StrCopy(Buf, PChar(S));
UDP.SendBuffer(Buf, Length(S));
Getinfo.Lines.Add(S);
end;
procedure TMenuForm.Button3Click(Sender: TObject);
begin
Getinfo.Clear;
end;
//这是接受
procedure TMenuForm.UDPDataReceived(Sender: TComponent;
NumberBytes: Integer; FromIP: String; Port: Integer);
var
S : Array [ 0..200 ] of char;
Len : integer;
begin
UDP.ReadBuffer(s,len);
if Len > 0 then
Getinfo.Lines.add(Copy(S,1,Len));
end;
end.