|
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif
#define LINELEN 128
extern int errno;
TCPdaytime(const char *host, const char *service)
{
char buf[LINELEN+1];
int s,n;
s = connectTCP(host, service);
while((n = read(s, buf, LINELEN)) > 0)
{
buf[n] = '\0';
(void)fputs(buf, stdout);
}
}
int errexit(const char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
exit(1);
}
int connectsock(const char *host, const char *service, const char *transport)
{
struct hostent *phe;
struct servent *pse;
struct protoent *ppe;
struct sockaddr_in sin;
int s, type;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
if(pse = getservbyname(service,transport))
sin.sin_port = pse->s_port;
else if((sin.sin_port=htons((unsigned short)atoi(service))) == 0)
errexit("can't get \"%s\" service entry\n",service);
if(phe = gethostbyname(host))
memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
else if((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
errexit("can't get \"%s\" host entry\n",host);
if((ppe = getprotobyname(transport)) == 0)
errexit("can't get \"%s\" protocol entry\n",transport);
if(strcmp(transport,"udp") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
s = socket(PF_INET, type, ppe->p_proto);
if(s<0)
errexit("can't create socket : %s\n",strerror(errno));
if(connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
errexit("can't connect to %s.%s: %s\n", host, service, strerror(errno));
return s;
}
int connectTCP(const char *host, const char *service)
{
return connectsock(host, service, "tcp");
}
int main(int argc, char *argv[])
{
char *host = "localhost";
char *service = "daytime";
switch(argc)
{
case 1:
host = "localhost";
break;
case 3:
service = argv[2];
case 2:
host = argv[1];
break;
default:
fprintf(stderr, "usage: TCPdaytime[host[port]]\n");
exit(1);
}
TCPdaytime(host, service);
exit(0);
}
以上是程序代码,用于TCP连接并且获取一条信息。
以下是报错信息,大家帮忙分析一下,是什么原因造成的编译不成功,如何解决?
cd '/tmp/daytimetcp/debug' && WANT_AUTOCONF_2_5="1" WANT_AUTOMAKE_1_6="1" gmake -k
gmake all-recursive
Making all in src
正在编译 daytimetcp.c (gcc)
正在连接 daytimetcp (libtool)
正在连接 daytimetcp (gcc)
/usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference in daytimetcp.o
/lib/libc.so.6: could not read symbols: Bad value
collect2: ld returned 1 exit status
gmake[2]: *** [daytimetcp] 错误 1
gmake[2]: 由于错误目标“all”并未重新创建。
gmake[2]: Nothing to be done for `all-am'.
gmake[1]: *** [all-recursive] 错误 1
gmake: *** [all] 错误 2
*** 退出状态:2 ***
本人刚刚接触linux程序设计,望高人指点,谢谢! |
|