UNIX network programming-Environment setup (solving unp. h and other source code compilation problems)

Source: Internet
Author: User

The following describes how to program the network with simple but typical client and server programs. This section describes the client, a client used to connect to and read the time sent by the server.

This involves writing code, so we need

Build a UNIX Network Programming Environment


The UNIX system is not installed, the system is not easy to obtain, and there is no need to install UNIX. Linux is the best choice. In addition, I was originally a Ubuntu system, so I set up the environment in Ubuntu.

1. Install the compiler. Install build-essential for completeness.

sudo apt-get install build-essential

2. Download the header file and sample source code of this book.

Click here to download unpv12e.tar.gz(this compilation has a question. Click here to download unpv13e.tar.gz (this article tells me that the error has been resolved in the itnovice reply, so it is best to use this to save a lot of time)


(If you download unpv13e, you can skip the issue in step 3)

3. Decompress unpv12e.tar.gz and go to the directory to view readme. Then, follow the prompts in the prompt box. However, problems may occur.

Step 1: Enter the upnv12e directory in the terminal and run the Code:

./configure


Step 2: Problems and Solutions

cd lib


Make // I have an error in this step


Error code:

Gcc-g-O2-d_reentrant-wall-c-o connect_nonb.o connect_nonb.cin file encoded from connect_nonb.c: 1: unp. h: 114: Error: redefinition of 'struct in_pktinfo 'make: *** [connect_nonb.o] Error 1

The redefinition of 'struct in_pktinfo 'solution is as follows:

The structure in_pktinfo has been included in the standard header file bits/in. h, and it uses netinet/in. h In unp. H is included. as long as unp. comment out the structure definition comment out in H.

Comment out and try again

make //build the basic library that all programs need


This is the success! The ../libunp. A file is generated.


Step 3: This step is correct.

cd ../libfree  # continue building the basic librarymake


Step 4: Solve the Problem

cd ../libgai   # the getaddrinfo() and getnameinfo() functionsmake

The error message is as follows:

Gcc-g-O2-d_reentrant-wall-c-o getaddrinfo. O getaddrinfo. cgetaddrinfo. c: In function 'getaddrinfo': getaddrinfo. c: 58: Error: 'eai _ addrfamily 'undeclared (first use in this function) getaddrinfo. c: 58: Error: (each undeclared identifier is reported only oncegetaddrinfo. c: 58: Error: for each function it appears in .) getaddrinfo. c: 116: Error: 'eai _ nodata' undeclared (first use in this function) Make: *** [getaddrinfo. o] Error 1

The solution is as follows:

Return to the unpv12e directory:

cd ..gedit configure.in 

Comment out the following line
Libgai_objs = "getaddrinfo. O getnameinfo. O freeaddrinfo. O gai_strerror.o"
Then replace the commented line with the following code.

  1. Libgai_objs =
  2. If test "$ ac_cv_func_getaddrinfo" = no; then
  3. Libgai_objs = "$ libgai_objs getaddrinfo. O"
  4. Fi
  5. If test "$ ac_cv_func_getnameinfo" = no; then
  6. Libgai_objs = "$ libgai_objs getnameinfo. O"
  7. Fi
  8. If test "$ ac_cv_func_freeaddrinfo" = no; then
  9. Libgai_objs = "$ libgai_objs freeaddrinfo. O"
  10. Fi
  11. If test "$ ac_cv_func_gai_strerror" = no; then
  12. Libgai_objs = "$ libgai_objs gai_strerror.o"
  13. Fi
autoconf./configure
gedit Make.defines 

Add the following sentence at the end of the file:
Cflags =-g-O2-d_reentrant-wall-D_gnu_source

Then run the code again:

cd libgai  # continue building the basic librarymake

OK. The problem is solved!

4. Copy the generated libunp. A static library to/usr/lib/and/usr/lib64.

CD .. // return to the unpv12e directory
sudo cp libunp.a /usr/libsudo cp libunp.a /usr/lib64


5. Modify unp. h and copy it and config. h to/usr/include.

Gedit lib/unp. h // modify # include "../config. H" in unp. h to # include "config. H"
sudo cp lib/unp.h /usr/includesudo cp config.h /usr/include

6. Compile the source code

cd ./introgcc daytimetcpcli.c -o daytimetcpcli -lunp

Unlike normal compilation, we need to add the Link Library at the end, and add the libunp. A parameter of-L to remove lib and.. Finally, the parameter-lunp is obtained.

7. Write a program

Compile the code in program installation 6 after compilation, and then the code can be exactly the same as in the book. The code editor or IDE can be selected based on your preferences. Many tutorials on how to build the C programming environment on the Internet.

I chose the command line to write a program using vi, mainly to train myself.


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.