title: Implementing stop and wait ARQ
body: <p>I am trying to emulate the protocol. This is what I have done so far:
  Reciever side code:</p>
  
  <pre><code>// SocketException class
  
  
  #ifndef SocketException_class
  #define SocketException_class
  
  #include &lt;string&gt;
  
  class SocketException
  {
   public:
    SocketException ( std::string s ) : m_s ( s ) {};
    ~SocketException (){};
  
    std::string description() { return m_s; }
  
   private:
  
    std::string m_s;
  
  };
  
  #endif
  
  // Definition of the Socket class
  
  #ifndef Socket_class
      #define Socket_class
  
  
      #include &lt;sys/types.h&gt;
      #include &lt;sys/socket.h&gt;
      #include &lt;netinet/in.h&gt;
      #include &lt;netdb.h&gt;
      #include &lt;unistd.h&gt;
      #include &lt;string&gt;
      #include &lt;arpa/inet.h&gt;
  
  
      const int MAXHOSTNAME = 200;
      const int MAXCONNECTIONS = 5;
      const int MAXRECV = 500;
  
      class Socket
      {
       public:
        Socket();
        virtual ~Socket();
  
        // Server initialization
        bool create();
        bool bind ( const int port );
        bool listen() const;
        bool accept ( Socket&amp; ) const;
  
        // Client initialization
        bool connect ( const std::string host, const int port );
  
        // Data Transimission
        bool send ( const std::string ) const;
        int recv ( std::string&amp; ) const;
  
  
        void set_non_blocking ( const bool );
  
        bool is_valid() const { return m_sock != -1; }
  
       private:
  
        int m_sock;
        sockaddr_in m_addr;
  
  
      };
  
  
  #endif
  
  // Implementation of the Socket class.
  
  #include "string.h"
  #include &lt;string.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;fcntl.h&gt;
  #include&lt;iostream&gt;
  
  
  Socket::Socket() :
    m_sock ( -1 )
  {
  
    memset ( &amp;m_addr,
         0,
         sizeof ( m_addr ) );
  
  }
  
  Socket::~Socket()
  {
    if ( is_valid() )
      ::close ( m_sock );
  }
  
  bool Socket::create()
  {
    m_sock = socket ( AF_INET,
              SOCK_STREAM,
              0 );
  
    if ( ! is_valid() )
      return false;
  
  
    // TIME_WAIT - argh
    int on = 1;
    if ( setsockopt ( m_sock, SOL_SOCKET, SO_REUSEADDR, ( const char* ) &amp;on, sizeof ( on ) ) == -1 )
      return false;
  
  
    return true;
  
  }
  
  
  
  bool Socket::bind ( const int port )
  {
  
    if ( ! is_valid() )
      {
        return false;
      }
  
  
  
    m_addr.sin_family = AF_INET;
    m_addr.sin_addr.s_addr = INADDR_ANY;
    m_addr.sin_port = htons ( port );
  
    int bind_return = ::bind ( m_sock,
                   ( struct sockaddr * ) &amp;m_addr,
                   sizeof ( m_addr ) );
  
  
    if ( bind_return == -1 )
      {
        return false;
      }
  
    return true;
  }
  
  
  bool Socket::listen() const
  {
    if ( ! is_valid() )
      {
        return false;
      }
  
    int listen_return = ::listen ( m_sock, MAXCONNECTIONS );
  
  
    if ( listen_return == -1 )
      {
        return false;
      }
  
    return true;
  }
  
  
  bool Socket::accept ( Socket&amp; new_socket ) const
  {
    int addr_length = sizeof ( m_addr );
    new_socket.m_sock = ::accept ( m_sock, ( sockaddr * ) &amp;m_addr, ( socklen_t * ) &amp;addr_length );
  
    if ( new_socket.m_sock &lt;= 0 )
      return false;
    else
      return true;
  }
  
  
  bool Socket::send ( const std::string s ) const
  {
    int status = ::send ( m_sock, s.c_str(), s.size(), MSG_NOSIGNAL );
    if ( status == -1 )
      {
        return false;
      }
    else
      {
        return true;
      }
  }
  
  
  int Socket::recv ( std::string&amp; s ) const
  {
    char buf [ MAXRECV + 1 ];
  
    s = "";
  
    memset ( buf, 0, MAXRECV + 1 );
  
    int status = ::recv ( m_sock, buf, MAXRECV, 0 );
  
    if ( status == -1 )
      {
        std::cout &lt;&lt; "status == -1   errno == " &lt;&lt; errno &lt;&lt; "  in Socket::recv\n";
        return 0;
      }
    else if ( status == 0 )
      {
        return 0;
      }
    else
      {
        s = buf;
        return status;
      }
  }
  
  
  
  bool Socket::connect ( const std::string host, const int port )
  {
    if ( ! is_valid() ) return false;
  
    m_addr.sin_family = AF_INET;
    m_addr.sin_port = htons ( port );
  
    int status = inet_pton ( AF_INET, host.c_str(), &amp;m_addr.sin_addr );
  
    if ( errno == EAFNOSUPPORT ) return false;
  
    status = ::connect ( m_sock, ( sockaddr * ) &amp;m_addr, sizeof ( m_addr ) );
  
    if ( status == 0 )
      return true;
    else
      return false;
  }
  
  void Socket::set_non_blocking ( const bool b )
  {
  
    int opts;
  
    opts = fcntl ( m_sock,
           F_GETFL );
  
    if ( opts &lt; 0 )
      {
        return;
      }
  
    if ( b )
      opts = ( opts | O_NONBLOCK );
    else
      opts = ( opts &amp; ~O_NONBLOCK );
  
    fcntl ( m_sock,
        F_SETFL,opts );
  
  }
  
  // Definition of the ServerSocket class
  
  #ifndef ServerSocket_class
      #define ServerSocket_class
  
      class ServerSocket : private Socket
      {
       public:
  
        ServerSocket ( int port );
        ServerSocket (){};
        virtual ~ServerSocket();
  
        const ServerSocket&amp; operator &lt;&lt; ( const std::string&amp; ) const;
        const ServerSocket&amp; operator &gt;&gt; ( std::string&amp; ) const;
  
        void accept ( ServerSocket&amp; );
  
      };
  #endif
  
  // Implementation of the ServerSocket class
  
  ServerSocket::ServerSocket ( int port )
  {
    if ( ! Socket::create() )
      {
        throw SocketException ( "Could not create server socket." );
      }
  
    if ( ! Socket::bind ( port ) )
      {
        throw SocketException ( "Could not bind to port." );
      }
  
    if ( ! Socket::listen() )
      {
        throw SocketException ( "Could not listen to socket." );
      }
  
  }
  
  ServerSocket::~ServerSocket()
  {
  }
  
  
  const ServerSocket&amp; ServerSocket::operator &lt;&lt; ( const std::string&amp; s ) const
  {
    if ( ! Socket::send ( s ) )
      {
        throw SocketException ( "Could not write to socket." );
      }
  
    return *this;
  
  }
  
  
  const ServerSocket&amp; ServerSocket::operator &gt;&gt; ( std::string&amp; s ) const
  {
    if ( ! Socket::recv ( s ) )
      {
        throw SocketException ( "Could not read from socket." );
      }
  
    return *this;
  }
  
  void ServerSocket::accept ( ServerSocket&amp; sock )
  {
    if ( ! Socket::accept ( sock ) )
      {
        throw SocketException ( "Could not accept socket." );
      }
  }
  
  #include &lt;string&gt;
  
  using namespace std;
  int main ( int argc, int argv[] )
  {
    cout &lt;&lt; "Waiting to receive frames...\n";
  
    try
      {
        // Create the socket
        ServerSocket server ( 30000 );
  
        while ( true )
      {
  
        ServerSocket new_sock;
        server.accept ( new_sock );
  
        try
          {
              while ( true )
              {
                string data;
                cout&lt;&lt;"Recieving  data."; //not being displayed
                new_sock &gt;&gt; data;
                cout&lt;&lt;"Recieved data. Sending back the acknowledgment";
                new_sock &lt;&lt; data;
              }
          }
        catch ( SocketException&amp; ) {}
  
      }
      }
    catch ( SocketException&amp; e )
      {
        std::cout &lt;&lt; "Exception was caught:" &lt;&lt; e.description() &lt;&lt; "\nExiting.\n";
      }
  
    return 0;
  }
  </code></pre>
  
  <p>Sender side code:</p>
  
  <pre><code>// SocketException class
  
  
  #ifndef SocketException_class
  #define SocketException_class
  
  #include &lt;string&gt;
  
  class SocketException
  {
   public:
    SocketException ( std::string s ) : m_s ( s ) {};
    ~SocketException (){};
  
    std::string description() { return m_s; }
  
   private:
  
    std::string m_s;
  
  };
  
  #endif
  
  // Definition of the Socket class
  
  #ifndef Socket_class
      #define Socket_class
  
  
      #include &lt;sys/types.h&gt;
      #include &lt;sys/socket.h&gt;
      #include &lt;netinet/in.h&gt;
      #include &lt;netdb.h&gt;
      #include &lt;unistd.h&gt;
      #include &lt;string&gt;
      #include &lt;arpa/inet.h&gt;
  
  
      const int MAXHOSTNAME = 200;
      const int MAXCONNECTIONS = 5;
      const int MAXRECV = 500;
  
      class Socket
      {
       public:
        Socket();
        virtual ~Socket();
  
        // Server initialization
        bool create();
        bool bind ( const int port );
        bool listen() const;
        bool accept ( Socket&amp; ) const;
  
        // Client initialization
        bool connect ( const std::string host, const int port );
  
        // Data Transimission
        bool send ( const std::string ) const;
        int recv ( std::string&amp; ) const;
  
  
        void set_non_blocking ( const bool );
  
        bool is_valid() const { return m_sock != -1; }
  
       private:
  
        int m_sock;
        sockaddr_in m_addr;
  
  
      };
  
  
  #endif
  
  // Implementation of the Socket class.
  
  #include "string.h"
  #include &lt;string.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;fcntl.h&gt;
  #include&lt;iostream&gt;
  
  
  Socket::Socket() :
    m_sock ( -1 )
  {
  
    memset ( &amp;m_addr,
         0,
         sizeof ( m_addr ) );
  
  }
  
  Socket::~Socket()
  {
    if ( is_valid() )
      ::close ( m_sock );
  }
  
  bool Socket::create()
  {
    m_sock = socket ( AF_INET,
              SOCK_STREAM,
              0 );
  
    if ( ! is_valid() )
      return false;
  
  
    // TIME_WAIT - argh
    int on = 1;
    if ( setsockopt ( m_sock, SOL_SOCKET, SO_REUSEADDR, ( const char* ) &amp;on, sizeof ( on ) ) == -1 )
      return false;
  
  
    return true;
  
  }
  
  
  
  bool Socket::bind ( const int port )
  {
  
    if ( ! is_valid() )
      {
        return false;
      }
  
  
  
    m_addr.sin_family = AF_INET;
    m_addr.sin_addr.s_addr = INADDR_ANY;
    m_addr.sin_port = htons ( port );
  
    int bind_return = ::bind ( m_sock,
                   ( struct sockaddr * ) &amp;m_addr,
                   sizeof ( m_addr ) );
  
  
    if ( bind_return == -1 )
      {
        return false;
      }
  
    return true;
  }
  
  
  bool Socket::listen() const
  {
    if ( ! is_valid() )
      {
        return false;
      }
  
    int listen_return = ::listen ( m_sock, MAXCONNECTIONS );
  
  
    if ( listen_return == -1 )
      {
        return false;
      }
  
    return true;
  }
  
  
  bool Socket::accept ( Socket&amp; new_socket ) const
  {
    int addr_length = sizeof ( m_addr );
    new_socket.m_sock = ::accept ( m_sock, ( sockaddr * ) &amp;m_addr, ( socklen_t * ) &amp;addr_length );
  
    if ( new_socket.m_sock &lt;= 0 )
      return false;
    else
      return true;
  }
  
  
  bool Socket::send ( const std::string s ) const
  {
    int status = ::send ( m_sock, s.c_str(), s.size(), MSG_NOSIGNAL );
    if ( status == -1 )
      {
        return false;
      }
    else
      {
        return true;
      }
  }
  
  
  int Socket::recv ( std::string&amp; s ) const
  {
    char buf [ MAXRECV + 1 ];
  
    s = "";
  
    memset ( buf, 0, MAXRECV + 1 );
  
    int status = ::recv ( m_sock, buf, MAXRECV, 0 );
  
    if ( status == -1 )
      {
        std::cout &lt;&lt; "status == -1   errno == " &lt;&lt; errno &lt;&lt; "  in Socket::recv\n";
        return 0;
      }
    else if ( status == 0 )
      {
        return 0;
      }
    else
      {
        s = buf;
        return status;
      }
  }
  
  
  
  bool Socket::connect ( const std::string host, const int port )
  {
    if ( ! is_valid() ) return false;
  
    m_addr.sin_family = AF_INET;
    m_addr.sin_port = htons ( port );
  
    int status = inet_pton ( AF_INET, host.c_str(), &amp;m_addr.sin_addr );
  
    if ( errno == EAFNOSUPPORT ) return false;
  
    status = ::connect ( m_sock, ( sockaddr * ) &amp;m_addr, sizeof ( m_addr ) );
  
    if ( status == 0 )
      return true;
    else
      return false;
  }
  
  void Socket::set_non_blocking ( const bool b )
  {
  
    int opts;
  
    opts = fcntl ( m_sock,
           F_GETFL );
  
    if ( opts &lt; 0 )
      {
        return;
      }
  
    if ( b )
      opts = ( opts | O_NONBLOCK );
    else
      opts = ( opts &amp; ~O_NONBLOCK );
  
    fcntl ( m_sock,
        F_SETFL,opts );
  
  }
  
  // Definition of the ClientSocket class
  
  #ifndef ClientSocket_class
      #define ClientSocket_class
  
      class ClientSocket : private Socket
      {
       public:
  
        ClientSocket ( std::string host, int port );
        virtual ~ClientSocket(){};
  
        const ClientSocket&amp; operator &lt;&lt; ( const std::string&amp; ) const;
        const ClientSocket&amp; operator &gt;&gt; ( std::string&amp; ) const;
  
      };
  
  #endif
  
  // Implementation of the ClientSocket class
  
  
  
  ClientSocket::ClientSocket ( std::string host, int port )
  {
    if ( ! Socket::create() )
      {
        throw SocketException ( "Could not create client socket." );
      }
  
    if ( ! Socket::connect ( host, port ) )
      {
        throw SocketException ( "Could not bind to port." );
      }
  
  }
  
  
  const ClientSocket&amp; ClientSocket::operator &lt;&lt; ( const std::string&amp; s ) const
  {
    if ( ! Socket::send ( s ) )
      {
        throw SocketException ( "Could not write to socket." );
      }
  
    return *this;
  
  }
  
  
  const ClientSocket&amp; ClientSocket::operator &gt;&gt; ( std::string&amp; s ) const
  {
    if ( ! Socket::recv ( s ) )
      {
        throw SocketException ( "Could not read from socket." );
      }
  
    return *this;
  }
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  int main ( int argc, int argv[] )
  {
    try
      {
  
        ClientSocket client_socket ( "localhost", 30000 );
  
        string reply, message;
  
        while(1){
            try{
                cout&lt;&lt;"Enter the frame to be sent: ";
                cin&gt;&gt;message;
                client_socket &lt;&lt; message;
                client_socket &gt;&gt; reply;
            }
            catch ( SocketException&amp; ) {}
  
            cout &lt;&lt; "We received this response from the server:\n\"" &lt;&lt; reply &lt;&lt; "\"\n";
        }
      }
    catch ( SocketException&amp; e )
      {
        cout &lt;&lt; "Exception was caught:" &lt;&lt; e.description() &lt;&lt; "\n";
      }
  
    return 0;
  }
  </code></pre>
  
  <p>In the receiver side code, in the <code>main()</code> function, it is receiving the data from the sender but I am unable to display it on the receiver terminal. <code>cout</code> statement is not working. What is wrong with the code? </p>