网络编程基础知识
using System.Net;
using System;
using System.Net.Scoket;
===============================================================================
public static IPAddress Parse(string ip opString);
IPAddress addr=IPAddress.Parse("192.168.0.2");
//调用Parse method运行失败时,可能会有以下两种异常状况
//1.ArgumentNullException:ipstring参数的值为空(null)
//2.FormatException:你所指定的IP地址格式不对
//比较两个IPAddress对象所指的是否为同一个IP地址时,可以通过Equals()这个方法来完成
public override bool Equals(object rhs);
//例如:
IPAddress ip1=IPAddress.Parse("192.168.0.121");
IPAddress ip2=IPAddress.Parse("192.168.0.122");
if(ip1.Equals(ip2))
Console.WriteLine("The IP is the same!");
else
Console.WriteLine("The IP is different!");
//DNS类
//Resolve Method:
public static IPHostEntry Resolve(string hostName);
IPHostEntry HostInfo=Dns.Resolve("192.168.0.201");
IPHostEntry HostInfo=Dns.Resolve("www.xxx.yyy.tw");
//当Resolve method运行失败时,可能会有以下两种异常状况
1.ArgumentNullException:hostName参数为空
2.SocketException :你所指定的主机名不存在
GetHostByAddress和GetHostByName Method
public static IPHostEntry GetHostByAddress(string address);
public static IPHostEntry GetHostByAddress(IPAddress address);
public static IPHostEntry GetHostByName(string name);
以上失败时,可能会有以下两种异常状况
1.ArgumentNullException
2.SocketException:当访问网络资源时发生错误的状况
GetHostName Method//获取本机名称
IPHostEntry类
常用属性:
1.AddressList
public IPAddress[] AddressList
说明:获取远程服务器主机地址列表。AddressList属性值是一个IPAddress类的数组。
2.Aliases
public string[] Aliases
说明:获取IP地址清单中相对的主机名称。
3.HostName
public string HostName
说明:获取服务器主机的域名
=========================================================================================
Socket
using .System.Net.Socket
属性
1.Connected
public bool Connected
说明:获取Socked对象的连接状态,若网络连接已经成功创建则返回true,否则返回false。
2.LovalEndPoint
public EndPoint LocalEndPoint
说明:获取代表本地端计算机的EndPoint对象,其中包含了IP地址及通信端口号等信息。
3.RemoteEndPoint
public EndPoint RemoteEndPoint
说明:获取代表远程计算机的EndPoint对象,其中包含了IP地址及通信端口号等信息.
4.ProtocolType
public int ProtocolType
说明:获取该Socket对象所使用的通信协议种类
5.Available
public int Available
说明:获取该Socket对象在网络输入缓冲区中的数据大小.
6.Handle
public IntPtr Handle
说明:获取该Socket对象在操作系统中相对应的Handle值。
==================================================
创建Socket对象
public Socket(AddressFamily addressFamily,SocketType socketType,ProtocolType protocolType);
例如:
Socket s=new Socket(AddressFamil.InterNetwork,socketType.Stream,ProtocolType.Tcp);
与远程服务器创建连接
先用IPEndPoint类创建一个表示远程主机IP地址和通信端口的对象,在调用Socket类中的Connect method;
public void Connect(EndPoint remoteEP);
例如:
//创建一个Socket对象
Socket s=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//获取代表远程服务器"www.nctu.edu.tw"的IPAddress对象
IPHostEntry hostEntry=Dns.Resolve("www.nctu.edu.tw");
//创建一个表示远程主机IP地址和通信端口号的IPEndPoint对象
IPEndPoint hos=new IPendPoint(hostEntry.AddressList[0],80);
try
{
//与服务器创建连接
s.Connect(host);
}
catch(ArgumentNullException error)
{
Console.WriteLine(error.Message);
}
在调用Connect method失败时,可能会产生的异常情况有两种
1.ArgumentNullException:remoteEP参数为空值
2.SocketException:在访问Socket时操作系统发生错误
-----------------------------------------------------
IPEndPoint类的构造函数:
public IPEndPoint(IPAddress address,int port);
其中address参数代表该计算机主机的IP地址,port代表所使用的端口号。
IPEndPoint类的属性:
public const int MaxPort;
指定通信端口号可被接受的最大值。
public const int MinPort;
指定通信端口号可被接受的最小值。
以上这两个属性是静态(Static)的属性
-------------------------------
public IPAddress address;
//获取或者设IP地址
public int port;
获取或者设置通信端口.
------------------------------------
当我们需要通过所创建的Socket对象将数据发送到远程服务器主机时,可以使用Send method;
public int Send(byte[] buffer);
public int Send(byte[] buffer,SocketFlags socketFlags);
public int Send(byte[] buffer,int size,SocketFlags socketFlags);
public int Send(byte[] buffer,int index,int size,SocketFlags socketFlags);
buffer是你所以发送的数据,而size则为buffer参数的长度,最后的flags参数是数据发送标志。
index可以指定从buffer中的那个位置开始发送。
返回值都是一个整数,此返回值都是Send method执行后实际送出了多少位组。
在调用第一个Send method失败时,可能产生的异常情况有两种:
1、ArgumentNullEXception:buffer参数为空
2、SocketException:在访问Socket时操作系统发生错误
在调用后三个Send method时,可能产生的异常错误有三种
1.ArgumentNullException:buffer参数为空
2.ArgumentException:index and size参数值超过了buffer的长度
3.ScoketException:在访问Socket时操作系统发生错误
---------------------------------------------------------------------------------
Socket类中还有一个与发送数据有关的method,那就是SendTo,格式如下:
public int SendTo(byte[],buffer,EndPoint remoteEP);
public int SendTo(byte[],buffer,SocketFlags socketFlags,EndPoint remoteEP);
public int SendTo(byte[],buffer,int size,SocketFlags socketFlags,EndPoint remoteEP);
public int SendTo(byte[],buffer,int index,int size,SocketFlags socketFlags,EndPoint remoteEP);
在调用第二个SendTo method失败时,可能产生的异常情况有两种:
1、ArgumentNullEXception:buffer参数为空
2、SocketException:在访问Socket时操作系统发生错误
在调用其余的SendTo method时,可能产生的异常错误有三种
1.ArgumentNullException:buffer参数为空
2.ArgumentException:index and size参数值超过了buffer的长度
3.ScoketException:在访问Socket时操作系统发生错误
问题点数:0、回复次数:8Top




