Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Recvfrom()未接收发送到系统的所有数据_C_Sockets - Fatal编程技术网

Recvfrom()未接收发送到系统的所有数据

Recvfrom()未接收发送到系统的所有数据,c,sockets,C,Sockets,我有一个代码,看起来像这样,我正在使用原始套接字从我的系统(10.0.0.2)扫描VM(10.0.0.1)的所有端口 int main(){ int raw_sock = socket (AF_INET, SOCK_RAW, IPPROTO_TCP); ip_header->ihl = 5; ip_header->version = 4; ip_header->tos = 0; ip_header->tot_len = sizeof (struct tcphdr) +

我有一个代码,看起来像这样,我正在使用原始套接字从我的系统(10.0.0.2)扫描VM(10.0.0.1)的所有端口

int main(){
int raw_sock = socket (AF_INET, SOCK_RAW, IPPROTO_TCP);
ip_header->ihl = 5;
ip_header->version = 4;
ip_header->tos = 0;
ip_header->tot_len =  sizeof (struct tcphdr) + sizeof (struct ip);
ip_header->id = htons (54321);
ip_header->frag_off = htons(16384);
ip_header->ttl = 64;
ip_header->protocol = IPPROTO_TCP;
ip_header->check = 0;
ip_header->saddr = inet_addr (source_ip);   
ip_header->daddr = destination_ip.s_addr;
...
//Same way I fill tcp header
tcp_header->source = htons (source_port);
tcp_header->dest = htons (80);
tcp_header->seq = htonl(1105024978);
tcp_header->ack_seq = 0;
tcp_header->doff = sizeof(struct tcphdr) / 4;       //Size of tcp header
tcp_header->fin=0;
tcp_header->syn=1;//Sending syn packet to the target
tcp_header->rst=0;
tcp_header->psh=0;
tcp_header->ack=0;
tcp_header->urg=0;
tcp_header->window = htons ( 14600);    // maximum allowed window size
tcp_header->check = 0;
tcp_header->urg_ptr = 0;
...
//Set IP_HDRINCL: Indicate kernel of header
...

//Create a thread to handle recv
int a = pthread_create(&thID, NULL, handle_receive, NULL);
...
destination.sin_family = AF_INET;
destination.sin_addr.s_addr = destination_ip.s_addr;
...
for(int i=1;i<=100;i++)
     sendto (raw_sock, packet ,...);

...
sleep(10);
return 0;//closes all program without waiting for threads to join
}

void* handle_receive(void *){
   while(1){
      data_size = recvfrom(raw_sock_global , buffer , 65536*128 , 0 , &saddr , &saddr_size);
   }
}
intmain(){
int raw_sock=套接字(AF INET、sock_raw、IPPROTO_TCP);
ip_头->ihl=5;
ip_头->版本=4;
ip_头->tos=0;
ip_头->tot_len=sizeof(struct tcphdr)+sizeof(struct ip);
ip_头->id=htons(54321);
ip_头->框架关闭=htons(16384);
ip_头->ttl=64;
ip_头->协议=IPPROTO_TCP;
ip_头->检查=0;
ip_头->SADD=inet_地址(源ip);
ip\u头->daddr=目的地\u ip.s\u地址;
...
//和我填充tcp头的方法一样
tcp\u头->源=htons(源\u端口);
tcp_头->dest=htons(80);
tcp_头->seq=htonl(1105024978);
tcp_头->确认顺序=0;
tcp_头->doff=sizeof(struct tcphdr)/4;//tcp头的大小
tcp_头->fin=0;
tcp_头->syn=1;//向目标发送syn数据包
tcp_头->rst=0;
tcp_头->psh=0;
tcp_头->确认=0;
tcp_头->urg=0;
tcp_header->window=htons(14600);//允许的最大窗口大小
tcp_头->检查=0;
tcp_头->urg_ptr=0;
...
//设置IP_HDRINCL:指示标头的内核
...
//创建一个线程来处理recv
int a=pthread\u create(&thID,NULL,handle\u receive,NULL);
...
destination.sinu family=AF\u INET;
destination.sinu addr.s\u addr=destination\u ip.s\u addr;
...

对于(int i=1;i指定名称
raw_socket
事实上指定了一个具有系列
AF_INET
和类型
SOCK_raw
的套接字的描述符?如果是这样,那么您如何扫描端口,因为端口不是IP层功能?总体而言,请提供一个示例来说明问题,因为问题很可能是n“您所提供的代码片段中有ot。@约翰伯林格,我添加了一些详细信息。请检查。我正在调试,发现它没有记录有时发送的最初几个回复(在tshark中,它显示从端口1、2、3返回发送端口的回复,以此类推。但它有时会跳过前几个回复,并从端口30、31、32等处获得回复…)@JohnBollingerAlso如果我扫描任何域(www.domewebsite.com),则接收不会有问题。只有当我给VM IP时,我才遇到这个问题。我还通过给我的大学服务器IP:P进行了测试,它是有效的。@johnbollinger名称
raw\u socket
实际上是用family
AF\u INET
和type
SOCK\u raw
指定套接字的描述符吗?如果是,那么你如何扫描端口,因为端口不是IP层?总的来说,请提供一个例子来说明这个问题,因为这个问题很可能不在您提供的代码片段中。@JohnBollinger我已经添加了一些详细信息。请检查。我正在调试,发现它没有记录有时发送的最初几个回复(在tshark中,它显示从端口1、2、3等返回到发送端口的回复。但有时它会跳过最初的几个回复,并从端口30、31、32等处获得回复。@JohnBollingerAlso如果我扫描任何域,接收时没有问题(www.domewebsite.com)。只有当我给虚拟机IP时,我才会遇到这个问题。我还通过给我的大学服务器IP:P进行了测试,结果证明它是有效的。@JohnBollinger