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.
– user28595
Okay thanks haha, I was trying to do that, I saw that it was not as legible as I wanted.
– Burns
los mensajes de error deben enviar a
stderr
, notstdout
. Cuando el error proviene de una función del sistema, useperror ()
so that the Moon through which the Cree system that produces the error is also emitted– user3629249
as regards to:
meusocket = socket(AF_INET, SOCKET_STREAM, 0);
quizás quisiste decir: ``meusocket = socket(AF_INET, SOCK_STREAM, 0);`– user3629249