Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the principle of accessing Network by C # Socket programming

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to analyze the principles of C # Socket programming to access the network. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

In the C # Socket programming study, we know that MS provides us with the System.Net.Sockets namespace, which contains the Socket class. So how does C # Socket access the network? Let's look at the basic principles.

The Conceptual explanation of C # Socket programming 1.C# Socket

A lot of things in Windows are borrowed from the field of Unix, and so is Socket. In Unix, socket represents a file descriptor (in Unix everything is in units of files), and here this descriptor is used to describe network access. What do you mean? That is, programmers can send and receive data on the network through socket. You can also understand it as an API. With it, you don't have to operate the network card directly, but through this interface, which saves a lot of complicated operations.

C # Socket programming 2.C# Socket to access the network:

The conditions required for C # Socket to access the network (the part related to programming): a. To determine the native IP and port, socket can be powerful only if it is bound to a certain IP and port. b. There has to be an agreement (otherwise who knows what you are sending to the network). If you want something complicated, we can make our own agreement. But I won't mention this in this article. I'd like to introduce two of the most familiar protocols: TCP & UDP. Don't say you don't know, otherwise. Otherwise. I won't tell you)

If you have the basic conditions, you can start using them to access the network. Let's take a look at the steps:

a. Create a socket

b. Bind native IP and port

c. If it is TCP, because it is connection-oriented, you should use the ListenO () method to listen to whether someone on the network is sending something to yourself; if it is UDP, because it is connectionless, you will not refuse anything.

In the case of TCP, if you listen for a connection, you can use accept to receive the connection, and then you can use Send/Receive to perform the operation. UDP, on the other hand, does not need accept, but directly uses SendTo/ReceiveFrom to perform the operation. (see clearly, it is different from the execution method of TCP, because UDP does not need to establish a connection, so it does not know the other party's IP and port before sending, so it is necessary to specify a sending node to send and receive normally.)

e. If you don't want to continue sending and receiving, don't waste resources. Close if you can close.

If you are not clear about the above text, take a look at the picture:

Connection-oriented socket system call timing

Connectionless socket system call timing

The above is the editor for you to share how to parse the principle of C # Socket programming to access the network, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report