Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <string>
- #include <iostream>
- int main()
- {
- int status;//hold getaddrinfo results
- struct addrinfo hints;
- struct addrinfo *res;
- const char *ip= "127.0.0.1";
- const char *port = "1337";
- hints.ai_family = AF_UNSPEC;//socket ipv4, or ipv6, unspec == both
- hints.ai_socktype = SOCK_STREAM;//user datagram socket
- hints.ai_flags = AI_PASSIVE;//non hardcoded ip value
- getaddrinfo(ip, port, &hints, &res);//fill struct res with socket info
- int sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);//create socket
- bind(sock, res->ai_addr, res->ai_addrlen);//bind to an address
- connect(sock, res->ai_addr, res->ai_addrlen);
- freeaddrinfo(res);
- char* buf[11];
- int data = recv(sock, (void*)buf, 11, 0); //(void*)buf == (void*)(char*) basically, you are typecasting char* to void*
- std::cout << data;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement