Hola tengo el siguiente error estoy practicando socket y estoy tratando de compilar este código que me encontre por hay pero me da los siguientes errores cuando intento compilar:
servidor.cpp: In function 'int main(int, char**)':
servidor.cpp:34:73: error: invalid conversion from 'int*' to 'socklen_t*'
servidor.cpp:34:73: error: initializing argument 3 of 'int accept(int, sockaddr*, socklen_t*)
Bueno el código es este:
#define PORTNUM 2343
int main(int argc, char *argv[])
{
char msg[] = "Hello World !\n";
struct sockaddr_in dest; /* socket info about the machine connecting to us */
struct sockaddr_in serv; /* socket info about our server */
int mysocket; /* socket used to listen for incoming connections */
int socksize = sizeof(struct sockaddr_in);
memset(&serv, 0, sizeof(serv)); /* zero the struct before filling the fields */
serv.sin_family = AF_INET; /* set the type of connection to TCP/IP */
serv.sin_addr.s_addr = INADDR_ANY; /* set our address to any interface */
serv.sin_port = htons(PORTNUM); /* set the server port number */
mysocket = socket(AF_INET, SOCK_STREAM, 0);
/* bind serv information to mysocket */
bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
/* start listening, allowing a queue of up to 1 pending connection */
listen(mysocket, 1);
int consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
while(consocket)
{
printf("Incoming connection from %s - sending welcome\n", inet_ntoa(dest.sin_addr));
send(consocket, msg, strlen(msg), 0);
}
close(consocket);
close(mysocket);
return EXIT_SUCCESS;
}
Alguien Sabe :huh: ???
Teóricamente te debería compilar, si introdujeras las bibliotecas correspondientes. ¿Cómo compilaste?
Además de que no sé de dónde has sacado la función close...
esta es la linea que me manda error las demás están bien pero no se porque >:D
int consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
¿No te da errores, si no incluyes las librerías?
No sé, depura y mira qué pasa. Te debería funcionar, si no, crea un nuevo proyecto.
Cita de: Queta en 28 Junio 2011, 23:20 PM
Además de que no sé de dónde has sacado la función close...
Si incluí las librerías solamente que no las puse cuando mostré todo el código:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORTNUM 2343
Como puse al principio, el código no es mio lo tome prestado, y si borras las lineas de close, siempre tira el mismo error en las mismas lineas
Pensaba que trabajabas con los sockets de Windows, entonces
close sí existe. No sé, te voy a decir como ultimátum que cambies el tercer parámetro por un puntero a
socklen_t :
Citara pointer to a socklen_t location that specifies the size of the client address structure passed to accept(). When accept() returns, this location indicates how many bytes of the structure were actually used.
Bien tenias toda la razón eso es!! ;D ;D
Mira qué bien ;D.