Piszę program w DevC++
Program się kompiluje jednak zawiesza się w tym miejscu:
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd == -1) {
cout << "[-] Error : Socket couldn't be created" << endl;
exit (EXIT_FAILURE);
}
a to cały kod źródłowy
#include <w32api.h>
#define WINVER WindowsXP
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x501
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
#include <sys/types.h>
#include <Winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#pragma comment(lib,"libws2_32.a")//ważna biblioteka DevC++ sockets libray
using namespace std;
/*
struct addrinfo {
int ai_flags; // AI_PASSIVE, AI_CANONNAME, etc.
int ai_family; // AF_INET, AF_INET6, AF_UNSPEC
int ai_socktype; // SOCK_STREAM, SOCK_DGRAM
int ai_protocol; // use 0 for "any"
size_t ai_addrlen; // size of ai_addr in bytes
struct sockaddr *ai_addr; // struct sockaddr_in or _in6
char *ai_canonname; // full canonical hostname
struct addrinfo *ai_next; // linked list, next node
};
*/
int main()
{
cout << "Hello world!" << endl;
char *target_addr = "www.google.com";
char *target_port = "80";
struct addrinfo info;
struct addrinfo *res;
int sockfd;
char buf[512];
int byte_count;
int status;
// get host info, make socket, and connect it
memset(&info, 0, sizeof info);// make sure the struct is empty
info.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever
info.ai_socktype = SOCK_STREAM;
//info.ai_flags = AI_PASSIVE;
if (status = getaddrinfo(target_addr, target_port, &info, &res) == -1) {
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
system("pause");
// nie działa:
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd == -1) {
cout << "[-] Error : Socket couldn't be created" << endl;
exit (EXIT_FAILURE);
}
system("pause");
// server = gethostbyname("smtp.mailtrap.io");
if (bind (sockfd, res->ai_addr, res->ai_addrlen) == -1 ) {
cout << "[-] Error: Failed to bind to port" << endl;
exit (EXIT_FAILURE);
}
system("pause");
if (connect (sockfd, res->ai_addr, res->ai_addrlen) == -1) {
cout << "[-] Error: Failed to connect to remote user" << endl;
exit (EXIT_FAILURE);
}
system("pause");
// all right! now that we're connected, we can receive some data!
byte_count = recv(sockfd, buf, sizeof buf, 0);
printf("recv()'d %d bytes of data in buf\n", byte_count);
freeaddrinfo(res);
WSACleanup(); // closes up the winsocks library
system("pause");
return 0;
}