delphi源程序:
unit msgdem;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Psock, NMMSG, ComCtrls, ScktComp, IdBaseComponent,
IdComponent, IdTCPServer, IdHostnameServer, IdTCPConnection, IdTCPClient,
IdWhois, Buttons;
type
TForm1 = class(TForm)
Panel1: TPanel;
Edit1: TEdit;
NMMsg1: TNMMsg;
NMMSGServ1: TNMMSGServ;
Label1: TLabel;
Label2: TLabel;
Edit3: TEdit;
StatusBar1: TStatusBar;
Label3: TLabel;
ClientSocket1: TClientSocket;
Edit2: TEdit;
Panel2: TPanel;
Panel3: TPanel;
ListBox2: TListBox;
ListBox1: TListBox;
Splitter1: TSplitter;
SaveDialog1: TSaveDialog;
BitBtn1: TBitBtn;
procedure Panel1Resize(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject;
var Key: Char);
procedure NMMSGServ1MSG(Sender: TComponent;
const sFrom, sMsg: String);
procedure NMMsg1MessageSent(Sender: TObject);
procedure NMMsg1Connect(Sender: TObject);
procedure NMMsg1ConnectionFailed(Sender: TObject);
procedure NMMsg1Disconnect(Sender: TObject);
procedure NMMsg1HostResolved(Sender: TComponent);
procedure NMMsg1Status(Sender: TComponent;
Status: String);
procedure NMMsg1InvalidHost(var handled: Boolean);
procedure NMMSGServ1ClientContact(Sender: TObject);
procedure NMMSGServ1Status(Sender: TComponent;
Status: String);
procedure FormActivate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Panel1Resize(Sender: TObject);
begin
Edit1.Left := 8;
Edit1.Width := Panel1.Width - 16;
Label1.Left := 8;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject;
var Key: Char);
begin
If Key = #13 then
begin
NMMsg1.Host := Edit3.Text;
NMMsg1.FromName := Edit2.Text;
NMMsg1.PostIt(Edit1.Text);
end;
end;
procedure TForm1.NMMSGServ1MSG(Sender: TComponent;
const sFrom,
sMsg: String);
begin
if ListBox1.Items.IndexOf(sFrom)=-1 then
ListBox1.Items.Add(sFrom);
ListBox2.Items.Add (sMsg);
end;
procedure TForm1.NMMsg1MessageSent(Sender: TObject);
begin
ShowMessage('Message sent!');
end;
procedure TForm1.NMMsg1Connect(Sender: TObject);
begin
StatusBar1.SimpleText := 'Connected';
end;
procedure TForm1.NMMsg1ConnectionFailed(Sender: TObject);
begin
ShowMessage('Connection Failed');
end;
procedure TForm1.NMMsg1Disconnect(Sender: TObject);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := 'Disconnected';
end;
procedure TForm1.NMMsg1HostResolved(Sender: TComponent);
begin
StatusBar1.SimpleText := 'Host Resolved';
end;
procedure TForm1.NMMsg1Status(Sender: TComponent;
Status: String);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := status;
end;
procedure TForm1.NMMsg1InvalidHost(var handled: Boolean);
var
TmpStr: String;
begin
If InputQuery('Invalid Host!', 'Specify a new host:', TmpStr) then
begin
NMMsg1.Host := TmpStr;
Handled := TRUE;
end;
end;
procedure TForm1.NMMSGServ1ClientContact(Sender: TObject);
begin
NMMsgServ1.ReportLevel := Status_Basic;
NMMsgServ1.TimeOut := 90000;
StatusBar1.SimpleText := 'Client connected';
end;
procedure TForm1.NMMSGServ1Status(Sender: TComponent;
Status: String);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := status;
end;
procedure TForm1.FormActivate(Sender: TObject);
var
ComputerName :array[0..MAX_COMPUTERNAME_LENGTH + 1] of char;
sComputerName :string;
lpSize
WORD;
begin
lpSize := MAX_COMPUTERNAME_LENGTH + 1;
if GetComputerName(@ComputerName ,lpSize) then
begin
sComputerName := ComputerName;
end
else
begin
sComputerName := '计算机名非法!';
end;
Edit2.Text :=sComputerName;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if SaveDialog1.Execute then
if savedialog1.FileName<>'' then
begin
SaveDialog1.Title := '保存日记到文本文件 ';
ListBox2.Items.SaveToFile(savedialog1.filename+'.doc');
listbox2.Items.Clear;
end;
end;
end.
vb源程序:
Option Explicit
Private Sub cmdConnect_Click()
lblStatus.Caption = "Connection to: " &
txtRemoteIP.Text &
":" &
txtRemotePort.Text ' Sets the status
Winsock1.Connect txtRemoteIP.Text, Val(txtRemotePort.Text) ' Connects the client to ip, val(port)
End Sub
Private Sub cmdDisconnect_Click()
Winsock1.Close ' Closes the connection
cmdConnect.Enabled = True ' Changes the buttons status
cmdDisconnect.Enabled = False
cmdListen.Enabled = True
cmdSend.
Enabled = False
lblStatus.Caption = "Disconnected" ' Set the new status message
End Sub
Private Sub cmdExit_Click()
Winsock1.Close ' Closes the connection
End ' Exits
End Sub
Private Sub cmdListen_Click()
Winsock1.LocalPort = Val(txtLocalPort.Text) ' Sets the port to listen on
Winsock1.Listen ' Opens port
lblStatus.Caption = "Listening on port: " &
txtLocalPort.Text ' Updates status
cmdConnect.Enabled = False ' Changes the buttons status
cmdDisconnect.Enabled = True
cmdListen.Enabled = False
cmdSend.
Enabled = False
End Sub
Private Sub cmdSend_Click()
Winsock1.SendData txtOut.Text ' Sends the data to the other end
End Sub
Private Sub Form_Load()
cmdDisconnect.Enabled = False ' Sets the buttons
cmdSend.
Enabled = False
End Sub
Private Sub txtIn_Change()
txtIn.SelStart = Len(txtIn.Text) ' Makes it so it scrollsdo
wn when more text is added
End Sub
Private Sub Winsock1_Connect()
lblStatus.Caption = "Connected: " &
txtRemoteIP.Text &
":" &
txtRemotePort.Text ' Sets status
cmdConnect.Enabled = False ' Updates buttons
cmdDisconnect.Enabled = True
cmdListen.Enabled = False
cmdSend.
Enabled = True
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If (Winsock1.State <> sckClosed) then
Winsock1.Close ' If not closed then
call close method to cleanup socket status
Winsock1.LocalPort = 0 ' Clear the localport
Winsock1.Accept requestID ' Accept the incoming connection
txtRemoteIP.Text = Winsock1.RemoteHostIP ' Tells you their ip
txtRemotePort.Text = Winsock1.RemotePort ' Tells you their port
lblStatus.Caption = "Connected: " &
txtRemoteIP.Text &
":" &
txtRemotePort.Text ' Updates status
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim INCOMING ' Sets the varible
Winsock1.GetData INCOMING, vbString ' Puts the incoming data into the varible
txtIn.Text = txtIn.Text &
INCOMING ' Adds the varible to the text box
End Sub
Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
lblStatus.Caption = "Error: " &
Description ' Descibes the error in the status box
End Sub