Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Network.hpp>
- #include <iostream>
- #include <string>
- #include <array>
- #include <cstring>
- int main(int argc, char* argv[])
- {
- sf::TcpSocket socket;
- std::string host = "irc.quakenet.org";
- std::string channel = "#cplusplus";
- std::string nick = "ResBot_9333";
- std::string user = "X";
- unsigned short port = 6667;
- sf::Socket::Status status = socket.connect(sf::IpAddress(host), 6667);
- if(status != sf::Socket::Done)
- {
- std::cout << "Failed to connect.\n";
- return 1;
- }
- std::string conn_message = "NICK " + nick + "\r\nUSER " + user + " 0 * :" + nick + "\r\n";// JOIN " + channel + "\r\n";
- if(socket.send(conn_message.c_str(), conn_message.size() - 1) != sf::Socket::Done)
- {
- std::cout << "Transmission of initial setup failed.\n";
- }
- std::array<char, 1024> buf;
- //std::string rx_data(1024,'\0');
- std::size_t received;
- std::string pong = "PONG";
- std::string message;
- bool joined = false;
- while(true)
- {
- if(socket.receive(&buf, 1024, received) != sf::Socket::Done)
- {
- std::cout << "Receive failed.\n";
- }
- std::string rx_data(buf.begin(), buf.begin() + received);
- int pos = 0;
- if ((pos = rx_data.find("PING")) != std::string::npos)//rx_data.compare(0, 4, "PING") == 0)
- {
- message = pong + rx_data.substr(pos + 4);
- std::cout << "MY PONG " << message;
- if (socket.send(message.c_str(), message.size() - 1) != sf::Socket::Done)
- {
- std::cout << "PONG failed.\n";
- }
- std::string s = "JOIN " + channel + "\r\n";
- // if (!joined)
- {
- socket.send(s.c_str(), s.size());
- joined = true;
- }
- }
- std::cout << rx_data.c_str();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment