UNIX Network Programming (Volume 1) first program compilation process

Source: Internet
Author: User
Tags socket error

The UNIX volume I bought last summer has only begun to look very ashamed, and I am ashamed that the first program has been on and off for a few days, so I have to write a program, and I am looking for a job soon, the following describes how to run the first program in this book:

Search various blogs

I use Ubuntu 13.04 as the system.

Thanks to the blog: After the portal, you must record your various problems and solutions. When we are new to a field, I often have a lot of time delay in getting started. I still have a problem after I have prepared everything in a good way.

Kapop @ kapop :~ /CPP $ g ++. CPP-o a-lunp/tmp/cczlhmnw. o: In the 'main' function:. CPP :(. text + 0x30): For 'err _ quit (char const *,...) 'undefined reference. CPP :(. text + 0x67): For 'err _ sys (char const *,...) 'undefined reference. CPP :(. text + 0xd9): For 'err _ quit (char const *,...) 'undefined reference. CPP :(. text + 0x108): For 'err _ sys (char const *,...) 'undefined reference. CPP :(. text + 0x142): For 'err _ sys (char const *,...) 'undefined reference. CPP :(. text + 0x181): For 'err _ sys (char const *,...) 'undefined reference collect2: Error: LD returns 1 Kapo P @ kapop :~ /CPP $ g ++ A. cpp unp. C-o a-lunpkapop @ kapop :~ /CPP $./A 127.0.0.103 Aug 2013 15:53:32 CST

The content of the UNP. c file is as follows:

#include<errno.h>/* for definition of errno */#include<stdarg.h>/* ANSI C header file *///#include"ourhdr.h"#include    "unp.h"static voiderr_doit(int, const char *, va_list);char*pname = NULL;/* caller can set this from argv[0] *//* Nonfatal error related to a system call. * Print a message and return. */void/* $f err_ret $ */err_ret(const char *fmt, ...){va_listap;va_start(ap, fmt);err_doit(1, fmt, ap);va_end(ap);return;}/* Fatal error related to a system call. * Print a message and terminate. */void/* $f err_sys $ */err_sys(const char *fmt, ...){va_listap;va_start(ap, fmt);err_doit(1, fmt, ap);va_end(ap);exit(1);}/* Fatal error related to a system call. * Print a message, dump core, and terminate. */void/* $f err_dump $ */err_dump(const char *fmt, ...){va_listap;va_start(ap, fmt);err_doit(1, 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/* $f err_msg $ */err_msg(const char *fmt, ...){va_listap;va_start(ap, fmt);err_doit(0, fmt, ap);va_end(ap);return;}/* Fatal error unrelated to a system call. * Print a message and terminate. */void/* $f err_quit $ */err_quit(const char *fmt, ...){va_listap;va_start(ap, fmt);err_doit(0, fmt, ap);va_end(ap);exit(1);}/* Print a message and return to caller. * Caller specifies "errnoflag". */static voiderr_doit(int errnoflag, const char *fmt, va_list ap){interrno_save;charbuf[MAXLINE];errno_save = errno;/* value caller might want printed */vsprintf(buf, fmt, ap);if (errnoflag)sprintf(buf+strlen(buf), ": %s", strerror(errno_save));strcat(buf, "\n");fflush(stdout);/* in case stdout and stderr are the same */fputs(buf, stderr);fflush(stderr);/* SunOS 4.1.* doesn't grok NULL argument */return;}

A. cpp is as follows:

#include "unp.h"#include<iostream>#include<algorithm>#include<string>#include<cstdlib>using namespace std;int main(int argc, char **argv){int sockfd, n;char recvline[MAXLINE + 1];struct sockaddr_in servaddr;if (argc != 2) {//cout << "ni mei can shu shao le!" << endl;err_quit("usage: a.out <IPaddress>");}if ( (sockfd = socket(AF_INET, SOCK_STREAM,0)) < 0) {//cout << "Fuck ing life ! " << endl;err_sys("socket error");}bzero(&servaddr, sizeof(servaddr));servaddr.sin_family = AF_INET;servaddr.sin_port =  htons(13);if (inet_pton(AF_INET,argv[1],&servaddr.sin_addr) <= 0) { //cout << "inet_pton error for" <<endl;err_quit("inet_pton error for %s",argv[1]);}if (connect(sockfd,(SA *) &servaddr,sizeof(servaddr)) <0) {/*cout << "connect error"<< endl ;*/err_sys("connect error");}while( (n = read(sockfd, recvline, MAXLINE)) >0 ){recvline[n]=0; if (fputs(recvline,stdout) == EOF) {//cout << "Fputs error" << endl ;err_sys("fputss error");}}if (n < 0 ) {   //cout << "read error" <<endl;err_sys("read error");}exit(0);//return 0;}

Thanks to the sharing spirit of the Internet, and to the experts who have recorded problems and provided solutions, let me wait for * nix cainiao to make progress.

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.