Windows.Networking.Sockets for hololens2 communication

Windows.Networking.Sockets:
This namespace is used to provide socket and websocket classes for network communication, as well as classes for real-time network notifications received by UWP applications in the background.

  •   The so-called socket (Socket) is an abstraction of an endpoint for two-way communication between application processes on different hosts in the network. A socket is one end of process communication on the network, providing a mechanism for application layer processes to exchange data using network protocols. From the perspective of its position, the socket connects to the application process and connects to the network protocol stack, which is the interface for the application program to communicate through the network protocol, and the interface for the application program to interact with the network protocol stack.
      Socket (socket) can be regarded as the endpoint in the respective communication connection when two network applications communicate, which is a logical concept. It is an API (application programming interface) for inter-process communication in a network environment, and it is also a communication endpoint that can be named and addressed. Each socket in use has its type and a process connected to it. During communication, one of the network applications writes a piece of information to be transmitted into the Socket of the host where it is located, and the Socket sends this piece of information to the Socket of another host through the transmission medium connected to the network interface card (NIC), so that The other party can receive this message. Socket is a combination of IP address and port, providing a mechanism to transmit data packets to the application layer process
      . number, separated by colons or commas. Each transport layer connection is uniquely identified by two endpoints (that is, two sockets) at both ends of the communication. For example: If the IP address is 210.37.145.1, and the port number is 23, then the socket is (210.37.145.1:23)

A brief introduction to two of the main classes:

StreamSocketListener class:
It supports listening for incoming network connections using TCP stream sockets or Bluetooth RFCOMM. The StreamSocketListener class supports listening for incoming network connections using stream sockets and accepting connections.
A typical run sequence is as follows:

  • Create a StreamSocketListener object
  • Use the Control property to retrieve a StreamSocketListenerControl object and set the desired socket quality of service.
  • Assign the ConnectionReceived event to an event handler.
  • Call the BindServiceNameAsync or bindenppointasync method to bind to a local TCP port number or service name. For Bluetooth RFCOMM, the local service name parameter is the Bluetooth service ID.
  • StreamSocketListenerConnectionReceivedEventArgs object to retrieve the Socket property of the created StreamSocket object.
  • Send and receive data using the StreamSocket object.
  • Call the Close method to stop listening and accepting incoming network connections and release any unmanaged resources associated with the StreamSocketListener object. Any StreamSocket objects created when a connection was received are unaffected and may continue to be used as desired.

When calling asynchronous methods on the StreamSocketListener class, you must write code to handle exceptions. Parameter validation errors, name resolution errors, and network errors can cause exceptions. Anomalies of network errors such as lost connections, connection failures, and server failures may occur at any time. These errors cause exceptions to be thrown. If the application does not handle the exception, it may result in the entire application being terminated by the runtime.
StreamSocket class:
The StreamSocket class supports network communication in UWP applications using stream sockets over TCP or Bluetooth RFCOMM.
For a client application, the most common sequence of operations using StreamSocket is as follows:

  • Create StreamSocket
  • Use the Control property to obtain the StreamSocketControl object, and set any properties on the StreamSocketControl object before calling a ConnectAsync method.
  • Call a ConnectAsync method to establish a connection with the remote endpoint. For bluetooth, the remote service name is a bluetooth service id. Some of the ConnectAsync methods can be used to specify if TCP requires an SSL/TLS connection, or if Bluetooth requires immediate encryption. If an SSL/TLS connection is required after sending and receiving some initial data on the TCP socket, the UpgradeToSslAsync method can be called later to upgrade the connection to use SSL.
  • Gets the OutputStream property to write data to the remote host.
  • Get the InputStream property to read data from a remote host.
  • Read and write data as needed.
  • Calling the Close method disconnects the socket, aborts any pending operations, and releases any unmanaged resources associated with the StreamSocket object.

Guess you like

Origin blog.csdn.net/ZhangJingHuaJYO/article/details/124375232