naraku9333

Untitled

Jan 14th, 2014
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <SFML/Network.hpp>
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <array>
  6. #include <cstring>
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10.     sf::TcpSocket socket;
  11.     std::string host = "irc.quakenet.org";
  12.     std::string channel = "#cplusplus";
  13.     std::string nick = "ResBot_9333";
  14.     std::string user = "X";
  15.     unsigned short port = 6667;
  16.  
  17.     sf::Socket::Status status = socket.connect(sf::IpAddress(host), 6667);
  18.     if(status != sf::Socket::Done)
  19.     {
  20.         std::cout << "Failed to connect.\n";
  21.         return 1;
  22.     }
  23.  
  24.     std::string conn_message = "NICK " + nick + "\r\nUSER " + user + " 0 * :" + nick + "\r\n";// JOIN " + channel + "\r\n";
  25.     if(socket.send(conn_message.c_str(), conn_message.size() - 1) != sf::Socket::Done)
  26.     {
  27.         std::cout << "Transmission of initial setup failed.\n";
  28.     }
  29.     std::array<char, 1024> buf;
  30.     //std::string rx_data(1024,'\0');
  31.     std::size_t received;
  32.     std::string pong = "PONG";
  33.     std::string message;
  34.     bool joined = false;
  35.     while(true)
  36.     {
  37.         if(socket.receive(&buf, 1024, received) != sf::Socket::Done)
  38.         {
  39.             std::cout << "Receive failed.\n";
  40.         }
  41.         std::string rx_data(buf.begin(), buf.begin() + received);
  42.         int pos = 0;
  43.         if ((pos = rx_data.find("PING")) != std::string::npos)//rx_data.compare(0, 4, "PING") == 0)
  44.         {
  45.             message = pong + rx_data.substr(pos + 4);
  46.             std::cout << "MY PONG " << message;
  47.             if (socket.send(message.c_str(), message.size() - 1) != sf::Socket::Done)
  48.             {
  49.                 std::cout << "PONG failed.\n";
  50.             }
  51.             std::string s = "JOIN " + channel + "\r\n";
  52.            // if (!joined)
  53.             {
  54.                 socket.send(s.c_str(), s.size());
  55.                 joined = true;
  56.             }
  57.         }
  58.  
  59.         std::cout << rx_data.c_str();
  60.     }
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment