《 UNIX網絡編程》源碼的使用

學習編程這東西,看代碼,改代碼,運行代碼這樣才能學到實際東西!本書說在www.unpbook.com能夠獲取源碼,不過打不開!因此googlehtml

unpv13e.tar.gz

並在網絡上找到了:源碼:http://code.google.com/p/stid/downloads/detail?name=unpv13e.tar.gz&can=2&q=linux

 

如今就是使用的問題了!

關於在windows下的問題

既然是unix網絡編程,直接在windows下運行固然是行不通的!(問:那還學這個幹嗎!?? 你說呢,咱們學的是思想和方法,window網絡編程其實差別不大)git

不過在windows下裝個UNIX模擬器cygwin貌似是一個不錯的選擇!也有人用這個模擬器搭建UNIX網絡編程平臺,不過對初學者有必定難度!(cygwin是個好東西呀!)typescript

 

關於在Linux 下的使用問題

Linux也是UNIX,固然沒問題!如今說明如何使用書本源碼unpv13e.tar.gz問題。編程

首先,解壓源碼,這裏是解壓到當前目錄:acer@ubuntu:~$ tar -xvf unpv13e.tar.gzubuntu

其後,建議去閱讀解壓後目錄unpv13e下的README文檔!windows

README文檔內容以下:(ubuntu下執行紅色部分便可)服務器

QUICK AND DIRTY
===============

Execute the following from the src/ directory:

   ./configure    # try to figure out all implementation differences

    cd lib         # build the basic library that all programs need
    make           # use "gmake" everywhere on BSD/OS systems

    cd ../libfree  # continue building the basic library make 
    cd ../libroute # only if your system supports 4.4BSD style routing sockets
    make           # only if your system supports 4.4BSD style routing sockets

    cd ../libxti   # only if your system supports XTI
    make           # only if your system supports XTI

  cd ../intro    # build and test a basic client program
    make daytimetcpcli
    ./daytimetcpcli 127.0.0.1

If all that works, you're all set to start compiling individual programs.

Notice that all the source code assumes tabs every 4 columns, not 8.

MORE DETAILS
============

5.  If you need to make any changes to the "unp.h" header, notice that it
    is a hard link in each directory, so you only need to change it once.

6.  Go into the "lib/" directory and type "make".  This builds the library
    "libunp.a" that is required by almost all of the programs.  There may
    be compiler warnings (see NOTES below).  This step is where you'll find
    all of your system's dependencies, and you must just update your cf/
    files from step 1, rerun "config" and do this step again.

6.  Go into the "libfree/" directory and type "make".  This adds to the
    "libunp.a" library.  The files in this directory do not #include
    the "unp.h" header, as people may want to use these functions
    independent of the book's examples.

8.  Once the library is made from steps 5 and 6, you can then go into any
    of the source code directories and make whatever program you are
    interested in.  Note that the horizontal rules at the beginning and
    end of each program listing in the book contain the directory name and
    filename.

    BEWARE: Not all programs in each directory will compile on all systems
    (e.g., the file src/advio/recvfromflags.c will not compile unless your
    system supports the IP_RECVDSTADDR socket option).  Also, not all files
    in each directory are included in the book.  Beware of any files with
    "test" in the filename: they are probably a quick test program that I
    wrote to check something, and may or may not work.

NOTES
-----

- Many systems do not have correct function prototypes for the socket
  functions, and this can cause many warnings during compilation.
  For example, Solaris 2.5 omits the "const" from the 2nd argument
  to connect().  Lots of systems use "int" for the length of socket
  address structures, while Posix.1g specifies "size_t".  Lots of
  systems still have the pointer argument to [sg]etsockopt() as a
  "char *" instead of a "void *", and this also causes warnings.

- SunOS 4.1.x: If you are using Sun's acc compiler, you need to run
  the configure program as

        CC=acc CFLAGS=-w CPPFLAGS=-w ./configure

  Failure to do this results in numerous system headers (<sys/sockio.h>)
  not being found during configuration, causing compile errors later.

- If your system supports IPv6 and you want to run the examples in the
  book using hostnames, you must install the latest BIND release.  You
  can get it from ftp://ftp.vix.com/pub/bind/release.  All you need from
  this release is a resolver library that you should then add to the
  LDLIBS and LDLIBS_THREADS lines.

- IPv6 support is still in its infancy.  There may be differences
  between the IPv6 sockets API specifications and what the vendor
  provides.  This may require hand tweaking, but should get better
  over time.

- If your system supports an older draft of the Posix pthreads standard,
  but configure detects the support of pthreads, you will have to disable
  this by hand.  Digital Unix V3.2C has this problem, for example, as it
  supports draft 4, not the final draft.

  To fix this, remove wrappthread.o from LIB_OBJS in "Make.defines" and
  don't try to build and run any of the threads programs.

COMMON DIFFERENCES
------------------

These are the common differences that I see in various headers that are
not "yet" at the level of Posix.1g or X/Open XNS Issue 5.

- getsockopt() and setsockopt(): 5th argument is not correct type.

- t_bind(): second argument is missing "const".

- t_connect(): second argument is missing "const".

- t_open(): first argument is missing "const".

- t_optmsmg(): second argument is missing "const".

- If your <xti.h> defines the members of the t_opthdr{} as longs,
  instead of t_uscalar_t, some of the printf formats of these value
  might generate warnings from your compiler, since you are printing
  a long without a corresponding long format specifier.

注意:之後咱們都用make編譯源代碼
網絡

最後運行:./daytimetcpcli 127.0.0.1以下app

acer@ubuntu:~/jwt/unpv13e/intro$ ./daytimetcpcli 127.0.0.1
connect error: Connection refused

發現報錯:connect error: Connection refused

這種狀況說明服務器沒有啓動daytime服務!那Ubuntu如何啓動,daytime服務呢?

Ubuntu啓動daytime服務

1. sudo apt-get install xinetd
2. sudo vi /etc/xinetd.d/daytime, 把 disable = yes to no
3. sudo /etc/init.d/xinetd restart

 

在運行 ./daytimetcpcli 127.0.0.1

出現結果!!

參考:http://blog.csdn.net/baikaishui525/article/details/7013907

根目錄下生成Make.defines以下:

#
# This file is generated by autoconf from "Make.defines.in".
#
# This is the "Make.defines" file that almost every "Makefile" in the
# source directories below this directory include.
# The "../" in the pathnames actually refer to this directory, since
# "make" is executed in all the subdirectories of this directory.
#
# System = i686-pc-linux-gnu

CC = gcc
CFLAGS = -I../lib -g -O2 -D_REENTRANT -Wall
LIBS = ../libunp.a -lpthread
LIBS_XTI = ../libunpxti.a ../libunp.a -lpthread
RANLIB = ranlib

# Following is the main library, built from all the object files
# in the lib/ and libfree/ directories.
LIBUNP_NAME = ../libunp.a

# Following is the XTI library, built from all the object files
# in the libxti/ directory.
LIBUNPXTI_NAME = ../libunpxti.a

# Following are all the object files to create in the lib/ directory.
LIB_OBJS =  connect_nonb.o connect_timeo.o daemon_inetd.o daemon_init.o dg_cli.o dg_echo.o error.o get_ifi_info.o gf_time.o host_serv.o family_to_level.o mcast_leave.o mcast_join.o mcast_get_if.o mcast_get_loop.o mcast_get_ttl.o mcast_set_if.o mcast_set_loop.o mcast_set_ttl.o my_addrs.o read_fd.o readline.o readn.o readable_timeo.o rtt.o signal.o signal_intr.o sock_bind_wild.o sock_cmp_addr.o sock_cmp_port.o sock_ntop.o sock_ntop_host.o sock_get_port.o sock_set_addr.o sock_set_port.o sock_set_wild.o sockfd_to_family.o str_cli.o str_echo.o tcp_connect.o tcp_listen.o tv_sub.o udp_client.o udp_connect.o udp_server.o wraplib.o wrapsock.o wrapstdio.o wrappthread.o wrapunix.o write_fd.o writen.o writable_timeo.o

# Following are all the object files to create in the libfree/ directory.
LIBFREE_OBJS =  in_cksum.o inet_ntop.o inet_pton.o

# Following are all the object files to create in the libgai/ directory.
LIBGAI_OBJS = 

# Following are all the object files to create in the libroute/ directory.
LIBROUTE_OBJS = get_rtaddrs.o if_indextoname.o if_nameindex.o if_nametoindex.o net_rt_iflist.o net_rt_dump.o sock_masktop.o

# Following are all the object files to create in the libxti/ directory.
LIBXTI_OBJS =  wrapxti.o xti_accept.o xti_flags_str.o xti_getopt.o xti_ntop.o xti_ntop_host.o xti_rdwr.o xti_setopt.o xti_str_opts.o xti_tlook_str.o

CLEANFILES = core core.* *.core *.o temp.* *.out typescript* \
        *.lc *.lh *.bsdi *.sparc *.uw

 

lib文件夾makefie內容:

include ../Make.defines

all: ${LIB_OBJS}
ar rv ${LIBUNP_NAME} $?
${RANLIB} ${LIBUNP_NAME}

clean:
rm -f ${PROGS} ${CLEANFILES}

 

intro文件夾下的lib文件Makefile以下:

include ../Make.defines

PROGS =    daytimetcpcli daytimetcpcli1 daytimetcpcli2 daytimetcpcli3 \
        daytimetcpsrv daytimetcpsrv1 daytimetcpsrv2 daytimetcpsrv3 \
        daytimetcpcliv6 daytimetcpsrvv6 \
        byteorder

all:    ${PROGS}

daytimetcpcli:    daytimetcpcli.o
        ${CC} ${CFLAGS} -o $@ daytimetcpcli.o ${LIBS}

daytimetcpcli1:    daytimetcpcli1.o
        ${CC} ${CFLAGS} -o $@ daytimetcpcli1.o ${LIBS}

daytimetcpcli2:    daytimetcpcli2.o
        ${CC} ${CFLAGS} -o $@ daytimetcpcli2.o ${LIBS}

daytimetcpcli3:    daytimetcpcli3.o
        ${CC} ${CFLAGS} -o $@ daytimetcpcli3.o ${LIBS}

daytimetcpsrv:    daytimetcpsrv.o
        ${CC} ${CFLAGS} -o $@ daytimetcpsrv.o ${LIBS}

daytimetcpsrv1:    daytimetcpsrv1.o
        ${CC} ${CFLAGS} -o $@ daytimetcpsrv1.o ${LIBS}

daytimetcpsrv2:    daytimetcpsrv2.o
        ${CC} ${CFLAGS} -o $@ daytimetcpsrv2.o ${LIBS}

daytimetcpsrv3:    daytimetcpsrv3.o
        ${CC} ${CFLAGS} -o $@ daytimetcpsrv3.o ${LIBS}

daytimetcpcliv6:    daytimetcpcliv6.o
        ${CC} ${CFLAGS} -o $@ daytimetcpcliv6.o ${LIBS}

daytimetcpsrvv6:    daytimetcpsrvv6.o
        ${CC} ${CFLAGS} -o $@ daytimetcpsrvv6.o ${LIBS}

byteorder:    byteorder.o
        ${CC} ${CFLAGS} -o $@ byteorder.o ${LIBS}

clean:
        rm -f ${PROGS} ${CLEANFILES}

 

在select下,編譯某個包含unp.h的文件。

 gcc -c strcliselect01.c  -I ../lib

 

ubuntu搭建:

http://lingshaohuprose.blog.163.com/blog/static/169579782201232505248560/

http://blog.csdn.net/hongkangwl/article/details/24013863

http://blog.sina.com.cn/s/blog_a43aba560101a7gf.html

相關文章
相關標籤/搜索