C++ Builder的程序,不好意思
//远程主机的UDP包
void __fastcall TMDIChild::UDPRemoteReceiveData(BYTE *buffer, int Len, AnsiString FromIP, int Port)
{//
ReceiveCount += Len ;
Memo->Lines->Add( "Receive From "+ FromIP +" : "+ IntToStr(Port)+" "+IntToStr(Len)+" Bytes");
int i;
AnsiString szTemp = FromIP+".",szIP;
BYTE *BUF =NULL;
BUF = (BYTE*)malloc( Len + 50 );
ZeroMemory(BUF,Len + 50 );
BUF[3] = 1; //IP V4
for(i = 0;i<4;i++)
{
szIP = szTemp.SubString(1,szTemp.Pos(".")-1);
szTemp = szTemp.SubString(szTemp.Pos(".")+1,szTemp.Length());
BUF[i+4] = StrToInt(szIP);
}
BUF[8] = UDPLocal->RemotePort / 0x100 ;
BUF[9] = UDPLocal->RemotePort % 0x100 ;
CopyMemory((BYTE*)&BUF[10],buffer,Len);
UDPLocal->Send((char*)BUF,Len+10);
Memo->Lines->Add( "Send To "+ UDPLocal->RemoteHost +" : "+ IntToStr(UDPLocal->RemotePort)+" "+IntToStr(Len+10)+" Bytes");
free(BUF);
}
//---------------------------------------------------------------------------
//客户端的UDP包
void __fastcall TMDIChild::UDPLocalReceiveData(BYTE *buf, int Len, AnsiString FromIP, int Port)
{
Memo->Lines->Add( "Receive From "+ FromIP +" : "+ IntToStr(Port)+" "+IntToStr(Len)+" Bytes");
int i;
char RemoteIP[120]=TEXT("");
int j ;
char domainName[150];
if( buf[0] == 0 && buf[1] == 0 )
{
switch( buf[3] )
{
case 1: //IP V4
//Get IP
sprintf(RemoteIP,"%u.%u.%u.%u",int(buf[4]), int(buf[5]) ,int(buf[6]),int(buf[7]) );
//UDP To Server
if( !UDPRemote->OpenFlag )
{
UDPRemote->Open();
}
UDPRemote->RemoteHost = RemoteIP;
UDPRemote->RemotePort = buf[8]*0x100 + buf[9];
//UDP ASSOCIATE
i = Len-10;
UDPRemote->Send((char*)&buf[10] ,i );
SendCount += i;
Memo->Lines->Add( "Send To "+ UDPRemote->RemoteHost +" : "+ IntToStr(UDPRemote->RemotePort)+" "+IntToStr(Len+10)+" Bytes");
break ;
case 3: //Domain Name
//Get DomainName
for(j = 0;j < buf[4] ;j ++ ) domainName[j] = buf[j+5];
domainName[j] = 0 ;
//UDP To Server
UDPRemote->RemoteHost = domainName;
UDPRemote->RemotePort = buf[8]*0x100 + buf[9];
//UDP ASSOCIATE
i = Len-10;
UDPRemote->Send((char*)&buf[10] ,i );
break;
case 4: //IP V6
//Later
break;
}
}
}
//---------------------------------------------------------------------------