Socket C++ Read Error

Asked

Viewed 88 times

0

I am developing a program in C++ using Socket, the problem is that when the client sends some information to me I do not receive. In fact the program hangs.

#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <cstdlib>
#include <cstring>
#include <unistd.h>




int main(int argc, char *argv[])  {
    int socket_;
    struct sockaddr_in config;
    socklen_t config_len;

    socket_ = socket(AF_INET, SOCK_STREAM, 6);
    struct hostent *hostname = gethostbyname("127.0.0.1");
    if (socket_ < 0) {
        std::cerr << "Deu Merda AA";
    }
    config.sin_family = hostname->h_addrtype;
    config.sin_port = htons(10001);
    config.sin_addr = *((struct in_addr *) hostname->h_addr);
    bzero(&(config.sin_zero), 8);
    config_len = sizeof(config);


    std::cout << "Bind\n";
    if(int e = bind(socket_, (struct sockaddr * )&config, config_len) < 0){
        std::cerr << "Deu Merda A";
        return e;
    }

    std::cout << "Listen\n";
    if(int e = listen(socket_, 5) < 0) {
        std::cerr << "Deu Merda B";
        return e;
    }

    std::cout << "Esperando Cliente:\n";
    int client_socket = accept(socket_,(struct sockaddr * )&config, &config_len);
    std::cout << "Numero do Cliente " << client_socket << "\n";
    if(client_socket < 0){
        std::cerr << "Deu Merda C";
    }else {
        char *buffer;
        bzero(buffer, 201);
        std::cout << read(client_socket,buffer,201) << "\n";
    }

}

My Way out:

Bind
Listen
Esperando Cliente:
Numero do Cliente 4

Process finished with exit code 10

The customer receives the error of Broken Pipe that is, the connection is down. But what’s wrong with my code?

1 answer

2


Pedro, I have an http server implemented with cross-platform sockets in c++ but I do not use the read function you are using, although I use recv which has similar structure.

int recv (int __fd, void *__buf, size_t __n, int __flags)

Where we have the following points:

0) The return is the number of bytes received. (a negative return is an error)

1) The first parameter (__fd) is the file descriptor (aka socket number) you are using for this connection.

2) A pointer to a buffer (I particularly use a char buffer of 4096 bytes)

3) The amount of buffer bytes (4096 in my case).

4) The flags for receiving (particularly I never had to touch this parameter, always use by default ZERO (0).

As the return of the function indicates Broken Pipe it is also necessary to check what type of client is being used to connect to the server and whether its implementation is compatible with yours.

  • It was exactly this problem, besides that when you create a variable in the if for some reason always comes 0. Thank you very much.

  • it is not idiomatic to use identifiers starting with two underlines, these identifiers in principle are reserved for language implementation (Runtime, libraries, etc)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.