- 论坛徽章:
- 0
|
在SCO OpenServer 5.0.6上要实现接收别的机器上发送过来的ping消息,于是用原始套接字编写了一个小程序,但是编译运行后,发现不能接收到别的机器上ping过来的消息。用dbx调试,发现程序一直停留在该行:size = recvfrom(sd, pkt, sizeof(pkt), 0, NULL,NULL);
但是将该程序移植到RH Linux AS3平台下,程序则能接受到别的机器ping过来的消息。
请各位大虾指点,为什么SCO平台上接受不到别的机器ping来的消息啊!
感谢各位了!
ceshi.c:
#include <stdio.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/timeb.h>
#include <fcntl.h>
#include <signal.h>
#include <termio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <string.h>
#define SIZE_OF_PACKET 100
int main(int argc, char *argv[])
{
int sd, size, fromlen;
char pkt[512];
int i,pid;
struct protoent *proto;
struct sockaddr_in from;
signal(SIGCHLD,SIG_IGN);
proto = getprotobyname("icmp" ;
//if ((sd = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0)
if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
exit(0);
setuid(getuid());
printf("sd=%d\n",sd);
do
{
printf("begin to rcv\n " ;
size = recvfrom(sd, pkt, sizeof(pkt), 0, NULL,NULL);
printf("ping of %i\n ", size-2 ;
} while (size != SIZE_OF_PACKET + 2 ;
close(sd);
return 0;
}
# cc -g -o ceshi ceshi.c -lsocket |
|