udp的小程序,发送方如何最简单的方法以同文件名强行保存到接收方的任意路径下呢。
//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::Button1Click(TObject *Sender)
{
if(OpenDialog1->Execute())
Edit3->Text=OpenDialog1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(Edit3->Text=="")
{
ShowMessage("请选择一个您要传送的文件.");
return;
}
NMUDP1->RemoteHost=Edit1->Text;
NMUDP1->RemotePort=StrToInt(Edit2->Text);
NMUDP1->ReportLevel=Nmudp::Status_Basic;
const int ThreshHold=2048;
//载入文件流
TFileStream *Strm=new TFileStream(Edit3->Text, fmOpenRead);
char *Buffer=new char[ThreshHold];
int BuffSize=Strm->Size;
int Buffers=BuffSize/ThreshHold;
for(int i=0;i<=Buffers;i++)
{
if(BuffSize>2048)
{
Strm->Read(Buffer,ThreshHold);
NMUDP1->SendBuffer(Buffer,ThreshHold,ThreshHold);
}
else
{
Strm->Read(Buffer,BuffSize);
NMUDP1->SendBuffer(Buffer,BuffSize,BuffSize);
}
BuffSize-=ThreshHold;
}
delete Buffer;
delete Strm;
Memo1->Lines->Add("");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMUDP1DataReceived(TComponent *Sender,
int NumberBytes, AnsiString FromIP, int Port)
{
char *TmpBuffer=new char[NumberBytes+1];
int i;
//读取接收到的文件流
NMUDP1->ReadBuffer(TmpBuffer,NumberBytes,i);
TmpBuffer[NumberBytes]='\0';
Memo1->Text=Memo1->Text+TmpBuffer;
delete[] TmpBuffer;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMUDP1DataSend(TObject *Sender)
{
StatusBar1->Panels->Items[0]->Text="文件传送完毕.";
}
//---------------------------------------------------------------------------
#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::Button1Click(TObject *Sender)
{
if(OpenDialog1->Execute())
Edit3->Text=OpenDialog1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(Edit3->Text=="")
{
ShowMessage("请选择一个您要传送的文件.");
return;
}
NMUDP1->RemoteHost=Edit1->Text;
NMUDP1->RemotePort=StrToInt(Edit2->Text);
NMUDP1->ReportLevel=Nmudp::Status_Basic;
const int ThreshHold=2048;
//载入文件流
TFileStream *Strm=new TFileStream(Edit3->Text, fmOpenRead);
char *Buffer=new char[ThreshHold];
int BuffSize=Strm->Size;
int Buffers=BuffSize/ThreshHold;
for(int i=0;i<=Buffers;i++)
{
if(BuffSize>2048)
{
Strm->Read(Buffer,ThreshHold);
NMUDP1->SendBuffer(Buffer,ThreshHold,ThreshHold);
}
else
{
Strm->Read(Buffer,BuffSize);
NMUDP1->SendBuffer(Buffer,BuffSize,BuffSize);
}
BuffSize-=ThreshHold;
}
delete Buffer;
delete Strm;
Memo1->Lines->Add("");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMUDP1DataReceived(TComponent *Sender,
int NumberBytes, AnsiString FromIP, int Port)
{
char *TmpBuffer=new char[NumberBytes+1];
int i;
//读取接收到的文件流
NMUDP1->ReadBuffer(TmpBuffer,NumberBytes,i);
TmpBuffer[NumberBytes]='\0';
Memo1->Text=Memo1->Text+TmpBuffer;
delete[] TmpBuffer;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NMUDP1DataSend(TObject *Sender)
{
StatusBar1->Panels->Items[0]->Text="文件传送完毕.";
}
//---------------------------------------------------------------------------
...全文
相关推荐
UDP数据发送测试
IP连接两台PC,在一台PC上运行send_UDP,向另一台PC发送UDP数据。需要设置对方的IP地址,可以调整发送的速率
udp文件发送(udp传输大文件的一个例子)
udp文件发送
UDP数据接收测试
IP连接两台PC,在一台PC上运行RECEIVE_UDP,接收另外一台PC发来的UDP数据并保存。需要设置另外一台PC的IP地址,选择数据存储目录和文件名。
C#使用Socket实现发送和接收图片的方法
本文实例讲述了C#使用Socket实现发送和接收图片的方法。分享给大家供大家参考。具体如下: using System; using System.Collections.Generic; using System.Text; using System.Net.Sockets; using System.Net; using System.IO; namespace ConsoleApplication1 { Class Program { static void Main (String[] args) { // 1. to create a socke
C#基于UDP协议的Socket通信整套源码(包括发送端和接收端)
C#基于UDP协议的Socket通信整套源码(包括发送端和接收端


网络及通讯开发

