1.一些概念
2. IP地址、端口号、mac地址
3.数据通信
4.OSI模型 !
5.TCP/IP协议
6.网络通信方案
7.IP地址和通信目标
IPAddress 和 IPEndPoint类
//默认主机ip是127.0.0.1
//Ip信息
IPAddress ipaddress = IPAddress.Parse("192.125.225.10");
//通信目标(ip+端口号)
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("192.125.225.10"), 8800);
8.域名解析
域名(类似 www.baidu.com这种形式)解析成ip地址等信息
IPHostEntry类 一般当作返回值来使用
Dns类 域名解析方法
public void Test()
{
//域名解析
//IPHostEntry 返回值
//Dns类
//获取本地主机名
//print(Dns.GetHostName());
//获取域名指定的IP信息
IPHostEntry iphost = Dns.GetHostEntry("www.baidu.com");
//ip地址
for (int i = 0; i < iphost.AddressList.Length; i++)
{
print(iphost.AddressList[i]);
}
//主机别名
for (int i = 0; i < iphost.Aliases.Length; i++)
{
print(iphost.Aliases[i]);
}
//DNS
print("DNS服务器名称:" + iphost.HostName);
//异步
LoadAsynd();
}
public async void LoadAsynd()
{
Task<IPHostEntry>tk= Dns.GetHostEntryAsync("www.baidu.com");
await tk;
//ip地址
for (int i = 0; i < tk.Result.AddressList.Length; i++)
{
print(tk.Result.AddressList[i]);
}
//主机别名
for (int i = 0; i < tk.Result.Aliases.Length; i++)
{
print(tk.Result.Aliases[i]);
}
//DNS
print("DNS服务器名称:" + tk.Result.HostName);
}
9.Socket套接字
tcp和udp的通信套接字
10.服务端
//创建tcp套接字
Socket sockettcp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
//绑定本地地址
sockettcp.Bind(endPoint);
}
catch (Exception e)
{
Console.WriteLine("创建套接字失败:"+e.Message);
}
//Listen监听
sockettcp.Listen(10);
Console.WriteLine("监听结束");
//Accept等待客户端连接 返回新的套接字
//这里要等待客户端连接之后才能执行之后的代码
Socket socketClient= sockettcp.Accept();
Console.WriteLine("等待连接");
//Send Receieve方法 收发数据
socketClient.Send(Encoding.UTF8.GetBytes("欢迎连接服务端"));
byte[] result = new byte[10];
int receieveNum = socketClient.Receive(result);
Console.WriteLine("接收到了来自{0}的消息{1}", socketClient.RemoteEndPoint.ToString(),
Encoding.UTF8.GetString(result, 0, receieveNum));
//释放连接
socketClient.Shutdown(SocketShutdown.Both);
//关闭套接字
socketClient.Close();
11.客户端
//Socket套接字
Socket socketTcp=new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
//写的是要连接的服务端的ip和端口号
IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
try
{
//Connect连接
socketTcp.Connect(iPEndPoint);
}