1,317
社区成员
发帖
与我相关
我的任务
分享
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "IdBaseComponent"
#pragma link "IdComponent"
#pragma link "IdUDPBase"
#pragma link "IdUDPServer"
#pragma link "IdUDPClient"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString ip = Edit1->Text;
int port = StrToInt(Edit2->Text);
TBytes MsgStr;
for (int i = 0; i < 5; i++)
{MsgStr = Memo2->Text.BytesOf();}
IdUDPClient1->SendBuffer(ip, port, MsgStr);
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::IdUDPServer1UDPRead(TIdUDPListenerThread *AThread, TBytes AData,
TIdSocketHandle *ABinding)
{
AnsiString RcvData,RcvIP,RcvPort;
RcvIP = ABinding->PeerIP;
RcvPort = IntToStr(ABinding->Port);
Edit3->Text = RcvIP;
Edit4->Text = RcvPort;
int RcvData_len = AData.Length ;
for(int i =0 ;i<RcvData_len ;i++)
{RcvData = RcvData + (char)AData[i] ;}
Memo1->Lines->Add(RcvData);
}
// ---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "urlmon.h" //syradd
#include "stdio.h" //syradd
#include "winsock2.h" //syradd
#include <wchar.h>
#pragma link "D:\\Program Files\\Borland\\CBuilder6\\Lib\\Psdk\\urlmon.lib" //syradd
#pragma link "D:\\Program Files\\Borland\\CBuilder6\\Lib\\Psdk\\ws2_32.lib" //syradd
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{//获取公网IP
char Buf_File_Read[MAX_PATH] = {0};
char IP_Temp[128];
char IP_Host[64];
URLDownloadToFile(0,"http://www.ip138.com/ip2city.asp","IPinTheWorld.ini",0,NULL);
FILE *fp_Download_URL_File=NULL;
if ((fp_Download_URL_File=fopen( "IPinTheWorld.ini","r")) == NULL)
{
MessageBox(NULL, "网络故障,无法接入Internet","提示",0);
return ;
}
if ( fp_Download_URL_File != NULL )
{
fseek(fp_Download_URL_File, 0, SEEK_SET);
fread(Buf_File_Read,1,256,fp_Download_URL_File);
fclose(fp_Download_URL_File);
char* Start_IP = strstr(Buf_File_Read,"[");
if (Start_IP)
{
sprintf( IP_Temp,"%s",Start_IP);
int Buf_Length = strlen(IP_Temp);
for (int i =0; i<Buf_Length; i++)
{
IP_Host[i] = IP_Temp[i+1];
if (IP_Temp[i] == ']')
{
IP_Host[i-1] = '\0';
DeleteFile("IPinTheWorld.ini");
continue;
}
}
}
}
AnsiString IP_World = IP_Host;
Memo1->Text = Memo1->Text + "\r\n公网IP:" + IP_World;
//MessageBox(NULL, chIP,chIP,0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{//获取本机IP
WSADATA Data_WSA;
int Result_WSAStartup = 1;
hostent *P_Host = NULL;
char Computer_Name[128] = "";
Result_WSAStartup = WSAStartup(MAKEWORD(2, 2), &Data_WSA);
if (Result_WSAStartup != 0)//无错返回0
{
MessageBox(NULL, "本机故障,无法启动网络服务","提示",0);
return ;
}
gethostname(Computer_Name, 128);//获取指定计算机的名字
if(Computer_Name[0] == '\0')
{
MessageBox(NULL, "本机网络故障,无法接入局域网","提示",0);
return ;
}
//Memo1->Text = s;
P_Host = gethostbyname(Computer_Name);
if(P_Host == NULL)
{
//Memo1->Text = "p NULL ";
//MessageBox(NULL, "本机网络故障,无法接入局域网","提示",0);
return ;
}
AnsiString IP_Host = inet_ntoa(*((in_addr *)P_Host->h_addr));//获取指定计算机的IP地址
if(P_Host != NULL)
{delete P_Host;}
P_Host = NULL;
WSACleanup();//完成对WSAStartup()的调用
Memo1->Text = Memo1->Text + "\r\n局域网IP:" + IP_Host;
//MessageBox(NULL, HostIP,HostIP,0);
}
//---------------------------------------------------------------------------
#include "stdafx.h"
#include <stdio.h>
#include<urlmon.h>
#pragma comment (lib,"Urlmon.lib")
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
char buf[MAX_PATH] = {0};
char chTempIp[128];
char chIP[64];
URLDownloadToFile(0,"http://www.ip138.com/ip2city.asp","c://1.ini",0,NULL);
FILE *fp=fopen( "c://1.ini", "r" );
if ( fp != NULL ) {
fseek(fp, 0, SEEK_SET);
fread(buf,1,256,fp);
fclose(fp);
char* iIndex = strstr(buf,"[");
if (iIndex) {
sprintf( chTempIp,"%s",iIndex);
int nBuflen = strlen(chTempIp);
for(int i =0; i<nBuflen; i++){
chIP[i] = chTempIp[i+1];
if(chTempIp[i] == ']') {
chIP[i-1] = '/0';
DeleteFile("c://1.ini");
continue;
}
}
}
}
MessageBox(NULL, chIP,chIP,0);
return 0;
}