Function Application of tcp ip protocol stack

Source: Internet
Author: User

Through the previous article, we have learned about the implementation of the TCP/IP protocol stack of the single chip microcomputer. Here we will focus on the function configuration. Now, in order to make it easier for everyone to implement this protocol, we will show some specific steps and functions. I hope it will be useful to you. So how to use the TCP/IP protocol stack? What Should users do after they get a single-chip tcp ip protocol stack? The processing is divided into two parts: the upper interface and the lower interface 。

◆ Upper Interface

The purpose of using single chip microcomputer TCP/IP is very clear, that is, to transmit data, that is, the data transmitted from a PC (or another single chip microcomputer) can be received on a local single chip microcomputer, the opposite is true. If the underlying layer is shielded, the single-chip TCP/IP protocol stack is a means of data transmission () function. This is the core of the TCP/IP protocol stack. But in fact, it is not that simple, for TCP, operations such as initiating a connection, accepting a connection, sending data, receiving data, and closing a connection are required to cooperate with data transmission. The tcp ip protocol stack generally provides the following interfaces for users:

1. initialize the function interfaces of the protocol stack and Release the protocol stack, such as Init () and Release 。

2. provides socket, sendto, recvfro, connect, bind, listen, accept, send, recv, closesocket, shutdown, getpeername, getsockname, htonl, htons, ntohl, ntohs, inet_addr, inet_ntoa, ioctlsocket, setsockopt, getsockopt, select, and other API functions. You can call these functions to initiate connections, receive connections, send data, receive data, and close connections 。

3. Accept connections, receive data, and passively close processing: because such operations are initiated by the host computer, the tcp ip protocol stack must provide a mechanism to handle such events 。

Generally, there are two methods:

A) Active wait: for example, use the recv () function of BSD to wait for the arrival of data 。

B) callback mechanism: the OnReceive () function similar to MFC is used for callback, that is, the OnReceive () function is automatically called when data is received 。

So how can users use the above interfaces to achieve remote control and remote data collection? The method is simple 。

For example, you need to implement a remote control application for lighting and removal of LED lights. First, the PC uses a TCP/IP sending tool, such as the SocketDlgTest Program, to send a byte "1" to the Development Board (according to the IP address of the Development Board ). send) in the OnReceive function, the Development Board finds "1" received, and enables the light through port P1. Otherwise, if "0" is received, the light is turned off 。

For a remote data collection application, such as regular temperature collection, the PC uses the SocketDlgTest program to wait for data to be sent. The Development Board collects data at intervals, and then sends () the function sends data to the PC (according to the IP address of the PC), so that the PC receives the current remote temperature data 。

◆ Lower layer interface

The lower-layer interface is a complex part, in fact, the connection between the tcp ip protocol stack and the underlying hardware is a problem. If you port the tcp ip protocol stack to your own single-chip system, there are some hardware differences, you need to modify the underlying code. here you need to include:

1. NIC Driver: If the TCP/IP protocol stack already provides NIC driver, and the driver is the same as the NIC chip used by the user, this is relatively easy. Here you only need to modify the basic address of the network card. Otherwise, you need to write the driver by yourself. If the network card uses RTL8019AS, you can refer to the MCU and TCP/IP network of laogu Development Network 。

2. TCP/IP underlying interface. Generally, single-chip microcomputer TCP/IP must provide the underlying interface for timed interruption and nic interruption processing 。

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.