- 分享
- 0
- 人气
- 0
- 主题
- 23
- 帖子
- 124
- UID
- 186218
- 积分
- 122
- 阅读权限
- 13
- 注册时间
- 2008-12-16
- 最后登录
- 2012-3-17
- 在线时间
- 358 小时
|
各位,我想请问下该如何在Solaris的环境下 编译 C 文件。
[img][/img]- /* TCPdaytime.c - TCPdaytime, main */
- #include <sys/errno.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- int TCPdaytime(const char *host, const char *service);
- int errexit(const char *format, ...);
- int connectTCP(const char *host, const char *service);
- #define LINELEN 128
- /*------------------------------------------------------------------------
- * main - TCP client for DAYTIME service
- *------------------------------------------------------------------------
- */
- int
- main(int argc, char *argv[])
- {
- char *host = "localhost"; /* host to use if none supplied */
- char *service = "daytime"; /* default service port */
- switch (argc) {
- case 1:
- host = "localhost";
- break;
- case 3:
- service = argv[2];
- /* FALL THROUGH */
- case 2:
- host = argv[1];
- break;
- default:
- fprintf(stderr, "usage: TCPdaytime [host [port]]\n");
- exit(1);
- }
- TCPdaytime(host, service);
- exit(0);
- }
- /*------------------------------------------------------------------------
- * TCPdaytime - invoke Daytime on specified host and print results
- *------------------------------------------------------------------------
- */
- TCPdaytime(const char *host, const char *service)
- {
- char buf[LINELEN+1]; /* buffer for one line of text */
- int s, n; /* socket, read count */
- s = connectTCP(host, service);
- while( (n = read(s, buf, LINELEN)) > 0) {
- buf[n] = '\0'; /* ensure null-terminated */
- (void) fputs( buf, stdout );
- }
- }
复制代码- /* connectTCP.c - connectTCP */
- int connectsock(const char *host, const char *service,
- const char *transport);
- /*------------------------------------------------------------------------
- * connectTCP - connect to a specified TCP service on a specified host
- *------------------------------------------------------------------------
- */
- int
- connectTCP(const char *host, const char *service )
- /*
- * Arguments:
- * host - name of host to which connection is desired
- * service - service associated with the desired port
- */
- {
- return connectsock( host, service, "tcp");
- }
复制代码- /* connectsock.c - connectsock */
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/errno.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <string.h>
- #include <stdlib.h>
- #ifndef INADDR_NONE
- #define INADDR_NONE 0xffffffff
- #endif /* INADDR_NONE */
- int errexit(const char *format, ...);
- /*------------------------------------------------------------------------
- * connectsock - allocate & connect a socket using TCP or UDP
- *------------------------------------------------------------------------
- */
- int
- connectsock(const char *host, const char *service, const char *transport )
- /*
- * Arguments:
- * host - name of host to which connection is desired
- * service - service associated with the desired port
- * transport - name of transport protocol to use ("tcp" or "udp")
- */
- {
- struct hostent *phe; /* pointer to host information entry */
- struct servent *pse; /* pointer to service information entry */
- struct protoent *ppe; /* pointer to protocol information entry*/
- struct sockaddr_in sin; /* an Internet endpoint address */
- int s, type; /* socket descriptor and socket type */
- memset(&sin, 0, sizeof(sin));
- sin.sin_family = AF_INET;
- /* Map service name to port number */
- 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);
- /* Map host name to IP address, allowing for dotted decimal */
- 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);
- /* Map transport protocol name to protocol number */
- if ( (ppe = getprotobyname(transport)) == 0)
- errexit("can't get \"%s\" protocol entry\n", transport);
- /* Use protocol to choose a socket type */
- if (strcmp(transport, "udp") == 0)
- type = SOCK_DGRAM;
- else
- type = SOCK_STREAM;
- /* Allocate a socket */
- s = socket(PF_INET, type, ppe->p_proto);
- if (s < 0)
- errexit("can't create socket: %s\n", strerror(errno));
- /* Connect the socket */
- if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
- errexit("can't connect to %s.%s: %s\n", host, service,
- strerror(errno));
- return s;
- }
复制代码 Things should be changed for Solaris :
(NO change is needed for FreeBSD and Linux systems)
1.add the following to the Makefile:
。 -lsocket -lnsl to CFLAGS (for socket and related system calls)
。CC = gcc (compiler, some files required by cc are missing, we have to use gcc instead)
2.errno.h is in the directory /usr/include, NOT in the directory /usr/include/sys
。you should change all the occurrences
3.make the following changes to the file TCPtecho.c
。include another header file: <sys/filio.h> (constant FIONBIO is defined here)
。define the macro MIN, which returns the min. of two integers
这是我找到要在solaris环境下编译C 文件时要更换的东西。但是我不懂怎样做更换。尤其是第一项的第一个。第二项是不是说#include <sys/errno.h> 换成 #include<errno.h>.?而第三项 增加 #include<sys/filio.h>,那第二项呢?
先谢谢大家 |
|