[C#] 纯文本查看 复制代码 using System;
using System.Windows.Forms;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.Threading;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void socket()
{
try
{
IPAddress[] ips = Dns.GetHostAddresses("ns1.dnspod.net");
IPAddress ipAddress = IPAddress.Parse(ips[0].ToString());
EndPoint point = new IPEndPoint(ipAddress, 6666);
Socket tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
tcpClient.Connect(point);
while (true)
{
byte[] data = new byte[1024];
tcpClient.Receive(data);
string stringData = Encoding.UTF8.GetString(data);
if (!string.IsNullOrWhiteSpace(stringData))
{
MessageBox.Show("您的外网IP为:" + stringData);
break;
}
}
tcpClient.Close();
}
catch
{
MessageBox.Show("error");
}
}
private void button1_Click_1(object sender, EventArgs e)
{
new Thread(socket).Start();
}
}
}
|