Dydaktyka:
FeedbackTo jest stara wersja strony!
#include <iostream> // ____ _ _ _ __ __ #include <sys/socket.h> // | _ \ ___ __ _ __| | ___ ___ ___| | _____| |_ / / __ ___ _____ _\ \. #include <netinet/in.h> // | |_) / _ \/ _` |/ _` | / __|/ _ \ / __| |/ / _ \ __| | | '__/ _ \/ __\ \ / /| | #include <arpa/inet.h> // | _ < __/ (_| | (_| | \__ \ (_) | (__| < __/ |_ | | | | __/ (__ \ V / | | #include <unistd.h> // |_| \_\___|\__,_|\__,_| |___/\___/ \___|_|\_\___|\__| | |_| \___|\___| \_/ | | // \_\ /_/ int main(int argc, char **argv) { int fd = socket(PF_INET, SOCK_STREAM, 0); sockaddr_in address { .sin_family = AF_INET, .sin_port = htons(13), .sin_addr = {.s_addr = inet_addr("127.0.0.1") } }; connect(fd, (sockaddr*) &address, sizeof(address)); char buffer[255]; ssize_t readSize = recv(fd, buffer, 254, 0); buffer[readSize] = 0; close(fd); std::cout << buffer << std::endl; return 0; }
#include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> #include <errno.h> #include <error.h> #include <netdb.h> int main(int argc, char ** argv){ if(argc!=3) error(1,0,"Need 2 args"); addrinfo *ao; int res = getaddrinfo(argv[1],argv[2],nullptr, &ao); if(res || !ao) error(1,errno, "getaddrinfo"); int sock = socket(ao->ai_family, SOCK_STREAM, 0); if(sock<0) error(1,errno, "socket"); res = connect(sock, ao->ai_addr, ao->ai_addrlen); if(res) error(1,errno, "connect"); freeaddrinfo(ao); // ... return 0; }