Network socket programming in the embedded operating system uClinux

Source: Internet
Author: User

2. network socket programming in the embedded operating system uClinux
Network socket data transmission is a special type of I/O, and socket is also a file descriptor. Socket also has a function call similar to opening a file. The socket () function returns an integer socket descriptor, and subsequent connection establishment and data transmission operations are implemented through this socket. There are two common socket types: stream socket: sock_stream and datagram socket: sock_dgram. Streaming is a connection-oriented socket for connection-oriented TCP Service applications; datagram socket is a connectionless socket that corresponds to connectionless UDP Service applications.
1. functions used in socket programming in uClinuxQuantity
(1) socket function: int socket (INT domain, int type, int Protocol );
To execute I/O, the first thing a process must do is to call the socket function, specify the expected communication protocol type (using IPv4 TCP, IPv6 UDP, and Unix domain byte stream protocol ).
(2) connect function: int connect (INT sockfd, struct sockaddr * serv_addr, int addrlen); TCP uses the connect function to establish a connection with the TCP server.
(3) bind function: int BIND (INT sockfd, struct sockaddr * my_addr, int addrlen );
The BIND function assigns a local Protocol address to the set of interfaces. For the Internet protocol, the Protocol address is a combination of two IPv4 addresses with 16-bit TCP or UDP port numbers.
(4) Listen function: int listen (INT sockfd, int backlog );
The listen function is called only by the TCP server. It performs two events. When the function socket creates a set of interfaces, it is assumed to be an active set of interfaces. That is to say, it is a customer interface that calls connect to initiate a connection. The listen function converts the unconnected interface into a passive interface, indicates that the kernel should accept connection requests pointing to this interface. The listen function is called Based on the TCP status conversion to convert the set of interfaces from the closed status to the lisen status. Generally, this function should be called after the socket and bind functions are called and before the function accept is called.
(5) accept function: int accept (INT sockfd, void * ADDR, int * addrlen );
The accept function is called by the TCP server and returns the next completed connection from the completed connection queue header. If the complete connection queue is empty, the process goes to sleep. (Assume that the set of interfaces is the default blocking method)
(6) Send () and Recv () functions:
Int send (INT sockfd, const void * MSG, int Len, int flags );
Int Recv (INT sockfd, void * Buf, int Len, unsigned int flags );
These two functions are used for communication between Stream sockets or datagram sockets. They use connectionless datagram sockets and should be used in sendto () and recvfrom () functions.

2 Implementation of network communication programming in uClinux
The Host Mode for the interaction between two processes in the TCP/IP network is the client/server model ). This mode is created based on the following two points:
1) Non-peering effect;
2) communication is completely asynchronous. In the Client/Server mode, the following request methods are used:
First, the server must start and provide corresponding services according to the request: (the process is as follows)
1. Open a channel and inform the local host that it is willing to receive customer requests at a recognized address;
2. Wait for the customer's request to reach the port;
3. Receive a duplicate service request, process the request, and send a response signal;
4. Return step 2, waiting for another customer's request;
5. Disable the server;
Customer:
1. Open a channel and connect to the specific port of the host where the server is located;
2. Send the service request message to the server, wait for and receive the response; Continue to make the request ......;
3. Close and terminate the communication channel after the request ends;
Based on the socket function described above, we can draw the sequence diagram of system call for the connected socket:
 
Note that there are two types of data storage priorities: high byte priority and low byte priority. Data on the internet is transmitted over the network in high byte precedence. Therefore, for machines that store data in low byte precedence mode, data transmission over the Internet requires conversion.
It should be said that the biggest difference between uClinux and standard Linux lies in memory management. At the same time, the memory management of uClinux also causes some problems that will not occur in standard Linux. UClinux reduces memory management and puts forward higher requirements for developers. Because the application must allocate continuous address space during loading, the memory size limit for block (continuous address) allocation is different for different hardware platforms, therefore, when developing an application, developers must consider the memory allocation and pay attention to the size of the running space required by the application. In addition, because the real-memory management policy is adopted, the user program is in the same address space as the kernel and other user programs. During program development, ensure that the address space of other programs is not infringed, so that the program does not disrupt the normal operation of the system, or cause exceptions in other programs. From the perspective of memory access, the developer's rights have increased (the developer can access any address space during programming), but at the same time the system's security is also greatly reduced.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.