Socket in C does not compile

Asked

Viewed 706 times

1

I’m having trouble creating a C socket on Ubuntu Linux. I’ve done everything like the guy explains in class and my code doesn’t compile.

Code

GNU nano 2.5.3                          Arquivo: socket.c                                                            

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

int main()
{
        int meusocket;
        int conecta;

        struct sockaddr_in alvo;

        meusocket  = socket(AF_INET, SOCKET_STREAM, 0);
        alvo.sin_family = AF_INET;
        alvo.sin_port = htons(80);
        alvo.sin_addr.s_addr = inet_addr("192.168.0.1");

        conecta = connect(meusocket, (struct sockaddr *)&alvo, sizeof alvo);

        if(conecta == 0)
        {
                printf("Porta aberta \n");
                close(meusocket);
                close(conecta);
        }else{
                printf("Porta Fechada \n");
        }
}

Error When Compiling

socket.c: In function ‘main’:
socket.c:11:31: error: ‘SOCKET_STREAM’ undeclared (first use in this function)
  meusocket  = socket(AF_INET, SOCKET_STREAM, 0);
                               ^
socket.c:11:31: note: each undeclared identifier is reported only once for each function it appears in
socket.c:14:25: warning: implicit declaration of function ‘inet_addr’ [-Wimplicit-function-declaration]
  alvo.sin_addr.s_addr = inet_addr("192.168.0.1");
                         ^
socket.c:21:3: warning: implicit declaration of function ‘close’ [-Wimplicit-function-declaration]
   close(meusocket);
  • I formatted the question so that it fits the site and becomes more legible.

  • Okay thanks haha, I was trying to do that, I saw that it was not as legible as I wanted.

  • los mensajes de error deben enviar a stderr, not stdout. Cuando el error proviene de una función del sistema, use perror () so that the Moon through which the Cree system that produces the error is also emitted

  • 1

    as regards to: meusocket = socket(AF_INET, SOCKET_STREAM, 0); quizás quisiste decir: ``meusocket = socket(AF_INET, SOCK_STREAM, 0);`

1 answer

2

suggest code:

  1. incorporate comments to the question
  2. compile cleanly
  3. documents why each header file is included

and now the proposed code

#include <stdio.h>        // printf(), perror()
#include <sys/types.h>    // AF_INET, SOCK_STREAM
#include <sys/socket.h>   // socket(), connect()
#include <netinet/in.h>   // struct sockaddr_in
#include <arpa/inet.h>    // htons(), inet_addr()
#include <unistd.h>       // close()
//#include <netdb.h>

int main( void )
{
        int meusocket;
        int conecta;

        struct sockaddr_in alvo;

        meusocket  = socket(AF_INET, SOCK_STREAM, 0);
        alvo.sin_family = AF_INET;
        alvo.sin_port = htons(80);
        alvo.sin_addr.s_addr = inet_addr("192.168.0.1");

        conecta = connect(meusocket, (struct sockaddr *)&alvo, sizeof alvo);

        if(conecta == 0)
        {
                printf("Porta aberta \n");
                close(meusocket);
                //close(conecta);
        }else{
                perror( "connect falhou" );
                printf("Porta Fechada \n");
        }
}
  • Thanks , I was able to compile with your code , thank you very much :)

  • 1

    as the structure sockaddr_in alvo is automatic (created on the stack), so it needs to be reset before being used: struct sockaddr_in alvo; memset(&alvo, 0, sizeof(alvo));

  • Thank you so much for your help! Thanks a lot, I learned a lot...

Browser other questions tagged

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