B
B
C++ / C#

Why hangs when calling connect WSA?

#include <iostream>
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <cstring>

int main(){
  std::cout << "vvvvvvv" << std::endl;
  WSADATA wsaData;
  if(WSAStartup(MAKEWORD(2,2), &wsaData) != 0){
    std::cout << "nananan" << std::endl;
  }
  std::cout << "sock" << std::endl;
  SOCKET sock = socket(2,1,0);
  if(sock == INVALID_SOCKET){
    std::cout << "trrrr" << std::endl;
  }
  struct addrinfo *result = NULL,*ptr = NULL,hints;
  ZeroMemory( &hints, sizeof(hints) );
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  std::cout << "addr" << std::endl;
  if(getaddrinfo("1.1.1.1", "80", &hints, &result) != 0){
    std::cout << "zzhhhh" << std::endl;
  }
  ptr = result->ai_next;
  std::cout << "conn" << std::endl;
  if(connect(sock, ptr->ai_addr, (int)ptr->ai_addrlen) == SOCKET_ERROR){
    std::cout << "thr thr" << std::endl;
  }
  std::cout << "vrum" << std::endl;
  const char *buff = "HEAD / HTTP/1.1\r\nHost: 1.1.1.1\r\n\r\n";
  send(sock,buff,strlen(buff),0);
  char rcv[1023];
  recv(sock,rcv,1023,0);
  std::cout << rcv << std::endl;
  WSACleanup();
  return 0;
}


It comes to connect and then freezes and pops up a search for Windows errors in the console, there are no errors. The connection never opens.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-03-23
are @l3m5

Why are you doing it ptr = result->ai_next;?
The documentation has an example of how to process the result:
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
Surely, it returns one record for you, and you immediately take the next one, which does not exist. And then you dereference the null pointer ptr.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question