-1

Why when run the program and send data to server return this errorrecv failed: Transport endpoint is not connected or don't show server accepted just show the message of send data function in client

server.cpp:

int main() {
    char packet[30];
    char buffer[20] = "I got your message";
    int conn_sock, comm_sock, n, m;
    struct sockaddr_in server_addr, client_addr;
    if ((conn_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("Couldn't create socket");
        exit(1);
    }
    cout << "Already create socket!!!\n" << endl;

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(0);
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    memset(&server_addr, 0, sizeof(server_addr));
    if (bind(conn_sock, (struct sockaddr *) &server_addr, sizeof(server_addr))
            == -1) {
        perror("couldn't bind");
        exit(1);
    }
    if (listen(conn_sock, 10) == -1) {
        perror("couldn't listen");
        exit(1);
    }

    cout << "Listening For Connection...\r" << endl;
    socklen_t len = sizeof(server_addr);
    if (getsockname(conn_sock, (struct sockaddr *) &server_addr, &len) == -1)

        perror("getsockname");
    else
        printf("port number %d\n", ntohs(server_addr.sin_port));

    while (1) {
        memset(&client_addr, 0, sizeof(client_addr));
        if ((comm_sock = accept(conn_sock, (struct sockaddr *) &client_addr,
                (socklen_t *) &client_addr)) == -1) {
            perror("couldn't accept\n");
            continue;
        }
        cout << "accepted" << endl;
        bzero(packet, 10);
        m = recv(conn_sock, packet, 10, 0);

        if (m < 0) {
            perror("recv failed");
            exit(1);
        }
       cout<<"recieved"<<endl;
        /* Write a response to the client */
        n = send(conn_sock, buffer, sizeof(buffer), 0);

        if (n < 0) {
            perror("ERROR send to client");
            exit(1);
        }

        close(n);
        close(m);
        close(comm_sock);

    }
    close(conn_sock);
    return 0;
}

cilent.cpp:

#define MYPORT 51833
namespace personalization {
bool client::conn() {
    //create socket if it is not already created
    if (sock == -1) {
        //Create socket
        sock = socket(AF_INET, SOCK_STREAM, 0);
        if (sock == -1) {
            perror("Could not create socket");
        }

        cout << "Socket created" << endl;
    }


    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(MYPORT);
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    if (connect(sock , (struct sockaddr *)&server_addr , sizeof(server_addr)) < 0)
        {
            perror("connect failed. Error");
            return false;
        }

        cout<<"Connected\n";
        return true;

    close(sock);

}
bool client::send_data() {
    //Send some data
     if( send(sock , packet , sizeof( packet ) , 0) < 0)
        {
            perror("Send failed");
            return false;
        }
        cout<<"Data send\n";

        return true;
        close(sock);
    }

bool client::rec_data() {
    char buffer[20];
    string reply;

    //Receive a echo from the server
    if (recv(sock, buffer, sizeof(buffer), 0) < 0) {
        perror("receive failed");
        return false;
    }

    reply = buffer;

    return true;
    close(sock);
}

client::client() {
    // TODO Auto-generated constructor stub
sock=-1;

}

output is:

server:Already create socket!!!

Listening For Connection...

port number 51833
  client:  Socket created
    Connected
    Data send
    receive failed: Connection reset by peer

or:

server:Already create socket!!!

Listening For Connection...

port number 51833
client:Socket created
    Connected
    Data send
srver:accepted
recv failed: Transport endpoint is not connected
isan
  • 25
  • 8

1 Answers1

0

In the server's recv and send calls, you need to pass the socket returned from accept.

So instead of

m = recv(conn_sock, packet, 10, 0);

do

m = recv(comm_sock, packet, 10, 0);

Same goes for the send call.

Also, don't call close on n and m, that is to say remove these two lines of code:

    close(n);
    close(m);

EDIT: Sorry, while I'm at it, this is probably not what you intended in the client's send_data and rec_data:

return true;
close(sock);
Carsten Hansen
  • 1,508
  • 2
  • 21
  • 27
  • thank you for your answer error solved ,but when output `client` is `data send` ,in other hand server doesn't show output `receive` and `accepted` always,sometimes show...why??? – isan Aug 17 '15 at 08:28
  • Ensure that the server doesn't terminate before `cout` gets flushed to screen. Check http://stackoverflow.com/questions/22345226/when-does-cout-flush#22345401 and maybe consider using a unbuffered print function for these messages. Please Mark as Answer if the original problem is solved, thank you – Carsten Hansen Aug 17 '15 at 08:38
  • @carstan flush(cout) is it true??can you show me if false...?? – isan Aug 17 '15 at 08:58
  • I think I'll need a few more details, such as: Does the server keep running, does it `exit`, or do you terminate it at any point? Does the client successfully send data and receive a response? What printouts do you see with the fixes implemented? – Carsten Hansen Aug 17 '15 at 09:56
  • when I run the program i see just Already create socket!!! `Listening For Connection... port number 53497 accepted received sending.....` or `Socket created Connected Data send receive your message` in 2 cases program keep running ,yes server keep running,yes client has successfully send data and receive – isan Aug 18 '15 at 03:50
  • you mean I shouldn't use`close(sock);` in send and rec?? – isan Aug 18 '15 at 04:51
  • The `close` calls are never executed because of the `return` statements before them. It is unreachable code (which the compiler should also have warned about) – Carsten Hansen Aug 18 '15 at 04:58
  • Those printouts are what I would expect to see. – Carsten Hansen Aug 18 '15 at 05:00