Search code examples
c++network-programmingace

ACE c++ send_n and recv_n not working as expected


I am trying to implement a basic client-server program using ACE, but the problem is that if I send from client and receive it from server, it works properly. But, when I am increasing the complexity i.e., multiple sends and recvs the programs isn't working. Any help is appreciated.

Here's the client code.

    #include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Connector.h"
#include "ace/SOCK_Stream.h"
#include "iostream"
using namespace std;
int main(){
    ACE_INET_Addr server(7128);
    ACE_SOCK_Connector cli;
    ACE_SOCK_Stream cstream;
    if(cli.connect(cstream, server)==-1){perror("Error");}
    char buff[100];
    if(-1==cstream.recv_n(buff, 7)){printf("Error");}
    cout<<buff<<endl;
    cstream.send_n("FU", 3);
    }

Here's the server code

#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Connector.h"
#include "ace/SOCK_Stream.h"
#include "iostream"
using namespace std;
int main(){
    ACE_INET_Addr server(7128);
    ACE_INET_Addr client;
    ACE_SOCK_Acceptor cli(server);
    ACE_SOCK_Stream cstream;
    if(cli.accept(cstream, &client)==-1){perror("Error");}
    char buff[100];
    if(-1==cstream.send_n("RAND", 5)){printf("Error");}
    cstream.recv_n(buff, 3);
    }

Solution

  • Your server is sending 5 bytes but your client is expecting to read 7. The client is blocked waiting for 2 more bytes that will never come.