(一)CSharp-网络编程

本文介绍了CSharp网络编程的基础知识,涵盖OSI参考模型、TCP/IP模型、IP地址的分类与子网掩码,以及端口号的使用。讨论了.NET中的网络组件,如System.NET命名空间和System.Web命名空间的主要类,以及网络编程中常用的IPHostEntry、IPEndPoint等类的作用和功能。
摘要由CSDN通过智能技术生成

一、OSI 参考模型

(1)物理层
作为原始的位流或电气处理。

(2)数据链路层
负责建立、维持和释放数据链路的连接。

(3)网络层
选择合适的网间路由和交换结点,以确保数据及时传送。网络层将数据链路层提供的帧组织成数据包,包中封装有网络层包头、其中含有逻辑地址信息(源站点和目的站点地址的网络地址)

(4)传输层
,为两个端系统(也就是源站和目的站)的会话层之间,提供建立、维护和取消传输连接的功能,并负责可靠地传输数据。

(5)会话层
提供包括访问验证和会话管理在内的建立和维护应用之间通信的机制。

(6)表示层
提供格式化的表示和转换数据服务。

(7)应用层
提供网络与用户应用软件之间的接口服务。

请添加图片描述

二、TCP/IP 模型

(1)链路层
负责接收 IP 数据报并通过网络发送到网络传输媒体上。

(2)网络层
把分组发往目标网络或主机。

(3)传输层
TCP 和 UDP 协议。

(4)应用层
超文本传输协议(HTTP)、简单网络管理协议(SNMP)、文件传输协议(FTP)、简单邮件传输协议(SMTP)、域名系统(DNS)、远程登录协议(Telnet)。

请添加图片描述

请添加图片描述

三、IP 地址

A 类: 0 ******* xxxxxxxx xxxxxxxx xxxxxxxx
B 类:10 ****** ******** xxxxxxxx xxxxxxxx
C 类:110 ***** ******** ******** xxxxxxxx
D 类:1110 xxxx xxxxxxxx xxxxxxxx xxxxxxxx
E 类:1111 xxxx xxxxxxxx xxxxxxxx xxxxxxxx

A 类地址: 第一个字节为网络地址,后三个字节为主机地址。
B 类地址: 前两个字节为网络地址,后两个字i节为主机地址。
C 类地址: 前三个字节为网络地址,最后一个字节为主机地址。
D 类地址: 多播地址,用于多播通信。只能作为目的地址,不能作为源地址。
E 类地址: 实验性地址,一般不会用到。

网路地址: 主机地址为0。例如,B类IP 地址为 175.22.10.48,则网络地址为175.22.0.0。
广播地址: 在 A、B、C 类地址中,主机号全为 1 的地址为广播地址。
回送地址: 首字节数值为127的地址是一个保留地址。用于网络测试或本机进程间通信,数据报不输出线路上。

1、子网与掩码

主机地址分为若干个子网,分为子网地址和主机地址。对于网路地址和子网地址的地址掩码位为1。对于主机地址的地址掩码位为0.

2、端口号

(1)全局分配。
(2)本地分配。

TCP/IP 端口号分为:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
网络编程是现代计算机应用程序开发中不可或缺的一部分。C#作为一种高级编程语言,提供了许多用于网络编程的类和API,如System.Net和System.Net.Sockets命名空间。 下面是一个简单的C#网络编程实验,用于建立一个TCP/IP客户端和服务器之间的连接,并在客户端和服务器之间发送消息。 1. 创建一个新的C#控制台应用程序项目。 2. 在Program.cs文件中添加以下代码,用于创建一个TCP/IP服务器: ```csharp using System; using System.Net; using System.Net.Sockets; using System.Text; namespace Server { class Program { static void Main(string[] args) { TcpListener server = null; try { // Set the TcpListener on port 13000. Int32 port = 13000; IPAddress localAddr = IPAddress.Parse("127.0.0.1"); // TcpListener server = new TcpListener(port); server = new TcpListener(localAddr, port); // Start listening for client requests. server.Start(); // Buffer for reading data Byte[] bytes = new Byte[256]; String data = null; // Enter the listening loop. while (true) { Console.Write("Waiting for a connection... "); // Perform a blocking call to accept requests. // You could also user server.AcceptSocket() here. TcpClient client = server.AcceptTcpClient(); Console.WriteLine("Connected!"); data = null; // Get a stream object for reading and writing NetworkStream stream = client.GetStream(); int i; // Loop to receive all the data sent by the client. while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { // Translate data bytes to a ASCII string. data = Encoding.ASCII.GetString(bytes, 0, i); Console.WriteLine("Received: {0}", data); // Process the data sent by the client. data = data.ToUpper(); byte[] msg = Encoding.ASCII.GetBytes(data); // Send back a response. stream.Write(msg, 0, msg.Length); Console.WriteLine("Sent: {0}", data); } // Shutdown and end connection client.Close(); } } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } finally { // Stop listening for new clients. server.Stop(); } Console.WriteLine("\nHit enter to continue..."); Console.Read(); } } } ``` 3. 添加以下代码,用于创建TCP/IP客户端: ```csharp using System; using System.Net; using System.Net.Sockets; using System.Text; namespace Client { class Program { static void Main(string[] args) { string server = "127.0.0.1"; int port = 13000; try { TcpClient client = new TcpClient(server, port); // Translate the passed message into ASCII and store it as a Byte array. string message = "Hello, server!"; Byte[] data = Encoding.ASCII.GetBytes(message); // Get a client stream for reading and writing. NetworkStream stream = client.GetStream(); // Send the message to the connected TcpServer. stream.Write(data, 0, data.Length); Console.WriteLine("Sent: {0}", message); // Receive the TcpServer.response. // 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 = Encoding.ASCII.GetString(data, 0, bytes); Console.WriteLine("Received: {0}", responseData); // Close everything. stream.Close(); client.Close(); } catch (ArgumentNullException e) { Console.WriteLine("ArgumentNullException: {0}", e); } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } Console.WriteLine("\nHit enter to continue..."); Console.Read(); } } } ``` 4. 运行程序,将启动服务器并等待客户端连接。然后启动客户端,将向服务器发送一条消息并显示响应。 这个实验只是一个简单的例子,但它演示了如何使用C#编写基本的网络应用程序。你可以使用这些基础知识来构建更复杂的应用程序,如聊天程序、文件传输应用程序等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值