Título: sockets C cliente&server error en cliente.... :huh: Publicado por: kotora 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! Título: Re: sockets C cliente&server error en cliente.... :huh: Publicado por: kotora 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.
|