gpt4 book ai didi

c - 发送:无效参数

转载 作者:太空宇宙 更新时间:2023-11-04 07:33:32 26 4
gpt4 key购买 nike

我对这行代码有疑问。我必须将一个数据包带到一个端口并重新发送到接口(interface)(例如:eth0)。我的程序成功地从端口获取数据包,但是当我重新发送(使用 send())到接口(interface)时出现错误:
发送:无效参数

代码行是:

    sock1=socket(AF_INET6,SOCK_DGRAM,0);
sock2=socket(AF_INET6,SOCK_DGRAM,0);
fd=fopen("port.txt","r+");
if(fd) {
while(!feof(fd)){
fscanf(fd,"%d",&port);
fscanf(fd, "%s",interface);
}
}

memset(&source_addr,0,sizeof(struct sockaddr_in6));
source_addr.sin6_family=AF_INET6;
source_addr.sin6_port=htons(port);
source_addr.sin6_addr=in6addr_any;



if(bind(sock1,(struct sockaddr*)&source_addr,sizeof(struct sockaddr_in6))==-1){perror("bind");}
//if(connect(sock1,(struct sockaddr*)&source_addr,sizeof(struct sockaddr_in6))==-1){perror("connect");}


//Device dove inviare
memset(&freq,0,sizeof(struct ifreq));
strncpy(freq.ifr_name,interface,IFNAMSIZ);

if(ioctl(sock2,SIOCGIFINDEX,&freq)==-1){perror("ioctl");}
memset(&destination_addr,0,sizeof(struct sockaddr_in6));
destination_addr.sin6_family=AF_INET6:
destination_addr.sin6_scope_id=htonl(2)
inet_pton(AF_INET6,"2001::620:40b:555:110",(void*)&destination_addr.sin6_addr.s6_addr);
if(bind(sock2,(struct sockaddr*)&destination_addr,sizeof(struct sockaddr_in6)==-1)
{perror("bind");}


if((buff=malloc(BUFFER_LENGTH))==NULL){ perror("malloc");}

packet_length=recv(sock1,buff,BUFFER_LENGTH,0);

if(packet_length<0){perror("recv");}

printf("La lunghezza è %d\n",packet_length);

packet=(unsigned char*)buff;

Sniffer(packet,packet_length,' ');


packet_length2=send(sock2,buff,BUFFER_LENGTH,0);

buff 是我从端口拿来的数据包!哪里出错了?

最佳答案

您没有在 UDP 套接字上调用 connect(),因此它没有默认目标(供 send() 使用)。要么调用 connect() 来设置默认目的地,要么使用带有显式目的地的 sendto

你也不应该发送超过你实际收到的(即。packet_length)

关于c - 发送:无效参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11031526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com