IP DNS

本文通过一个C#实例展示了如何使用DNS解析域名获取IP地址,并实现了简单的TCP客户端与服务器的数据传输功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Threading;

namespace 网络应用二
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                IPHostEntry iphost = Dns.GetHostEntry(textBox_input.Text);
              //  IPHostEntry iphost = Dns.Resolve(textBox_input.Text); //这个提示过时了。。__!
                foreach (IPAddress ip in iphost.AddressList)
                {
                    string ipaddress = ip.AddressFamily.ToString();
                    listBox1.Items.Add(ipaddress);
                    listBox1.Items.Add(" " + ip.ToString());
                }
                textBox_out.Text = iphost.HostName;
            }
            catch
            {
                MessageBox.Show("请检查网络", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox_input.Text = "www.baidu.com";
            Thread threadNew = new Thread(new ThreadStart(listen));
            threadNew.Start();
           
        }
        //-----------TCP 演示--发送-
        private void button_send_Click(object sender, EventArgs e) //TCP演示
        {
            TcpClient tcpCFir = new TcpClient(textBox_tc.Text,Int32.Parse(textBox_port.Text));
            NetworkStream netstring = tcpCFir.GetStream();
            FileStream fstr = File.Open("form1.cs", FileMode.Open);
            int data= fstr.ReadByte();
            if (data != -1)
            {
                netstring.WriteByte((byte)data);
                data = fstr.ReadByte();
            }
            fstr.Close();
            netstring.Close();
            tcpCFir.Close();
           

        }
        //---TCP演示 接受--
        public void listen()
        {
            IPAddress localAdd = IPAddress.Parse("127.0.0.1");
            int port = 2112;
            TcpListener clientLis = new TcpListener(localAdd, port);
            clientLis.Start();
            //----
            TcpClient tcpCf = clientLis.AcceptTcpClient();
            NetworkStream ns = tcpCf.GetStream();
            StreamReader rs = new StreamReader(ns);
            string data = rs.ReadToEnd();
           // textBox1.Text = data;
           Invoke(new Updelegate(UpadataDisplay), new object[] { data }); //-------
            tcpCf.Close();
            clientLis.Stop();

        }
        //------------
        public void UpadataDisplay(string text)
        {
            textBox1.Text = text;
        }
        //----------
        protected delegate void Updelegate(string text);
       

       

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值