wince 6.0 Tcp 编程实例
2010-05-31 15:19
447 查看
TCPClient 类使用 TCP 从 Internet 资源请求数据。
1、public TcpClient()当使用这种不带任何参数的构造函数时,将使用本机默认的ip地址并将使用默认的通信端口号0。
以下语句示例了如何使用默认构造函数来创建新的 TcpClient:
TcpClient tcpClientC = new TcpClient();
2、public TcpClient(IPEndPoint)使用本机IPEndPoint创建TcpClient的实例对象。
IPEndPoint将网络端点表示为IP地址和端口号,在这里它用于指定在建立远程主机连接时所使用的本地网络接口(IP 地址)和端口号。
IPHostEntry ipInfo=Dns.GetHostByName("www.tuha.net");//主机信息
IPAddressList[] ipList=ipInfo.AddressList;//IP地址数组
IPAddress ip=ipList[0];//多IP地址时一般用第一个
IPEndPoint ipEP=new IPEndPoint(ip,4088);//得到网络终结点
try{
TcpClient tcpClientA = new TcpClient(ipLocalEndPoint);
}
catch (Exception e ) {
Console.WriteLine(e.ToString());
}
到这里,你可能会感到困惑,客户端要和服务端创建连接,所指定的IP地址及通信端口号应该是远程服务器的呀!事实上的确如此,使用以上两种构造函数,你所实现的只是TcpClient实例对象与IP地址和Port端口的绑定,要完成连接,你还需要显式指定与远程主机的连接,这可以通过TcpClient类的Connect方法来实现, Connet方法使用指定的主机名和端口号将客户端连接到 远程主机:
1)、public void Connect(IPEndPoint); 使用指定的远程网络终结点将客户端连接到远程 TCP 主机。
public void Connect(IPAddress, int); 使用指定的 IP 地址和端口号将客户端连接到 TCP 主机。
public void Connect(string, int); 将客户端连接到指定主机上的指定端口。
需要指出的是,Connect方法的所有重载形式中的参数IPEndPoint网络终结点、IPAddress以及表现为string的Dns主机名和int指出的Port端口均指的是远程服务器。
以下示例语句使用主机默认IP和Port端口号0与远程主机建立连接:
TcpClient tcpClient = new TcpClient();//创建TcpClient对象实例
try{
tcpClient.Connect("www.contoso.com",11002);//建立连接
}
catch (Exception e ){
Console.WriteLine(e.ToString());
}
3、public TcpClient(string, int);初始化 TcpClient 类的新实例并连接到指定主机上的指定端口。与前两个构造函数不一样,这个构造函数将自动建立连接,你不再需要额外调用Connect方法,其中string类型的参数表示远程主机的Dns名,如:www.tuha.net。
以下示例语句调用这一方法实现与指定主机名和端口号的主机相连:
try{
TcpClient tcpClientB = new TcpClient("www.tuha.net", 4088);
}
catch (Exception e ) {
Console.WriteLine(e.ToString());
}
下面是我使用的实际例子:
1.创建tcpclient客户端,包含在System.Net.Socket;
2.创建ip地址,使用IPAddress,包含在System.Net.IPAddress,IPAddress ip = IPAddress.Parse("127.0.0.1");;
3.实例化tcpclient,使用不带参数的方法,这样默认绑定本地ip地址
4.使用tcpclient的Connect方法连接远程主机,参数为第二步创建的ip,和端口(直接写数字)
5.创建字节数组,然后将要发送的字符串转换为字节数组
Byte[] data = new Byte[256];
data = System.Text.Encoding.ASCII.GetBytes("Hello");
6.创建网络流,并且绑定到已经创建好的tcpclient上,使用NetworkStream stream = tcpClient.GetStream();
7.可以往网络上发送了,使用stream.Write(data, 0, data.Length);
三个参数的意义如下:
字节数组。此方法将 count 个字节从 buffer 复制到当前流。
offset
buffer 中的从零开始的字节偏移量,从此处开始将字节复制到当前流。
count
要写入当前流的字节数。
8.如果要等待远程主机的回应,继续往下看 如果不需要就可以跳到最后关闭连接
9.创建一个新的字节流缓冲区用于接收响应的数据(即远程主机发来的数据);
使用以下方法接收
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
这里要注意,stream.Read是阻塞的,如果套接字没被关闭,他就会一直等待到有数据为止,如果套接字关闭,他则会返回0;
10.自有处理收到的响应
11。关闭字节流,关闭套接字。
1、public TcpClient()当使用这种不带任何参数的构造函数时,将使用本机默认的ip地址并将使用默认的通信端口号0。
以下语句示例了如何使用默认构造函数来创建新的 TcpClient:
TcpClient tcpClientC = new TcpClient();
2、public TcpClient(IPEndPoint)使用本机IPEndPoint创建TcpClient的实例对象。
IPEndPoint将网络端点表示为IP地址和端口号,在这里它用于指定在建立远程主机连接时所使用的本地网络接口(IP 地址)和端口号。
IPHostEntry ipInfo=Dns.GetHostByName("www.tuha.net");//主机信息
IPAddressList[] ipList=ipInfo.AddressList;//IP地址数组
IPAddress ip=ipList[0];//多IP地址时一般用第一个
IPEndPoint ipEP=new IPEndPoint(ip,4088);//得到网络终结点
try{
TcpClient tcpClientA = new TcpClient(ipLocalEndPoint);
}
catch (Exception e ) {
Console.WriteLine(e.ToString());
}
到这里,你可能会感到困惑,客户端要和服务端创建连接,所指定的IP地址及通信端口号应该是远程服务器的呀!事实上的确如此,使用以上两种构造函数,你所实现的只是TcpClient实例对象与IP地址和Port端口的绑定,要完成连接,你还需要显式指定与远程主机的连接,这可以通过TcpClient类的Connect方法来实现, Connet方法使用指定的主机名和端口号将客户端连接到 远程主机:
1)、public void Connect(IPEndPoint); 使用指定的远程网络终结点将客户端连接到远程 TCP 主机。
public void Connect(IPAddress, int); 使用指定的 IP 地址和端口号将客户端连接到 TCP 主机。
public void Connect(string, int); 将客户端连接到指定主机上的指定端口。
需要指出的是,Connect方法的所有重载形式中的参数IPEndPoint网络终结点、IPAddress以及表现为string的Dns主机名和int指出的Port端口均指的是远程服务器。
以下示例语句使用主机默认IP和Port端口号0与远程主机建立连接:
TcpClient tcpClient = new TcpClient();//创建TcpClient对象实例
try{
tcpClient.Connect("www.contoso.com",11002);//建立连接
}
catch (Exception e ){
Console.WriteLine(e.ToString());
}
3、public TcpClient(string, int);初始化 TcpClient 类的新实例并连接到指定主机上的指定端口。与前两个构造函数不一样,这个构造函数将自动建立连接,你不再需要额外调用Connect方法,其中string类型的参数表示远程主机的Dns名,如:www.tuha.net。
以下示例语句调用这一方法实现与指定主机名和端口号的主机相连:
try{
TcpClient tcpClientB = new TcpClient("www.tuha.net", 4088);
}
catch (Exception e ) {
Console.WriteLine(e.ToString());
}
下面是我使用的实际例子:
1.创建tcpclient客户端,包含在System.Net.Socket;
2.创建ip地址,使用IPAddress,包含在System.Net.IPAddress,IPAddress ip = IPAddress.Parse("127.0.0.1");;
3.实例化tcpclient,使用不带参数的方法,这样默认绑定本地ip地址
4.使用tcpclient的Connect方法连接远程主机,参数为第二步创建的ip,和端口(直接写数字)
5.创建字节数组,然后将要发送的字符串转换为字节数组
Byte[] data = new Byte[256];
data = System.Text.Encoding.ASCII.GetBytes("Hello");
6.创建网络流,并且绑定到已经创建好的tcpclient上,使用NetworkStream stream = tcpClient.GetStream();
7.可以往网络上发送了,使用stream.Write(data, 0, data.Length);
三个参数的意义如下:
参数
buffer字节数组。此方法将 count 个字节从 buffer 复制到当前流。
offset
buffer 中的从零开始的字节偏移量,从此处开始将字节复制到当前流。
count
要写入当前流的字节数。
8.如果要等待远程主机的回应,继续往下看 如果不需要就可以跳到最后关闭连接
9.创建一个新的字节流缓冲区用于接收响应的数据(即远程主机发来的数据);
使用以下方法接收
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
这里要注意,stream.Read是阻塞的,如果套接字没被关闭,他就会一直等待到有数据为止,如果套接字关闭,他则会返回0;
10.自有处理收到的响应
11。关闭字节流,关闭套接字。
private void ConnectThreadFunction() { TcpClient tcpClient; IPAddress ip = IPAddress.Parse("127.0.0.1"); try { tcpClient = new TcpClient(); tcpClient.Connect(ip, 6000); Byte[] data = new Byte[256]; data = System.Text.Encoding.ASCII.GetBytes("Hello"); // Get a client stream for reading and writing. // Stream stream = client.GetStream(); NetworkStream stream = tcpClient.GetStream(); // Send the message to the connected TcpServer. stream.Write(data, 0, data.Length); // Buffer to store the response bytes. data = new Byte[256]; // String to store the response ASCII representation. String responseData = String.Empty; // Read the first batch of the TcpServer response bytes. Int32 bytes = stream.Read(data, 0, data.Length); responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes); dfun += new delerefresh(refreshstatusbar); this.BeginInvoke(dfun, new object[] { "Connect Succeed!"+responseData}); // Close everything. stream.Close(); tcpClient.Close(); } catch (Exception e) { dfun += new delerefresh(refreshstatusbar); this.BeginInvoke(dfun, new object[] { e.ToString() }); } }
相关文章推荐
- linux下TCP/socket编程实例
- 在魅族M8手机(WinCE6.0系统)上编程设置文件关联(C/C++源码)
- unix环境IPC通信之socket(三),tcp采用并发服务器编程实例,多进程方式 4000
- socket编程实例TCP
- java 网络编程【6】 用UDP编写网络聊天程序 以及 TCP编程实例
- TCP Socket编程实例1---Client端
- python网络编程之TCP通信实例和socketserver框架使用例子
- Linux网络编程系列-TCP编程实例
- Linux C Socket TCP编程介绍及实例
- windows socket 编程实例--TCP(2007-07-28 14:33)
- Linux Socket 编程实例——TCP
- Linux TCP客户端、服务器编程模型及实例
- java网络编程之TCP实例
- Linux Socket编程:基于TCP/IP的客户端与服务端通讯实例
- TCP网络编程【实例】
- 网络编程-TCP程序实例(client端heserver端相互通信)
- C# .net2.0 套接字编程实例 UDP TCP通信
- Linux网络编程系列-TCP编程实例
- python网络编程之TCP通信实例和socketserver框架使用例子
- Linux C Socket TCP编程介绍及实例