elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Introducción a Git (Primera Parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  sockets C cliente&server error en cliente.... :huh:
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: sockets C cliente&server error en cliente.... :huh:  (Leído 2,575 veces)
kotora

Desconectado Desconectado

Mensajes: 6


Ver Perfil
sockets C cliente&server error en cliente.... :huh:
« en: 2 Noviembre 2013, 20:12 pm »

Buenas, quiero establecer conexión entre un server y un cliente mediante sockets UDP...

He estado investigando por la red y después de leer, leeeer y leeeeeer.... he cogido unos codes por la red y los he "troleado", en lado server uso OS X y en lado cliente uso Debian...

Os posteo los codes

////////////////////SERVER//////////////////////
////////////////////////////////////////////////

// Ejemplo de sockets en Linux que mandan y reciben datagramas UDP
#include <stdio.h>
#include <stdlib.h>

// Estas cabeceras son necesarias para los 'sockets'
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>  // Usado por 'timeval'
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>  // Usado por 'close'

#include <string.h>  // Usado por 'strlen'
#define PORT 25555
#define BUFSIZE 1024

int main(int argc, char *argv[])
{



        struct sockaddr_in myaddr;      /* our address */
        struct sockaddr_in remaddr;     /* remote address */
        socklen_t addrlen = sizeof(remaddr);            /* length of addresses */
        int recvlen;                    /* # bytes received */
        int fd;                         /* our socket */
        unsigned char buf[BUFSIZE];     /* receive buffer */

        /* create a UDP socket */

        if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
                perror("cannot create socket\n");
                return 0;
        }

        /* bind the socket to any valid IP address and a specific port */

        memset((char *)&myaddr, 0, sizeof(myaddr));
        myaddr.sin_family = AF_INET;
        myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        myaddr.sin_port = htons(PORT);

        if (bind(fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
                perror("bind failed");
                return 0;
        }

        /* now loop, receiving data and printing what we received */
        for (;;) {
                printf("waiting on port %d\n", PORT);
                recvlen = recvfrom(fd, buf, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen);
                printf("received %d bytes\n", recvlen);
                if (recvlen > 0) {
                        buf[recvlen] = 0;
                        printf("received message: \"%s\"\n", buf);
                }
        }
        /* never exits */
}



//////////////////CLIENT///////////////////
///////////////////////////////////////////
// Ejemplo de sockets en Linux que mandan y reciben datagramas UDP
#include <stdio.h>
#include <stdlib.h>

// Estas cabeceras son necesarias para los 'sockets'
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>  // Usado por 'timeval'
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>  // Usado por 'close'

#include <string.h>  // Usado por 'strlen'
#define PORT 25555
#define BUFFER_LEN 1024

int main(int argc, char *argv[])
{
        char *host = "192.168.1.102";
        int port=PORT;
        int socketfd;
        if( (socketfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
                perror("cannot create socket");
                return 0;
        }

        struct sockaddr_in myaddr;

        /* bind to an arbitrary return address */
        /* because this is the client side, we don't care about the address */
        /* since no application will initiate communication here - it will */
        /* just send responses */
        /* INADDR_ANY is the IP address and 0 is the socket */
        /* htonl converts a long integer (e.g. address) to a network representation */
        /* htons converts a short integer (e.g. port) to a network representation */

        memset((char *)&myaddr, 0, sizeof(myaddr));
        myaddr.sin_family = AF_INET;
        myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        myaddr.sin_port = htons(0);

        if (bind(socketfd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
                perror("bind failed");
                return 0;
                }


        struct hostent *hp;     /* host information */
        struct sockaddr_in servaddr;    /* server address */
        char *my_message = "this is a test message";

/* fill in the server's address and data */
        memset((char*)&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(port);

/* look up the address of the server given its name */
        hp = gethostbyname(host);
        if (!hp) {
                fprintf(stderr, "could not obtain address of %s\n", host);
                return 0;
        }

/* put the host's address into the server address structure */
        memcpy((void *)&servaddr.sin_addr, hp->h_addr_list[0], hp->h_length);

/* send a message to the server */
        if (sendto(socketfd, &my_message, strlen(my_message), 0, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
                perror("sendto failed");
                return 0;
        }
}



El lado server parece que hace lo que debe, pero el lado client no, en Deban me da error al crear el bind, pero si los invierto, es decir client OS X y server Debian no me da error pero no llega nada   :huy: 

¿¿Alguein me podría decir donde falla??  Gracias y un saludo!


En línea

kotora

Desconectado Desconectado

Mensajes: 6


Ver Perfil
Re: sockets C cliente&server error en cliente.... :huh:
« Respuesta #1 en: 2 Noviembre 2013, 20:14 pm »

Perdonar mi torpeza, no quiero establecer conexión(para los puristas ;) ), quiero transmitir el puntero *my_message = "this is a test message" del cliente al server.


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Problema con sockets Cliente - Servidor ( IP )
Programación Visual Basic
EstoyBaneado 8 3,419 Último mensaje 22 Agosto 2005, 02:06 am
por soplo
VB.NET sockets cliente-servidor
.NET (C#, VB.NET, ASP)
Blizknight 2 13,457 Último mensaje 11 Marzo 2008, 04:53 am
por elmaro
Creacion de Chat con sockets y mas de un cliente
Programación Visual Basic
Banchan 3 4,547 Último mensaje 7 Julio 2008, 02:00 am
por Banchan
Comunicación mediante sockets RAW (cliente-servidor)
Programación C/C++
PeKiN 2 5,317 Último mensaje 20 Febrero 2011, 22:06 pm
por PeKiN
AYUDA CON SOCKETS (CLIENTE/SERVIDOR)
Programación C/C++
ApOkAlizE 4 4,219 Último mensaje 29 Febrero 2012, 22:56 pm
por avmiitxe
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines