Socket error in C recv(), send() // SERVER [CLOSED]

Asked

Viewed 34 times

0

I am new in c and in the area of networks, went to create a code in c to put into practice the theory, however, after bindar and create the connect socket, any function like recv() or send() returns me -1 [error]. Sometimes too, just by putting a printf() or changing anything in the code I get a connection error on the client side. To simulate a client I am using netcat 7.8. If there is a possibility of someone explaining to me why of this mistake I will be grateful. If there are suggestions for code improvements and everything else please do not hesitate to talk! Thank you.

Here’s the code:

#include <stdio.h>
#include <netdb.h>
#include <stdlib.h>

int main()
{
    printf("\niniciando ...\n");
    int server, conecta, client,x;
    struct sockaddr_in endereco_servidor;
    struct sockaddr_in endereco_cliente;

    server = socket(AF_INET, SOCK_STREAM, 0);

    endereco_servidor.sin_family = AF_INET;
    endereco_servidor.sin_port = htons(30000);
    endereco_servidor.sin_addr.s_addr = inet_addr("10.0.0.176");

    bind(server, (struct sockaddr *)&endereco_servidor, sizeof endereco_servidor);
    x = listen(server, 1);
    while(1==1)
    {
        printf("\nENtROU WHILE\n");
        client = accept(server, (struct sockaddr *)&endereco_cliente, sizeof endereco_cliente);  
        x = send(client, "Ola", 100, 0);
        printf("%i", x);
    }
    return 0;
} 

1 answer

1


The main problem is this:

In the call to accept you are passing the third argument incorrectly. This third argument should be the memory address of the variable that stores the size of the struct relative to the client. This is due to the fact that this function can modify it.

So you can declare a new variable for this before:

int size_endereco_cliente = sizeof(endereco_cliente);

And in the call to accept pass your pointer this way:

client = accept(server, (struct sockaddr *) &endereco_cliente, &size_endereco_cliente);

Furthermore, if you are not interested in knowing customer information, it is possible to simply pass on NULL as second and third arguments:

client = accept(server, NULL, NULL);

Is an alternative.

Improvements:

There is a defect in shipping the "Hello" to the customer. Running the way you are you will notice that the customer will receive some strange characters right after the message. This is because there is a need to adjust the third argument of send to the correct size of the message that is in the second argument. And in this case, this size is not 100.

I recommend using a variable to save the message and measure it with strlen() at the time of dispatch.

Tip: You are using the computer IP on the LAN as the server address, maybe it is not static. In that case, I recommend using 127.0.0.1 to do the tests if they are only on your computer.

References:

https://www.geeksforgeeks.org/accept-system-call

http://man7.org/linux/man-pages/man2/accept.2.html

https://linux.die.net/man/2/send

  • In the part where I use (struct sockaddr *)&address_client, what does this pointer mean? Ah, and thanks for the answer and very clear tips, they were of great help!

Browser other questions tagged

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