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);
}
}