A simple time to get the client program

Source: Internet
Author: User
Tags htons

Program execution Flow:

1. Creating sockets

sockfd = socket(AF_INET, SOCK_STREAM, 0)

2. Zeroing the structure servaddr

bzero(&servaddr, sizeof(servaddr))

3. Place the address cluster asAF_INET

servaddr.sin_family = AF_INET

4. Port number set to 13

IP address and port number in an internetwork socket address structure These 2 members must use a specific format

Htons host to network short integer, to convert binary port number

servaddr.sin_port   = htons(13)

5. Transform the ASCII dot decimal string (IP address) into the correct format

inet_pton(AF_INET, argv[1], &servaddr.sin_addr)

6. Establish a connection to the server

connect(sockfd, (SA *) &servaddr, sizeof(servaddr))

Establishes a TCP connection with the server specified by the socket address structure that the first parameter of the Connect function points to

The length of the socket address structure must also be specified as the third parameter of the function

#define SA struct SOCKADDR is the universal socket address structure

7. Read the server's response

n = read(sockfd, recvline, MAXLINE)

Because there is no guarantee that a read call can return the entire answer to the server, when reading data from a TCP socket, we always need to write read in a loop, and when read returns 0 to close the connection to the end, a negative value indicates a dislocation, both terminating the loop.

8. Output results

fputs(recvline, stdout)

9. Termination procedures

exit(0)

#include "unp.h" intmain (int argc, char **argv) {INTSOCKFD, n;charrecvline[maxline + 1];struct sockaddr_inservaddr;if ( ARGC! = 2) err_quit ("Usage:a.out <IPaddress>"); if (SOCKFD = socket (af_inet, sock_stream, 0)) < 0) Err_sys ("Socke T error "); Bzero (&servaddr, sizeof (SERVADDR)); servaddr.sin_family = Af_inet;servaddr.sin_port   = htons (13);/* Daytime server */if (Inet_pton (Af_inet, argv[1], &servaddr.sin_addr) <= 0) err_quit ("Inet_pton error for%s", argv[ 1]); if (Connect (SOCKFD, (SA *) &servaddr, sizeof (SERVADDR)) < 0) Err_sys ("Connect Error"), while (n = Read (SOCKFD, Recvline, MAXLINE)) > 0) {recvline[n] = 0;/* null terminate */if (fputs (recvline, stdout) = = EOF) Err_sys ("Fputs error") ;} if (n < 0) Err_sys ("read error"); exit (0);}

A simple time to get the 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.