1

I'm trying to create server/client communication and it faileson the connect function with errno 88. I checked if the socketfd is valid (because this errno means that I'm trying to make socket operation on non socket object).

int main (int argc, char *argv[])
{
// Validity check
if (argc != VALID_ARG_NUM) {
    std::cout << INVALID_ARG;
    return 0; // todo - return 0 is correct??
}

int port;
struct sockaddr_in server_address;

// server_address initialization.
server_address.sin_family = AF_INET;
server_address.sin_port = htons((uint32_t)atof(argv[3]));
server_address.sin_addr.s_addr = inet_addr(argv[2]);
memset(&(server_address.sin_zero), '\0', sizeof(server_address));

// Create the client socket.
if (sockfd = socket(AF_INET, SOCK_STREAM, 0) < 0) {
    std::cout << "ERROR: socket " << errno << "." << std::endl;
    exit(1);
}
// Connect to server.
if (connect(sockfd, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) {
    std::cout << "ERROR: connect " << errno << "." << std::endl;
    std::cout << CON_FAIL;
    close(sockfd);
    exit(1);
}
.......

thank you.

3 Answers3

1

I think this part is causing buffer overrun:

memset(&(server_address.sin_zero), '\0', sizeof(server_address));

You should zero the whole struct before writing somefields:

struct sockaddr_in server_address;
// server_address initialization.
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
user7860670
  • 35,849
  • 4
  • 58
  • 84
1

The precedence of the statement

sockfd = socket(AF_INET, SOCK_STREAM, 0) < 0

is

sockfd = ((socket(AF_INET, SOCK_STREAM, 0)) < 0)

and you are therefore assigning a boolean, which is implicitly converted to an integer (or whatever type sockfd has). What you probably meant was instead

(sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0
You
  • 22,800
  • 3
  • 51
  • 64
0

Broken pipe usually means that remote peer closes connection. Are you sure your server code is OK? Maybe it just do something like this?

int main()
{
...
listen();
accept();

return 0;//procees exits and therefore brakes connection
}
vatosarmat
  • 1,090
  • 10
  • 22