[问题] Tcp Socket在 SUN OS compiler Error

楼主: TwoDemon (飞天)   2012-12-17 22:44:25
请教各位先进,我在测试 SUN OS 平台转至 至Linux (RHEL)上有关
TCP Socket的问题,其中包含了 htonl、ntohl等问题,所以在测试
client / server之间的沟通,测试程式client & server在 linux上
都可以comipler过且正确执行,但把server程式放到 SUN OS就compiler
过不了,因为我主要测试 client 在linux上run但server是 SUN OS。
Server 程式如下:
s1.c
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/types.h> /* for Socket data types */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <netinet/in.h> /* for IP Socket data types */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
char sendBuff[1025];
time_t ticks;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
ticks = time(NULL);
//snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
snprintf(sendBuff, sizeof(sendBuff), "%4s\r\n","hello world");
write(connfd, sendBuff, strlen(sendBuff));
close(connfd);
sleep(1);
}
}
=========================
gcc s1.c 如下
user@testhost[/home/user/Sample]gcc s1.c
Undefined first referenced
symbol in file
bind /var/tmp//cc8yehtb.o
accept /var/tmp//cc8yehtb.o
listen /var/tmp//cc8yehtb.o
socket /var/tmp//cc8yehtb.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
这是什么错误讯息呀?
楼主: TwoDemon (飞天)   2012-02-17 22:48:00
这边好像也有人有同的样问题,但我看不太懂他的说明http://goo.gl/mWEfS刚刚再试了一下:gcc -lsocket -lnsl s1.c这样可以compiler过了

Links booklink

Contact Us: admin [ a t ] ucptt.com