資源簡介
用上面的接口可以查詢自己的公網(wǎng)IP,本人已經(jīng)搭了服務器!
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Threading.Tasks;
using?System.Net.Sockets;
using?System.Net;
using?System.Runtime.InteropServices;
namespace?QueryMyPublicNetworkIP
{
????class?Program
????{
????????//TCP?Client
????????public?static?string?QueryMyPublicNetworkIPbyTcp()
????????{
????????????string?ipstr?=?string.Empty;
????????????try
????????????{
????????????????//1.Socket()
????????????????Socket?s?=?new?Socket(AddressFamily.InterNetwork?SocketType.Stream?ProtocolType.Tcp);
????????????????//2.Connect()
????????????????s.Connect(“148.70.217.194“?1002);
????????????????//3.Send()
????????????????byte[]?buf?=?new?byte[50];
????????????????string?pass?=?“Please?tell?me?My?Public?Network?IPAddress“;
????????????????buf?=?Encoding.ASCII.GetBytes(pass);
????????????????s.Send(buf);
????????????????////4.Receive()
????????????????byte[]?data?=?new?byte[50];
????????????????s.Receive(data);
????????????????ipstr?=?Encoding.ASCII.GetString(data).TrimEnd(‘\0‘);
?
評論
共有 條評論