Estou com problemas para criar um socket em C no Linux Ubuntu. Ja fiz de tudo como o cara explica na aula e meu codigo nao compila.
Codigo
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");
}
}
Erro Na hora de Compilar
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);
stderr
, not` stdout. Cuando el error proviene de una función del sistema, use
perror ()` para que la razón por la que el sistema cree que se produjo el error también se emitameusocket = socket(AF_INET, SOCKET_STREAM, 0);
quizás quisiste decir: ``meusocket = socket(AF_INET, SOCK_STREAM, 0);`