Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //TCP Sockets
- #ifndef SOCKET_H
- #define SOCKET_H
- #if defined _WIN32
- #define _WIN32_WINNT 0x501
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #endif
- #if defined __linux
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #define INVALID_SOCKET -1
- #endif
- #if defined __APPLE__
- // Why would you do that?
- #endif
- #include <string>
- #include <cstdio>
- #include <cstring>
- #include <sstream>
- namespace aaix {
- const std::string sendl("\r\n");
- class Socket
- {
- public:
- Socket();
- int connect(const std::string host, const std::string port);
- int bindl(const std::string port, int backcon);
- int accept(Socket& s);
- int sock_err(const std::string err_string);
- int get();
- Socket& operator<< (const std::string& ostring);
- Socket& operator<< (const int oint);
- virtual ~Socket();
- std::string recv_string;
- #if defined __linux
- int sock;
- #elif defined __WIN32
- SOCKET sock;
- #endif
- protected:
- struct addrinfo *ai;
- std::stringstream ss;
- std::string sendstring;
- };
- Socket::Socket()
- {
- #if defined _WIN32
- WSADATA wsa;
- int wsa_err = WSAStartup(MAKEWORD(2,2), &wsa);
- if ( wsa_err != 0 )
- {
- std::cout << "Winsock Error: " << wsa_err << std::endl;
- WSACleanup();
- }
- #endif
- }
- int Socket::sock_err(const std::string err_string)
- {
- #if defined _WIN32
- std::cout << err_string << std::endl << WSAGetLastError() << std::endl;
- WSACleanup();
- #elif defined __linux
- perror(err_string.c_str());
- #endif
- freeaddrinfo(ai);
- return false;
- }
- int Socket::connect(const std::string host, const std::string port)
- {
- struct addrinfo hints;
- memset(&hints, 0, sizeof(struct addrinfo));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- int gai_err = getaddrinfo(host.c_str(), port.c_str(), &hints, &ai);
- if ( gai_err != 0 )
- {
- std::cout << gai_strerror(gai_err) << std::endl;
- return false;
- }
- sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
- if (sock == std::string::npos) sock_err("Socket creation failed");
- if (::connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) sock_err("Connection failed");
- std::cout << "yeah" << std::endl;
- return true;
- }
- int Socket::bindl(const std::string port, int backcon)
- {
- struct addrinfo hints;
- memset(&hints, 0, sizeof(struct addrinfo));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- hints.ai_flags = AI_PASSIVE;
- int gai_err = getaddrinfo(0, port.c_str(), &hints, &ai);
- if ( gai_err != 0 )
- {
- std::cout << gai_strerror(gai_err) << std::endl;
- return false;
- }
- sock = ::socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
- if (sock == std::string::npos) sock_err("Socket creation failed");
- if (::bind(sock, ai->ai_addr, ai->ai_addrlen)) sock_err("Bind failed");
- freeaddrinfo(ai);
- if(listen(sock, backcon)) return false;
- std::cout << "binded" << std::endl;
- return true;
- }
- int Socket::accept(Socket& s)
- {
- s.sock = ::accept(sock, 0, 0);
- if (s.sock == INVALID_SOCKET) return false;
- return true;
- }
- // Idea :: Make this into the ">>" operator.
- int Socket::get()
- {
- char buf[513];
- int brecv = recv(sock, buf, 512, 0);
- if (brecv <= 0) return false;
- recv_string = buf;
- memset(&buf, 0, 513);
- return true;
- }
- Socket::~Socket()
- {
- #if defined _WIN32
- WSACleanup();
- #elif defined __linux
- close(sock);
- #endif
- }
- Socket& Socket::operator<< (const std::string& ostring)
- {
- if (ostring == sendl)
- {
- ss << ostring;
- sendstring = ss.str();
- if (send(sock, sendstring.c_str(), sendstring.length() + 1, 0) == -1) sock_err("Send failed");
- ss.str("");
- puts(sendstring.c_str()); // Checking output. Will delete later.
- }
- ss << ostring;
- return *this;
- }
- Socket& Socket::operator<< (const int oint)
- {
- ss << oint;
- return *this;
- }
- }
- #endif // SOCKET_H
Add Comment
Please, Sign In to add comment