Simple client program

Source: Internet
Author: User

Today, I am writing a host computer program for controlling the car and sending instructions to the WiFi module of the car. So I need to write a client program because the WiFi module has been configured as a server.

To connect the client to the server, you need to know the IP address of the server, not its IPv4 address, but its default Route IP address, and then specify the port number.

The IP address is used to determine the host location in the same network, and the port number is used to determine the processes for communication on the host.

Now that you know the key part (principle), the next step is how to program it.

**************************************** * Illustration ************************************** *************

Create a socket Descriptor (ID, which uniquely identifies the endpoint in network communication.). The following describes the function used to create a socket.

 Int socket (INT domain, int type, int Protocol)(From sys/types. H, sys/socket. h)

Domain is the protocol name of the specified communication.

Type indicates the type of the stream used for communication.

Generally, the Protocol is 0. Read the manual. This parameter is used to specify multiple protocols.

I wrote this, socket_fd = socket (af_inet, sock_stream, 0); // use IPv4 protocol, reliable and orderly, connection-oriented data transmission stream

If the call succeeds, 0 is returned. Otherwise,-1 is returned, and an error is returned.

  Create a socket address and store the IP address and port number together. The following structure description

Struct sockaddr_in {(From netinet/in. h)

Uint8 sin_len;

Sa_family sin_family;

In_port_t sin_port; // 16-bit segment slogan, network byte order

Struct in_addr sin_addr; // the description of this IP address structure is as follows:

Char sin_zero [8]; // standby

};

Struct in_addr {

In_addr_t s_addr; // 32-bit IP address, in bytes

};

  

My_addr.sin_family = af_inet; // 4154my_addr.sin_port = htons (2001); // converts a 16-bit host number to a 16-bit network byte number segment = inet_addr ("192.168.8.1 "); // converts a string to a 32-bit network byte number.
Segment

  Then, write the connection request.

  Int connect (INT sockfd, const struct sockaddr * ADDR, socklen_t addrlen)


Sockfd is the socket descriptor.

ADDR is an IP address structure.

Addrlen is the size of the address structure.

  Note that the structure type of this IP address is different from that of the previous one. This requires conversion and the length is still the previous one.

Code

Fprintf (stdout, "Connecting... \ n "); do {connect_ret = connect (socket_fd, (struct sockaddr *) & my_addr, sizeof (struct sockaddr_in);} while (connect_ret! = 0); fprintf (stdout, "connected! \ N ");/* until 0 is returned */

If the connection is successful, 0 is returned. Otherwise,-1 is returned, and an error is returned.

In this step, you can communicate with the server.

If the communication ends, call close. The parameter is also the socket descriptor.

Simple client program

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.