unp.h的安装以及第一个程序的运行
unp.h的安装以及第一个程序的运行
源代码下载以及编译
解压到本地文件夹,如果访问不了GitHub的话就用我搬到gitee的仓库吧
git clone https://gitee.com/Find-ing/unpv13e.git
直接这样就不用解压了
这里可以看看README
按照README上的顺序
首先执行
./configure
然后
cd lib
make
cd ../libfree
make
cd ../libroute
make
如果出现了错误,就再来一次
cd ../lib
make
cd ../libfree
make
cd ../libroute
make
第二遍做完之后,如果出现了如下错误:
[root@docker libfree]# cd ../libroute
[root@docker libroute]# make
gcc -I../lib -g -O2 -D_REENTRANT -Wall -c -o get_rtaddrs.o get_rtaddrs.c
In file included from get_rtaddrs.c:1:
unproute.h:3:10: fatal error: net/if_dl.h: No such file or directory
#include <net/if_dl.h> /* sockaddr_sdl{} */
^~~~~~~~~~~~~
compilation terminated.
make: *** [<builtin>: get_rtaddrs.o] Error 1
哈哈哈,看了半天教程,改了好久也没搞好,仔细看看README,我真是
好,仔细看看README怎么说的
> cd ../libroute # only if your system supports 4.4BSD style routing sockets
> make # only if your system supports 4.4BSD style routing sockets
<font color='red'>only if your system supports 4.4BSD style routing sockets</font>
okok,下一步
可以看到内容已经被编译到unpv13e/libunp.a
里面去了
拷贝
sudo cp libunp.a /usr/lib
sudo cp libunp.a /usr/lib64
# 有的系统可能没有这个文件夹,那就不用拷贝这个了,反正我的有
在
.\lib\unp.h
中将#include "../config.h"
改为#include "config.h"
然后
sudo cp config.h /usr/include
sudo cp lib/unp.h /usr/include
现在是第三阶段,因为我们在运行代码时会报一个错
/tmp/ccM5DNZP.o: In function `main':
test.c:(.text+0x2c): undefined reference to `err_quit'
test.c:(.text+0x58): undefined reference to `err_sys'
test.c:(.text+0xd2): undefined reference to `err_quit'
test.c:(.text+0xfe): undefined reference to `err_sys'
test.c:(.text+0x13a): undefined reference to `err_sys'
test.c:(.text+0x171): undefined reference to `err_sys'
collect2: error: ld returned 1 exit status
原因是err_quit,err_sys等这些函数都是作者自己定义的函数真正的函数在http://www.apuebook.com/code3e.html下载。
把
error.c
的文件名改为myerr.c
在
apue.h
文件最后一行#endif /* _APUE_H */
前加入#include "myerr.c"
在
unp.h
中加入#include "apue.h"
将
apue.h
和myerr.c
文件都拷贝到/usr/include
中去上面这些改改找找的操作各位要是想做就可以做一遍,如果是在不想做,就按照下面的操作
打开
/usr/include
然后删除
unp.h
里面所有内容,然后复制这一段粘进去/* include unph */
/* Our own header. Tabs are set for 4 spaces, not 8 */ #ifndef __unp_h
#define __unp_h
#include "apue.h"
#include "config.h" /* configuration options for current OS */
/* "../config.h" is generated by configure */
#define MAX_LINE 2048
/* If anything changes in the following list of #includes, must change
acsite.m4 also, for configure's tests. */ #include <sys/types.h> /* basic system data types */
#include <sys/socket.h> /* basic socket definitions */
#if TIME_WITH_SYS_TIME
#include <sys/time.h> /* timeval{} for select() */
#include <time.h> /* timespec{} for pselect() */
#else
#if HAVE_SYS_TIME_H
#include <sys/time.h> /* includes <time.h> unsafely */
#else
#include <time.h> /* old system? */
#endif
#endif
#include <netinet/in.h> /* sockaddr_in{} and other Internet defns */
#include <arpa/inet.h> /* inet(3) functions */
#include <errno.h>
#include <fcntl.h> /* for nonblocking */
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h> /* for S_xxx file mode constants */
#include <sys/uio.h> /* for iovec{} and readv/writev */
#include <unistd.h>
#include <sys/wait.h>
#include <sys/un.h> /* for Unix domain sockets */ #ifdef HAVE_SYS_SELECT_H
# include <sys/select.h> /* for convenience */
#endif #ifdef HAVE_SYS_SYSCTL_H
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h> /* OpenBSD prereq for sysctl.h */
#endif
# include <sys/sysctl.h>
#endif #ifdef HAVE_POLL_H
# include <poll.h> /* for convenience */
#endif #ifdef HAVE_SYS_EVENT_H
# include <sys/event.h> /* for kqueue */
#endif #ifdef HAVE_STRINGS_H
# include <strings.h> /* for convenience */
#endif /* Three headers are normally needed for socket/file ioctl's:
* <sys/ioctl.h>, <sys/filio.h>, and <sys/sockio.h>.
*/
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_FILIO_H
# include <sys/filio.h>
#endif
#ifdef HAVE_SYS_SOCKIO_H
# include <sys/sockio.h>
#endif #ifdef HAVE_PTHREAD_H
# include <pthread.h>
#endif #ifdef HAVE_NET_IF_DL_H
# include <net/if_dl.h>
#endif #ifdef HAVE_NETINET_SCTP_H
#include <netinet/sctp.h>
#endif /* OSF/1 actually disables recv() and send() in <sys/socket.h> */
#ifdef __osf__
#undef recv
#undef send
#define recv(a,b,c,d) recvfrom(a,b,c,d,0,0)
#define send(a,b,c,d) sendto(a,b,c,d,0,0)
#endif #ifndef INADDR_NONE
/* $$.Ic INADDR_NONE$$ */
#define INADDR_NONE 0xffffffff /* should have been in <netinet/in.h> */
#endif #ifndef SHUT_RD /* these three POSIX names are new */
#define SHUT_RD 0 /* shutdown for reading */
#define SHUT_WR 1 /* shutdown for writing */
#define SHUT_RDWR 2 /* shutdown for reading and writing */
/* $$.Ic SHUT_RD$$ */
/* $$.Ic SHUT_WR$$ */
/* $$.Ic SHUT_RDWR$$ */
#endif /* *INDENT-OFF* */
#ifndef INET_ADDRSTRLEN
/* $$.Ic INET_ADDRSTRLEN$$ */
#define INET_ADDRSTRLEN 16 /* "ddd.ddd.ddd.ddd\0"
1234567890123456 */
#endif /* Define following even if IPv6 not supported, so we can always allocate
an adequately sized buffer without #ifdefs in the code. */
#ifndef INET6_ADDRSTRLEN
/* $$.Ic INET6_ADDRSTRLEN$$ */
#define INET6_ADDRSTRLEN 46 /* max size of IPv6 address string:
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" or
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd\0"
1234567890123456789012345678901234567890123456 */
#endif
/* *INDENT-ON* */ /* Define bzero() as a macro if it's not in standard C library. */
#ifndef HAVE_BZERO
#define bzero(ptr,n) memset(ptr, 0, n)
/* $$.If bzero$$ */
/* $$.If memset$$ */
#endif /* Older resolvers do not have gethostbyname2() */
#ifndef HAVE_GETHOSTBYNAME2
#define gethostbyname2(host,family) gethostbyname((host))
#endif /* The structure returned by recvfrom_flags() */
struct unp_in_pktinfo {
struct in_addr ipi_addr; /* dst IPv4 address */
int ipi_ifindex;/* received interface index */
};
/* $$.It unp_in_pktinfo$$ */
/* $$.Ib ipi_addr$$ */
/* $$.Ib ipi_ifindex$$ */ /* We need the newer CMSG_LEN() and CMSG_SPACE() macros, but few
implementations support them today. These two macros really need
an ALIGN() macro, but each implementation does this differently. */
#ifndef CMSG_LEN
/* $$.Im CMSG_LEN$$ */
#define CMSG_LEN(size) (sizeof(struct cmsghdr) + (size))
#endif
#ifndef CMSG_SPACE
/* $$.Im CMSG_SPACE$$ */
#define CMSG_SPACE(size) (sizeof(struct cmsghdr) + (size))
#endif /* POSIX requires the SUN_LEN() macro, but not all implementations DefinE
it (yet). Note that this 4.4BSD macro works regardless whether there is
a length field or not. */
#ifndef SUN_LEN
/* $$.Im SUN_LEN$$ */
# define SUN_LEN(su) \
(sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
#endif /* POSIX renames "Unix domain" as "local IPC."
Not all systems DefinE AF_LOCAL and PF_LOCAL (yet). */
#ifndef AF_LOCAL
#define AF_LOCAL AF_UNIX
#endif
#ifndef PF_LOCAL
#define PF_LOCAL PF_UNIX
#endif /* POSIX requires that an #include of <poll.h> DefinE INFTIM, but many
systems still DefinE it in <sys/stropts.h>. We don't want to include
all the STREAMS stuff if it's not needed, so we just DefinE INFTIM here.
This is the standard value, but there's no guarantee it is -1. */
#ifndef INFTIM
#define INFTIM (-1) /* infinite poll timeout */
/* $$.Ic INFTIM$$ */
#ifdef HAVE_POLL_H
#define INFTIM_UNPH /* tell unpxti.h we defined it */
#endif
#endif /* Following could be derived from SOMAXCONN in <sys/socket.h>, but many
kernels still #define it as 5, while actually supporting many more */
#define LISTENQ 1024 /* 2nd argument to listen() */ /* Miscellaneous constants */
#define MAXLINE 4096 /* max text line length */
#define BUFFSIZE 8192 /* buffer size for reads and writes */ /* Define some port number that can be used for our examples */
#define SERV_PORT 9877 /* TCP and UDP */
#define SERV_PORT_STR "9877" /* TCP and UDP */
#define UNIXSTR_PATH "/tmp/unix.str" /* Unix domain stream */
#define UNIXDG_PATH "/tmp/unix.dg" /* Unix domain datagram */
/* $$.ix [LISTENQ]~constant,~definition~of$$ */
/* $$.ix [MAXLINE]~constant,~definition~of$$ */
/* $$.ix [BUFFSIZE]~constant,~definition~of$$ */
/* $$.ix [SERV_PORT]~constant,~definition~of$$ */
/* $$.ix [UNIXSTR_PATH]~constant,~definition~of$$ */
/* $$.ix [UNIXDG_PATH]~constant,~definition~of$$ */ /* Following shortens all the typecasts of pointer arguments: */
#define SA struct sockaddr #ifndef HAVE_STRUCT_SOCKADDR_STORAGE
/*
* RFC 3493: protocol-independent placeholder for socket addresses
*/
#define __SS_MAXSIZE 128
#define __SS_ALIGNSIZE (sizeof(int64_t))
#ifdef HAVE_SOCKADDR_SA_LEN
#define __SS_PAD1SIZE (__SS_ALIGNSIZE - sizeof(u_char) - sizeof(sa_family_t))
#else
#define __SS_PAD1SIZE (__SS_ALIGNSIZE - sizeof(sa_family_t))
#endif
#define __SS_PAD2SIZE (__SS_MAXSIZE - 2*__SS_ALIGNSIZE) struct sockaddr_storage {
#ifdef HAVE_SOCKADDR_SA_LEN
u_char ss_len;
#endif
sa_family_t ss_family;
char __ss_pad1[__SS_PAD1SIZE];
int64_t __ss_align;
char __ss_pad2[__SS_PAD2SIZE];
};
#endif #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
/* default file access permissions for new files */
#define DIR_MODE (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)
/* default permissions for new directories */ typedef void Sigfunc(int); /* for signal handlers */ #define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b)) #ifndef HAVE_ADDRINFO_STRUCT
# include "../lib/addrinfo.h"
#endif #ifndef HAVE_IF_NAMEINDEX_STRUCT
struct if_nameindex {
unsigned int if_index; /* 1, 2, ... */
char *if_name; /* null-terminated name: "le0", ... */
};
/* $$.It if_nameindex$$ */
/* $$.Ib if_index$$ */
/* $$.Ib if_name$$ */
#endif #ifndef HAVE_TIMESPEC_STRUCT
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* and nanoseconds */
};
/* $$.It timespec$$ */
/* $$.Ib tv_sec$$ */
/* $$.Ib tv_nsec$$ */
#endif
/* end unph */ /* prototypes for our own library functions */
int connect_nonb(int, const SA *, socklen_t, int);
int connect_timeo(int, const SA *, socklen_t, int);
int daemon_init(const char *, int);
void daemon_inetd(const char *, int);
void dg_cli(FILE *, int, const SA *, socklen_t);
void dg_echo(int, SA *, socklen_t);
int family_to_level(int);
char *gf_time(void);
void heartbeat_cli(int, int, int);
void heartbeat_serv(int, int, int);
struct addrinfo *host_serv(const char *, const char *, int, int);
int inet_srcrt_add(char *);
u_char *inet_srcrt_init(int);
void inet_srcrt_print(u_char *, int);
void inet6_srcrt_print(void *);
char **my_addrs(int *);
int readable_timeo(int, int);
ssize_t readline(int, void *, size_t);
ssize_t readn(int, void *, size_t);
ssize_t read_fd(int, void *, size_t, int *);
ssize_t recvfrom_flags(int, void *, size_t, int *, SA *, socklen_t *,
struct unp_in_pktinfo *);
Sigfunc *signal_intr(int, Sigfunc *);
int sock_bind_wild(int, int);
int sock_cmp_addr(const SA *, const SA *, socklen_t);
int sock_cmp_port(const SA *, const SA *, socklen_t);
int sock_get_port(const SA *, socklen_t);
void sock_set_addr(SA *, socklen_t, const void *);
void sock_set_port(SA *, socklen_t, int);
void sock_set_wild(SA *, socklen_t);
char *sock_ntop(const SA *, socklen_t);
char *sock_ntop_host(const SA *, socklen_t);
int sockfd_to_family(int);
void str_echo(int);
void str_cli(FILE *, int);
int tcp_connect(const char *, const char *);
int tcp_listen(const char *, const char *, socklen_t *);
void tv_sub(struct timeval *, struct timeval *);
int udp_client(const char *, const char *, SA **, socklen_t *);
int udp_connect(const char *, const char *);
int udp_server(const char *, const char *, socklen_t *);
int writable_timeo(int, int);
ssize_t writen(int, const void *, size_t);
ssize_t write_fd(int, void *, size_t, int); #ifdef MCAST
int mcast_leave(int, const SA *, socklen_t);
int mcast_join(int, const SA *, socklen_t, const char *, u_int);
int mcast_leave_source_group(int sockfd, const SA *src, socklen_t srclen,
const SA *grp, socklen_t grplen);
int mcast_join_source_group(int sockfd, const SA *src, socklen_t srclen,
const SA *grp, socklen_t grplen,
const char *ifname, u_int ifindex);
int mcast_block_source(int sockfd, const SA *src, socklen_t srclen,
const SA *grp, socklen_t grplen);
int mcast_unblock_source(int sockfd, const SA *src, socklen_t srclen,
const SA *grp, socklen_t grplen);
int mcast_get_if(int);
int mcast_get_loop(int);
int mcast_get_ttl(int);
int mcast_set_if(int, const char *, u_int);
int mcast_set_loop(int, int);
int mcast_set_ttl(int, int); void Mcast_leave(int, const SA *, socklen_t);
void Mcast_join(int, const SA *, socklen_t, const char *, u_int);
void Mcast_leave_source_group(int sockfd, const SA *src, socklen_t srclen,
const SA *grp, socklen_t grplen);
void Mcast_join_source_group(int sockfd, const SA *src, socklen_t srclen,
const SA *grp, socklen_t grplen,
const char *ifname, u_int ifindex);
void Mcast_block_source(int sockfd, const SA *src, socklen_t srclen,
const SA *grp, socklen_t grplen);
void Mcast_unblock_source(int sockfd, const SA *src, socklen_t srclen,
const SA *grp, socklen_t grplen);
int Mcast_get_if(int);
int Mcast_get_loop(int);
int Mcast_get_ttl(int);
void Mcast_set_if(int, const char *, u_int);
void Mcast_set_loop(int, int);
void Mcast_set_ttl(int, int);
#endif uint16_t in_cksum(uint16_t *, int); #ifndef HAVE_GETADDRINFO_PROTO
int getaddrinfo(const char *, const char *, const struct addrinfo *,
struct addrinfo **);
void freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
#endif #ifndef HAVE_GETNAMEINFO_PROTO
int getnameinfo(const SA *, socklen_t, char *, size_t, char *, size_t, int);
#endif #ifndef HAVE_GETHOSTNAME_PROTO
int gethostname(char *, int);
#endif #ifndef HAVE_HSTRERROR_PROTO
const char *hstrerror(int);
#endif #ifndef HAVE_IF_NAMETOINDEX_PROTO
unsigned int if_nametoindex(const char *);
char *if_indextoname(unsigned int, char *);
void if_freenameindex(struct if_nameindex *);
struct if_nameindex *if_nameindex(void);
#endif #ifndef HAVE_INET_PTON_PROTO
int inet_pton(int, const char *, void *);
const char *inet_ntop(int, const void *, char *, size_t);
#endif #ifndef HAVE_INET_ATON_PROTO
int inet_aton(const char *, struct in_addr *);
#endif #ifndef HAVE_PSELECT_PROTO
int pselect(int, fd_set *, fd_set *, fd_set *,
const struct timespec *, const sigset_t *);
#endif #ifndef HAVE_SOCKATMARK_PROTO
int sockatmark(int);
#endif #ifndef HAVE_SNPRINTF_PROTO
int snprintf(char *, size_t, const char *, ...);
#endif /* prototypes for our own library wrapper functions */
void Connect_timeo(int, const SA *, socklen_t, int);
int Family_to_level(int);
struct addrinfo *Host_serv(const char *, const char *, int, int);
const char *Inet_ntop(int, const void *, char *, size_t);
void Inet_pton(int, const char *, void *);
char *If_indextoname(unsigned int, char *);
unsigned int If_nametoindex(const char *);
struct if_nameindex *If_nameindex(void);
char **My_addrs(int *);
ssize_t Read_fd(int, void *, size_t, int *);
int Readable_timeo(int, int);
ssize_t Recvfrom_flags(int, void *, size_t, int *, SA *, socklen_t *,
struct unp_in_pktinfo *);
Sigfunc *Signal(int, Sigfunc *);
Sigfunc *Signal_intr(int, Sigfunc *);
int Sock_bind_wild(int, int);
char *Sock_ntop(const SA *, socklen_t);
char *Sock_ntop_host(const SA *, socklen_t);
int Sockfd_to_family(int);
int Tcp_connect(const char *, const char *);
int Tcp_listen(const char *, const char *, socklen_t *);
int Udp_client(const char *, const char *, SA **, socklen_t *);
int Udp_connect(const char *, const char *);
int Udp_server(const char *, const char *, socklen_t *);
ssize_t Write_fd(int, void *, size_t, int);
int Writable_timeo(int, int); /* prototypes for our Unix wrapper functions: see {Sec errors} */
void *Calloc(size_t, size_t);
void Close(int);
void Dup2(int, int);
int Fcntl(int, int, int);
void Gettimeofday(struct timeval *, void *);
int Ioctl(int, int, void *);
pid_t Fork(void);
void *Malloc(size_t);
int Mkstemp(char *);
void *Mmap(void *, size_t, int, int, int, off_t);
int Open(const char *, int, mode_t);
void Pipe(int *fds);
ssize_t Read(int, void *, size_t);
void Sigaddset(sigset_t *, int);
void Sigdelset(sigset_t *, int);
void Sigemptyset(sigset_t *);
void Sigfillset(sigset_t *);
int Sigismember(const sigset_t *, int);
void Sigpending(sigset_t *);
void Sigprocmask(int, const sigset_t *, sigset_t *);
char *Strdup(const char *);
long Sysconf(int);
void Sysctl(int *, u_int, void *, size_t *, void *, size_t);
void Unlink(const char *);
pid_t Wait(int *);
pid_t Waitpid(pid_t, int *, int);
void Write(int, void *, size_t); /* prototypes for our stdio wrapper functions: see {Sec errors} */
void Fclose(FILE *);
FILE *Fdopen(int, const char *);
char *Fgets(char *, int, FILE *);
FILE *Fopen(const char *, const char *);
void Fputs(const char *, FILE *); /* prototypes for our socket wrapper functions: see {Sec errors} */
int Accept(int, SA *, socklen_t *);
void Bind(int, const SA *, socklen_t);
void Connect(int, const SA *, socklen_t);
void Getpeername(int, SA *, socklen_t *);
void Getsockname(int, SA *, socklen_t *);
void Getsockopt(int, int, int, void *, socklen_t *);
#ifdef HAVE_INET6_RTH_INIT
int Inet6_rth_space(int, int);
void *Inet6_rth_init(void *, socklen_t, int, int);
void Inet6_rth_add(void *, const struct in6_addr *);
void Inet6_rth_reverse(const void *, void *);
int Inet6_rth_segments(const void *);
struct in6_addr *Inet6_rth_getaddr(const void *, int);
#endif
#ifdef HAVE_KQUEUE
int Kqueue(void);
int Kevent(int, const struct kevent *, int,
struct kevent *, int, const struct timespec *);
#endif
void Listen(int, int);
#ifdef HAVE_POLL
int Poll(struct pollfd *, unsigned long, int);
#endif
ssize_t Readline(int, void *, size_t);
ssize_t Readn(int, void *, size_t);
ssize_t Recv(int, void *, size_t, int);
ssize_t Recvfrom(int, void *, size_t, int, SA *, socklen_t *);
ssize_t Recvmsg(int, struct msghdr *, int);
int Select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
void Send(int, const void *, size_t, int);
void Sendto(int, const void *, size_t, int, const SA *, socklen_t);
void Sendmsg(int, const struct msghdr *, int);
void Setsockopt(int, int, int, const void *, socklen_t);
void Shutdown(int, int);
int Sockatmark(int);
int Socket(int, int, int);
void Socketpair(int, int, int, int *);
void Writen(int, void *, size_t); void err_dump(const char *, ...);
void err_msg(const char *, ...);
void err_quit(const char *, ...);
void err_ret(const char *, ...);
void err_sys(const char *, ...); #endif /* __unp_h */新建一个
apue.h
,把下面这段粘贴进去/*
* Our own header, to be included before all standard system headers.
*/
#ifndef _APUE_H
#define _APUE_H #define _POSIX_C_SOURCE 200809L #if defined(SOLARIS) /* Solaris 10 */
#define _XOPEN_SOURCE 600
#else
#define _XOPEN_SOURCE 700
#endif #include <sys/types.h> /* some systems still require this */
#include <sys/stat.h>
#include <sys/termios.h> /* for winsize */
#if defined(MACOS) || !defined(TIOCGWINSZ)
#include <sys/ioctl.h>
#endif #include <stdio.h> /* for convenience */
#include <stdlib.h> /* for convenience */
#include <stddef.h> /* for offsetof */
#include <string.h> /* for convenience */
#include <unistd.h> /* for convenience */
#include <signal.h> /* for SIG_ERR */ #define MAXLINE 4096 /* max line length */ /*
* Default file access permissions for new files.
*/
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) /*
* Default permissions for new directories.
*/
#define DIR_MODE (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH) typedef void Sigfunc(int); /* for signal handlers */ #define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b)) /*
* Prototypes for our own functions.
*/
char *path_alloc(size_t *); /* {Prog pathalloc} */
long open_max(void); /* {Prog openmax} */ int set_cloexec(int); /* {Prog setfd} */
void clr_fl(int, int);
void set_fl(int, int); /* {Prog setfl} */ void pr_exit(int); /* {Prog prexit} */ void pr_mask(const char *); /* {Prog prmask} */
Sigfunc *signal_intr(int, Sigfunc *); /* {Prog signal_intr_function} */ void daemonize(const char *); /* {Prog daemoninit} */ void sleep_us(unsigned int); /* {Ex sleepus} */
ssize_t readn(int, void *, size_t); /* {Prog readn_writen} */
ssize_t writen(int, const void *, size_t); /* {Prog readn_writen} */ int fd_pipe(int *); /* {Prog sock_fdpipe} */
int recv_fd(int, ssize_t (*func)(int,
const void *, size_t)); /* {Prog recvfd_sockets} */
int send_fd(int, int); /* {Prog sendfd_sockets} */
int send_err(int, int,
const char *); /* {Prog senderr} */
int serv_listen(const char *); /* {Prog servlisten_sockets} */
int serv_accept(int, uid_t *); /* {Prog servaccept_sockets} */
int cli_conn(const char *); /* {Prog cliconn_sockets} */
int buf_args(char *, int (*func)(int,
char **)); /* {Prog bufargs} */ int tty_cbreak(int); /* {Prog raw} */
int tty_raw(int); /* {Prog raw} */
int tty_reset(int); /* {Prog raw} */
void tty_atexit(void); /* {Prog raw} */
struct termios *tty_termios(void); /* {Prog raw} */ int ptym_open(char *, int); /* {Prog ptyopen} */
int ptys_open(char *); /* {Prog ptyopen} */
#ifdef TIOCGWINSZ
pid_t pty_fork(int *, char *, int, const struct termios *,
const struct winsize *); /* {Prog ptyfork} */
#endif int lock_reg(int, int, int, off_t, int, off_t); /* {Prog lockreg} */ #define read_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
#define readw_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
#define write_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
#define writew_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
#define un_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len)) pid_t lock_test(int, int, off_t, int, off_t); /* {Prog locktest} */ #define is_read_lockable(fd, offset, whence, len) \
(lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)
#define is_write_lockable(fd, offset, whence, len) \
(lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0) void err_msg(const char *, ...); /* {App misc_source} */
void err_dump(const char *, ...) __attribute__((noreturn));
void err_quit(const char *, ...) __attribute__((noreturn));
void err_cont(int, const char *, ...);
void err_exit(int, const char *, ...) __attribute__((noreturn));
void err_ret(const char *, ...);
void err_sys(const char *, ...) __attribute__((noreturn)); void log_msg(const char *, ...); /* {App misc_source} */
void log_open(const char *, int, int);
void log_quit(const char *, ...) __attribute__((noreturn));
void log_ret(const char *, ...);
void log_sys(const char *, ...) __attribute__((noreturn));
void log_exit(int, const char *, ...) __attribute__((noreturn)); void TELL_WAIT(void); /* parent/child from {Sec race_conditions} */
void TELL_PARENT(pid_t);
void TELL_CHILD(pid_t);
void WAIT_PARENT(void);
void WAIT_CHILD(void);
#include "myerr.c"
#endif /* _APUE_H */再新建一个
myerr.c
,把下面内容填充进去#include "apue.h"
#include <errno.h> /* for definition of errno */
#include <stdarg.h> /* ISO C variable aruments */ static void err_doit(int, int, const char *, va_list); /*
* Nonfatal error related to a system call.
* Print a message and return.
*/
void
err_ret(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
} /*
* Fatal error related to a system call.
* Print a message and terminate.
*/
void
err_sys(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
exit(1);
} /*
* Nonfatal error unrelated to a system call.
* Error code passed as explict parameter.
* Print a message and return.
*/
void
err_cont(int error, const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, error, fmt, ap);
va_end(ap);
} /*
* Fatal error unrelated to a system call.
* Error code passed as explict parameter.
* Print a message and terminate.
*/
void
err_exit(int error, const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, error, fmt, ap);
va_end(ap);
exit(1);
} /*
* Fatal error related to a system call.
* Print a message, dump core, and terminate.
*/
void
err_dump(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
abort(); /* dump core and terminate */
exit(1); /* shouldn't get here */
} /*
* Nonfatal error unrelated to a system call.
* Print a message and return.
*/
void
err_msg(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
} /*
* Fatal error unrelated to a system call.
* Print a message and terminate.
*/
void
err_quit(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
exit(1);
} /*
* Print a message and return to caller.
* Caller specifies "errnoflag".
*/
static void
err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
char buf[MAXLINE]; vsnprintf(buf, MAXLINE-1, fmt, ap);
if (errnoflag)
snprintf(buf+strlen(buf), MAXLINE-strlen(buf)-1, ": %s",
strerror(error));
strcat(buf, "\n");
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(NULL); /* flushes all stdio output streams */
}
然后编译就成功了
然后运行就失败了
解决方案:
sudo apt-get install xinetd
# sudo yum install xinetd
vi /etc/xinetd.d/daytime
把disable = yes
改成disable = no
service daytime
{
disable = no
type = INTERNA:L
id = daytime-stream
socket_type = stream
protocol = tcp
user = root
wait = no
}
接下来:
sudo service xinetd reload
netstat -a -t | grep daytime # 确认服务正在运行
如果不行,报错了,就看看这篇照着做肯定行https://www.cnblogs.com/yewandemty/p/5396391.html(一般centOS可能会报错,Ubuntu一般不会报错)
照着做完之后代码运行成功:
我运行的代码:
#include "unp.h"
int
main(int argc, char **argv)
{
int sockfd, n;
char recvline[MAXLINE + 1];
struct sockaddr_in servaddr;
if (argc != 2)
err_quit("usage: a.out <IPaddress>");
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err_sys("socket 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);
}
unp.h的安装以及第一个程序的运行的更多相关文章
- Java和eclipxe的安装以及第一个程序
首先我们需要下载java开发工具包JDK,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 注意事项:安装 ...
- [视频解说]Java(JDK的下载安装及第一个程序执行)
(JDK的下载安装及第一个程序执行) 内容:Java JDK 的安装以及HelloWorld 程序的执行 欢迎童鞋们前往围观 http://v.youku.com/v_show/id_XODA3Mzk ...
- /var/run/yum.pid 已被锁定,PID 为 XXXX 的另一个程序正在运行。
安装st-load时, 终端提示 “/var/run/yum.pid 已被锁定,PID 为 13908 的另一个程序正在运行.” 解决方法:直接在终端运行 rm -f /var/run/yum.pid ...
- Loadrunner11录制过程中报错:由于另一个程序正在运行中,此操作无法完成
第一次安装LR11录制没有问题,第二次录制也没有问题,后面第二天录制就出现了如下截图的现象 报错内容:由于另一个程序正在运行中,此操作无法完成.请选择“切换到”来激活正在运行中的的程序,并更正问题 ...
- 运行yum时出现/var/run/yum.pid已被锁定,PID为xxxx的另一个程序正在运行的问题解决
出现问题 [root@localhost ~]#yum update 已加载插件: fastestmirror,security /var/run/yum.pid已被锁定,PID为1610的另一个程序 ...
- Java(1)开发环境配置及第一个程序Hello World
作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201468.html 博客主页:https://www.cnblogs.com/testero ...
- SpringBoot环境搭建及第一个程序运行(详细!)
spring boot简介 spring boot框架抛弃了繁琐的xml配置过程,采用大量的默认配置简化我们的开发过程. 所以采用Spring boot可以非常容易和快速地创建基于Spring 框架的 ...
- C# Winform防止一个程序重复运行
1: //在写一些服务型的软件的时候,你可能不希望一个操作系统里有两个副本在运行,这样也许会扰乱你的操作.这时,你就需要限制程序的副本.下面的这个方法,很简单的就可以实现上述功能. using Sys ...
- Web自动化——介绍与安装以及第一个web自动化程序(一)
1. 为什么要做Web自动化测试 什么是web自动化测试 让程序代替人,去验证网页上功能的过程 web自动化测试与手工测试的比较 web自动化测试执行的测试用例是手工功能测试的子集 web自动化测试的 ...
- flask安装及第一个程序
1.flask是一个轻量级的python web框架 ·1.Flask 依赖两个外部库: Jinja2 模板引擎和 Werkzeug WSGI 套件 ·2.安装: # easy_install fla ...
随机推荐
- AI实战 | 手把手带你打造智能待办助手
背景 大家好,我是努力的小雨.今天我想分享一下搭建待办助手的经历.起初,我并没有什么特别的创意点子.但在4月16日的百度Create大会上,我看到了小度的大模型加持使得其变得更加智能.我被一场示例所震 ...
- 80x86汇编—汇编程序基本框架
文章目录 First Program 指令系统 伪指令 数值表达式 程序框架解释 int 21 中断 通过一个基本框架解释各个指令和用处,方便复习.所以我认为最好的学习顺序就是先看一段完整的汇编代码程 ...
- 80x86汇编—寻址方式
文章目录 术语解释 8086寻址方式 直数寻址 寄存器间接寻址 寄存器相对寻址 基址变址寻址 比例变址寻址方式 基址比例变址寻址方式 术语解释 EA:有效地址,通过段地址:偏移地址组合得到的Effec ...
- kubernetes 之 Rolling Update 滚动升级
滚动升级 1.错误的yml文件 [machangwei@mcwk8s-master ~]$ cat mcwHttpd.yml apiVersion: apps/v1 kind: Deployment ...
- 一款基于C#开发的通讯调试工具(支持Modbus RTU、MQTT调试)
前言 今天大姚给大家分享一款基于C#.WPF.Prism.MaterialDesign.HandyControl开发的通讯调试工具(支持Modbus RTU.MQTT调试,界面色彩丰富):Wu.Com ...
- Spring 面向切面编程AOP 详细讲解
1. Spring 面向切面编程AOP 详细讲解 @ 目录 1. Spring 面向切面编程AOP 详细讲解 每博一文案 2. AOP介绍说明 2.1 AOP的七大术语 2.2 AOP 当中的 切点表 ...
- Django——admin后台上传文件
from django.db import models class Mytb(models.Model): file = models.FileField(upload_to='uploads/') ...
- RTMP推流FLV插入自定义SEI数据总结
一.需求 在RTMP推送的流中添加一个接口,可以添加自定义的数据(一段字节数组). 经过分析,在H264的流中可以通过SEI添加自定义数据,下面是实施的总结 二.实施 1)准备工具 RTMP推流客户端 ...
- switch case 跳转表
一.事情来源 事情来源是一段奇怪的代码,代码如下 int x = 1000; switch (x) { case 1000: { NSLog(@"%d", 1); } case 2 ...
- k8s——pod的yaml文件
理解什么是pod pod基于deployment创建,删除deployment,pod也会被删除 基础pod的yaml文件的资源清单 点击查看列表 | 参数名 | 类型 | 字段说明 | | ---- ...