Introduction to Linux network programming __arduino

Source: Internet
Author: User
Tags localhost 8888

A Linux Network Programming--Introduction of network knowledge

Linux Network Programming--Introduction of network knowledge
Client and service side
One of the biggest differences between web programs and ordinary programs is that the Web program is made up of two parts-the client and the server side.

Client
In a network program, if a program is actively communicating with an outside program, then we call this program a client program. For example, we use the FTP program from another
A place to get the file, it is our FTP program actively with the outside to communicate (get files), so this place our FTP program is the client program.
Service side
The program that corresponds to the client is the server-side program. A program that passively waits for an outside program to communicate with itself is called a server-side program.
For example, the above file acquisition, another place of the program is the server, we get the file from the server to come.
Mutual customer and service-side
In real life, some programs are mutual service and client. In this case project, a program is both a client and a server.

Common commands
Because the network program is composed of two parts, so it is troublesome to debug, we need to know some common network commands
Netstat
Command netstat is used to display network connections, routing tables, and interface statistics. Netstat has many options.
Our common option is-na to display detailed network status. As for the other options, we can use the Help manual for detailed information.
Telnet
Telnet is a program to log in remotely, but we can use this program to debug our server program.
For example, our server program is listening on port 8888, we can use
telnet localhost 8888
To view the status of the service side.
The Pingping program is used to determine whether the state of the network is normal, and one of the most common uses is
Ping 192.168.0.1
Indicates that we want to see if the hardware connection to 192.168.0.1 is normal
TCP/UDP Introduction
TCP (Transfer control Protocol) transmission protocol is a connection-oriented protocol, and when our network program uses this protocol,
The network can ensure that our client and server connections are reliable and secure.

The UDP (user Datagram Protocol) Datagram Protocol is not a connection-oriented protocol,
This protocol does not guarantee that the connection of our network program is reliable, so the program we are writing now generally uses the TCP protocol.

Two Linux Network Programming--Introduction to Elementary network functions (TCP)

Linux systems are programmed by providing sockets for network programming. Network program through the socket and several other functions of the call,
Returns a file descriptor for the communication, which we can use as the descriptor for the normal file, which is the benefit of the Linux device-independent nature.
We can communicate data between networks by reading and writing to descriptors.
(a) socket

int socket (int domain, int type,int protocol)

Domain: Describes the host of our network program used by the Communication Association (AF_UNIX and Af_inet, etc.).
Af_unix can only be used for single UNIX system interprocess communication,
And Af_inet is for the internet, so it allows you to allow remote
Communication between hosts (when we find that the domain option is pf_* instead of af_* when we're a man socket, because GLIBC is a POSIX implementation, it replaces AF with PF,
But we can all use it.

Type: The communication protocol (SOCK_STREAM,SOCK_DGRAM, etc.) that our network program uses
Sock_stream shows that we are using the TCP protocol, which provides sequential, reliable, bidirectional, and connection-oriented bit streams.
Sock_dgram shows that we are using the UDP protocol, which will only provide fixed-length, unreliable, connectionless communications.

Protocol: Because we specified the type, so this place we generally as long as the 0来 instead of the socket for network communication to do basic preparation.
Returns a file descriptor on success, returns 1 when it fails, and looks at the details of the error that errno can see.


(ii) BIND
int bind (int sockfd, struct sockaddr *my_addr, int addrlen)

SOCKFD: Is the file descriptor returned by the socket call.

Addrlen: Is the length of the SOCKADDR structure.

MY_ADDR: is a pointer to a sockaddr. There is a definition of sockaddr in the

struct sockaddr{
unisgned short as_family;
Char sa_data[14];
};

However, due to the compatibility of the system, we generally do not use this header file, instead of using another structure (struct sockaddr_in) to replace. There are sockaddr_in definitions in
struct sockaddr_in{
unsigned short sin_family;
unsigned short int sin_port;
struct IN_ADDR sin_addr;
unsigned char sin_zero[8];
}
We mainly use the internet so
Sin_family is generally af_inet,
Sin_addr set to Inaddr_any indicates that it can communicate with any host.
Sin_port is the port number that we are listening to. Sin_zero[8] is used to populate.
Bind the local port with the socket

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.