🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: es.comp.os.linux.programacion
2 messages
2 total messages Started by Switch Thu, 26 Aug 1999 00:00
Sockets.
#18509
Author: Switch
Date: Thu, 26 Aug 1999 00:00
52 lines
2001 bytes
Hola.
Estoy programando un demonio  en C que recoge peticiones por un socket
(servidor).
Cuando le llega una peticion lanza un thread que la atiende.
Este Thread recibe como par�metro el identificador de socket, que m�s
adelante usa para devolver la respuesta.
El problema es que necesito saber la direcci�n IP del cliente a partir del
identificador de socket para verificar que el lugar desde el que se conecta
es v�lido.
�Como saco yo un array de caracteres que contenga la direcci�n IP del
cliente a partir del identificador de socket?
La conexi�n se acepta as�:


{
        int result;
        int addrlen;
        struct sockaddr_in i_name;

        if (listen(sock_id, 5) == -1)
                return -1;


        fcntl(sock_id, F_SETFL, fcntl(sock_id, F_GETFL, 0) & ~O_NONBLOCK);

        /* Bucle aceptando conexiones y creando threads */
        while (TRUE) {
                addrlen = sizeof(i_name);
                result = accept(sock_id, (struct sockaddr *)&i_name,
&addrlen);

                if (result == -1) {
                        switch(errno) {
                        case ECONNABORTED:
                        case EINTR:
                                return 0;
                        }
                        return -1;
                }
                else {
                        fcntl(result, F_SETFL, fcntl(result, F_GETFL, 0) &
~O_NONBLOCK);

                        thread_Ejecuta(result);

                }
        }

        return 0;
}

--
Re: Sockets.
#18512
Author: Ignacio Arenaza
Date: Thu, 26 Aug 1999 00:00
15 lines
1001 bytes
>>>>> "Switch" == Switch  <fros@bitel.es> writes:

    Switch> desde el que se conecta es v�lido.  �Como saco yo un array
    Switch> de caracteres que contenga la direcci�n IP del cliente a
    Switch> partir del identificador de socket?  La conexi�n se acepta

Echa una ojeada a la funcion getpeername(2). Esto te devuelve la IP
del host al otro extremo, asi que luego usas gethostbyname/gethostbyaddr 
en funcion de lo que desees y listo.

Saludos. I�aki.

-- 
  PGP Key available at http://personales.jet.es/inaki.arenaza/pgpkey.html
  Linux Registered User #44369
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads