ICQ能夠實現點到點的數據傳送,但如果想把一個多媒體圖像文件送給另一方,是比較難的。但可以利用C++Builder 5.0 的控件來編寫一個用於發送圖像的應用程序。
打開C++Builder 5.0 ,新建一個工程,在Form1中添加NMStrm,NMStrmServ,OpenPictureDialog控件,再添;加Button控件3個,Label控件1個,Edit控件 2 個,StatusBar控件1個。
首先我們來認識一下NMStrm控件,NMStrmServ控件和OpenPictureDialog控件。NMStrm控件是一個基於TCP/IP的控件,它可以接收數據流,然後通過網絡將其發送給服務器。NMStrmServ控件是可以接收從客戶端發送來的數據流,服務器只監聽TCP/IP 端口,不負責監聽UDP端口。OpenPictureDialog控件是用於打開圖像文件進行浏覽或編輯。
各種控件的屬性設計如下:
控件名 屬性 值
Form Caption 發送圖像
Image Name Image1
Button name=Button1,Caption=發送
Button name=Button2,Caption=顯示
Button name=Button3,Caption=打開
Label name=Label1,Caption=遠程主機:
Edit name Edit1
Edit name Edit2
NMStrm name NMStrm1
NMStrmServ name NMStrmServ1
OpenPictureDialog name OpenPictureDialog1
StatusBar name StatusBar1
源程序代碼如下:
//“Unit1.h"的源代碼
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//----------------------------------------- -------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <Dialogs.hpp>
#include <ExtCtrls.hpp>
#include <ExtDlgs.hpp>
#include <NMSTRM.hpp>
#include <Psock.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TNMStrm *NMStrm1;
TNMStrmServ *NMStrmServ1;
TOpenPictureDialog *OpenPictureDialog1;
TImage *Image1;
TLabel *Label1;
TEdit *Edit1;
TButton *Button3;
TEdit *Edit2;
TStatusBar *StatusBar1;
void __fastcall Button3Click(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall NMStrm1Connect(TObject *Sender);
void __fastcall NMStrm1ConnectionFailed(TObject *Sender);
void __fastcall NMStrm1Disconnect(TObject *Sender);
void __fastcall NMStrm1HostResolved(TComponent *Sender);
void __fastcall NMStrm1MessageSent(TObject *Sender);
void __fastcall NMStrm1PacketSent(TObject *Sender);
void __fastcall NMStrm1Status(TComponent *Sender, AnsiString Status);
void __fastcall NMStrm1InvalidHost(bool &Handled);
void __fastcall NMStrmServ1ClientContact(TObject *Sender);
void __fastcall NMStrmServ1MSG(TComponent *Sender,
const AnsiString sFrom, TStream *strm);
void __fastcall NMStrmServ1Status(TComponent *Sender,
AnsiString Status);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//“Unit1.cpp"的源代碼
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
//打開文件
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if (OpenPictureDialog1->Execute())
Edit2->Text =OpenPictureDialog1->FileName ;
}
//---------------------------------------------------------------------------
//發送圖像文件
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TFileStream *MyFStream;
MyFStream = new TFileStream(OpenPictureDialog1->FileName, fmOpenRead);
try
{ NMStrm1->Host=Edit1->Text; //指定主機名
NMStrm1->PostIt(MyFStream); //發送的文件
}
catch(...){}
}
//---------------------------------------------------------------------------
//顯示圖像文件
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Image1->Picture->LoadFromFile(".\\tmp.bmp");
}
//---------------------------------------------------------------------------
//連接到遠程主機
void __fastcall TForm1::NMStrm1Connect(TObject *Sender)
{
StatusBar1->SimpleText = "Connected";
}
//---------------------------------------------------------------------------
//沒有連接到主機時產生此事件
void __fastcall TForm1::NMStrm1ConnectionFailed(TObject *Sender)
{
ShowMessage("無法連接到對方");
}
//---------------------------------------------------------------------------
//在客戶端文件發送到時產生此事件
void __fastcall TForm1::NMStrm1Disconnect(TObject *Sender)
{
if (StatusBar1 != 0)
StatusBar1->SimpleText = "文件發送到了";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMStrm1HostResolved(TComponent *Sender)
{
StatusBar1->SimpleText = "Host Resolved";
}
//---------------------------------------------------------------------------
//當文件發送到對方時產生此事件
void __fastcall TForm1::NMStrm1MessageSent(TObject *Sender)
{
ShowMessage("有文件發送到對方");
}
//---------------------------------------------------------------------------
//數據發送到對方時產生此事件
void __fastcall TForm1::NMStrm1PacketSent(TObject *Sender)
{
StatusBar1->SimpleText = IntToStr(NMStrm1->BytesRecvd)+" bytes of "
+NMStrm1->BytesTotal+" sent";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMStrm1Status(TComponent *Sender,AnsiString Status)
{
if (StatusBar1 != 0)
StatusBar1->SimpleText = Status;
}
//---------------------------------------------------------------------------
//沒有主機名時產生此事件
void __fastcall TForm1::NMStrm1InvalidHost(bool &Handled)
{
AnsiString NewHost;
if (InputQuery("沒有主機", "請輸入要發送的主機名:", NewHost))
{
NMStrm1->Host = NewHost;
Edit1->Text=NewHost;
}
}
//---------------------------------------------------------------------------
//表示客戶機已連接到服務器了
void __fastcall TForm1::NMStrmServ1ClientContact(TObject *Sender)
{
StatusBar1->SimpleText = "Client Contacted";
}
//---------------------------------------------------------------------------
//接收客戶端發送來的文件
void __fastcall TForm1::NMStrmServ1MSG(TComponent *Sender,
const AnsiString sFrom, TStream *strm)
{
if (FileExists(".\\tmp.bmp"))
DeleteFile(".\\tmp.bmp");
TFileStream *MyFStream;
MyFStream = new TFileStream(".\\tmp.bmp", fmCreate);
try
{
MyFStream->CopyFrom(strm, strm->Size);
}
catch(...)
{
}
MyFStream->Free();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMStrmServ1Status(TComponent *Sender,
AnsiString Status)
{
if (StatusBar1 != 0)
StatusBar1->SimpleText = Status;
}
//---------------------------------------------------------------------------
編好代碼以後,編譯源程序,在兩台計算機上運行它就行了,每一方都可以做服務器或工作站,只要對方運行了該程序,都可以向其發送圖象文件,也可以在單機上操作,快來試試吧!