UNP Study Notes (Introduction to Chapter I.)

Source: Internet
Author: User
Tags socket error htons

Environment construction

1. Download the decompression unpv13e.tar.gz

2. Go to directory execution

./configure
cd Lib //Enter the Lib directory
Make //Execute Make command

3. Copy the generated libunp.a static library to/usr/lib/and/usr/lib64/

CD..   Return to unpv13e Directory
sudo cp libunp.a/usr/lib /usr/lib64

4. Modify the Unp.h and copy it and config.h to/usr/include for later include convenience

VI lib/unp.h   // will be unp.h in # # # ". /config.h "modified to include" Config.h   "
sudo cp lib/unp.h/usr/include  /usr/include  

5. Compiling the source code

CD./intro  -o daytimetcpcli DAYTIMETCPCLI.C-LUNP

Unlike the normal compilation is to add just that the link library, the-l parameter plus just that Libunp.a removed Lib and the back. Finally get the parameter-LUNP.

The following is an example of socket network programming, which will be explained in detail later in this section

A simple time to get the client program

1#include"Unp.h"2 3 int4MainintargcChar**argv)5 {6     intsockfd, N;7     CharRecvline[maxline +1];8     structsockaddr_in servaddr;9 Ten     if(ARGC! =2) OneErr_quit ("usage:a.out <IPaddress>"); A  -     if(SOCKFD = socket (af_inet, Sock_stream,0)) <0) -Err_sys ("Socket Error"); the  -Bzero (&AMP;SERVADDR,sizeof(SERVADDR)); -servaddr.sin_family =af_inet; -Servaddr.sin_port = htons ( -);/*Daytime Server*/ +     if(Inet_pton (Af_inet, argv[1], &servaddr.sin_addr) <=0) -Err_quit ("Inet_pton error for%s", argv[1]); +  A     if(Connect (SOCKFD, (SA *) &servaddr,sizeof(SERVADDR)) <0) atErr_sys ("Connect Error"); -  -      while((n = Read (SOCKFD, Recvline, MAXLINE)) >0) { -Recvline[n] =0;/*null Terminate*/ -         if(Fputs (recvline, stdout) = =EOF) -Err_sys ("fputs Error"); in     } -     if(N <0) toErr_sys ("Read Error"); +  -Exit0); the}
View Code

Above is an implementation of the TCP current time query client program. After the client establishes a TCP connection with the server, the server is simply sent back to the current time and date in an intuitive, readable format.

Protocol agnostic

The above program is related to the IPV4 protocol. Modify the program to allow it to run in IPV6

1#include"Unp.h"2 3 int4MainintargcChar**argv)5 {6     intsockfd, N;7     structsockaddr_in6 servaddr;8     CharRecvline[maxline +1];9 Ten     if(ARGC! =2) OneErr_quit ("usage:a.out <IPaddress>"); A  -     if(SOCKFD = socket (Af_inet6, Sock_stream,0)) <0) -Err_sys ("Socket Error"); the  -Bzero (&AMP;SERVADDR,sizeof(SERVADDR)); -servaddr.sin6_family =Af_inet6; -Servaddr.sin6_port = htons ( -);/*Daytime Server*/ +     if(Inet_pton (Af_inet6, argv[1], &servaddr.sin6_addr) <=0) -Err_quit ("Inet_pton error for%s", argv[1]); +  A     if(Connect (SOCKFD, (SA *) &servaddr,sizeof(SERVADDR)) <0) atErr_sys ("Connect Error"); -  -      while((n = Read (SOCKFD, Recvline, MAXLINE)) >0) { -Recvline[n] =0;/*null Terminate*/ -         if(Fputs (recvline, stdout) = =EOF) -Err_sys ("fputs Error"); in     } -     if(N <0) toErr_sys ("Read Error"); +  -Exit0); the}
View Code

Wrapping function

This book is an example of a package function for error handling: The contract package function name is the initial capitalization of the actual function name, for example, the socket function is the socket function, and the following code demonstrates the wrapping function

1 int2Socket (intFamilyintTypeintprotocol)3 {4     intN;5     if((n = socket (family, type, protocol)) <0)6Err_sys ("Socket Error");7     return(n);8}
View Code

A simple time to get the server program

1#include"Unp.h"2#include <time.h>3 4 int5MainintargcChar**argv)6 {7     intlistenfd, CONNFD;8     structsockaddr_in servaddr;9     CharBuff[maxline];Ten time_t ticks; One  ALISTENFD = Socket (Af_inet, Sock_stream,0); -  -Bzero (&AMP;SERVADDR,sizeof(SERVADDR)); theservaddr.sin_family =af_inet; -SERVADDR.SIN_ADDR.S_ADDR =htonl (inaddr_any); -Servaddr.sin_port = htons ( -);/*Daytime Server*/ -  +Bind (LISTENFD, (SA *) &servaddr,sizeof(SERVADDR)); -  + Listen (LISTENFD, Listenq); A  at      for ( ; ; ) { -CONNFD = Accept (LISTENFD, (SA *) NULL, and NULL); -  -Ticks =Time (NULL); -snprintf (Buff,sizeof(Buff),"%.24s\r\n", CTime (&ticks)); - Write (CONNFD, Buff, strlen (buff)); in  - Close (CONNFD); to     } +}
View Code

Test program

Using the root user to run the server program from the background

Run the first client program

127.0.0.1 is the loopback address. In this example, the client program connects to the server program, the server program sends the time and date to the client, and the client prints it to the terminal.

UNP Study Notes (Introduction to Chapter I.)

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.