网络编程技术简介
目录
什么是网络编程???
网络编程从大的方面说就是对信息的发送到接收,中间传输为物理线路的作用。
网络编程最主要的工作就是在发送端把信息通过规定好的协议进行组装包,在接收端按照规定好的协议把包进行解析,从而提取出对应的信息,达到通信的目的。
网络编程的本质是两个设备之间的数据交换。其实就是把一个设备中的数据发送给另外一个设备,然后接受那个设备反馈的数据。比如C/S 和B/S架构,都体现了这种关系,都是基本上都是请求响应式的。不过模式有些不同。而我们做的就是基于网络通信的原理,编写这种网络应用程序。
传统的网络编程技术简介
网络编程的技术还要从套接字说起,Unix系统把主机+端口,叫做"套字"(socket)。套接字是支持TCP/IP网络通信的基本操作单元。有了它,就可以进行网络应用程序开发了。
一个套接字实例中保存有本机的IP地址和端口 对方的IP地址和端口 双方通信采用的网络协议等信息。
NET传统网络编程
- .System.Net命名空间中提供了一组Socket类调用WinSocket
- 针对TCP、UDP、FTP等协议对Socket类再次封装,提供
- TCPCLIENT、TCPLISTENER、 UDPCLIENT
- FTP应用编程、SMTP应用编程
WCF编程技术
基于传统网络编程技术
套接字编程(Socket类)
套接字有3种不同的类型
- 流式套接字:实现面向连接的TCP通信
- 数据报套接字:实现无连接的UDP通信
- 原始套接字:实现IP数据包通信,实现自定义协议通信
特点
- 可控性强。
- 灵活,可实现自定义网络协议。
- 代码编写复杂
TCP应用编程(TcpClient类、TcpListener类)
TCP通信
- 面向连接通信
- 可靠的数据传输
TCP实现方法
- Socket类面向连接的套接字
- 服务端使用TcpListener类客户端使用TcpClient类
UDP应用编程(UdpClient类)
UDP通信
-
无连接的套接字
-
无连接的套接字不需要在网络设备之间发送连接信息。
实现方法:
-
UdpClient类:
-
在UDP层面对套接字编程的进一步封装
-
Socket无连接套接字
参考资料:
[1] 网络编程