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

The C # network programming code framework is the most practical.

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

Share

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

This article to share with you is about C#network programming code framework is the most practical, Xiaobian feel quite practical, so share to everyone to learn, I hope you can read this article after some gains, not much to say, follow Xiaobian to see it.

Basic network programming process:

1: Socket concept and prototype function

The original meaning is "hole" or "socket." As a process communication mechanism for BSD UNIX. Also commonly referred to as a "socket," it describes IP addresses and ports and is a handle to a communication chain. Hosts on the Internet typically run multiple service software and provide several services simultaneously. Each service opens a Socket and binds to a port, and different ports correspond to different services.

Function prototype: int socket(int domain, int type, int protocol); Parameter description:

Domain: Protocol domain, also known as protocol family. Common protocol families include AF_INET, AF_INET6, AF_LOCAL (or AF_UNIX, Unix domain Socket), AF_ROUTE, etc. The protocol family determines the address type of the socket, and the corresponding address must be used in communication. For example, AF_INET determines to use the combination of ipv4 address (32 bits) and port number (16 bits), AF_UNIX determines to use an absolute pathname as the address. type: Specifies the Socket type. Common socket types include SOCK_STREAM, SOCK_DRAM, SOCK_RAW, SOCK_PACKET, SOCK_SEQPACKET, etc. SOCK_STREAM is a connection-oriented Socket for connection-oriented TCP service applications. Datagram Socket (SOCK_DDRAM) is a connectionless Socket corresponding to connectionless UDP service applications. protocol: Specify the protocol. Common protocols include IPPROTO_TCP, IPPROTO_UDP, IPPROTO_SCTP, IPPROTO_TIPC, etc., which correspond to TCP transmission protocol, UDP transmission protocol, STCP transmission protocol and TIPC transmission protocol respectively. Note: type and protocol cannot be combined arbitrarily, for example, SOCK_STREAM cannot be combined with IPPROTO_UDP. When the third parameter is 0, the default protocol corresponding to the second parameter type is automatically selected. Return value: Returns the descriptor of the newly created socket if the call succeeds, INVALID_SOCKET if it fails (-1 on Linux). A socket descriptor is a value of type integer. Each process has a socket descriptor table in its process space, which stores the correspondence between socket descriptors and socket data structures. The table has a field for the descriptor of the newly created socket and another field for the address of the socket data structure, so that the socket descriptor can be used to find its corresponding socket data structure. Each process has a socket descriptor table in its own process space, but socket data structures are stored in kernel buffers of the operating system.

2: BIND function prototype: int bind(SOCKET socket, construct sockaddr* address, socklen_t address_len); parameter description: socket: is a socket descriptor. address: is a pointer to a sockaddr structure that contains the address and port number of the machine to be bound to. address_len: Determines the length of the address buffer. Return value: 0 if the function executes successfully, otherwise SOCKET_ERROR.

3: Overall client and server interaction Figure 4: Accept method

5: A simple example in C#

Server side:

The above is the C#network programming code framework is the most practical, Xiaobian believes that some knowledge points may be seen or used in our daily work. I hope you can learn more from this article. For more details, please 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