![]() |
Pages (2): « 1 [2] Show 150 posts per page |
.dsy:it. (http://www.dsy.it/forum/)
- Reti di calcolatori (http://www.dsy.it/forum/forumdisplay.php?forumid=68)
-- [LAB] Programmazione di rete in C (http://www.dsy.it/forum/showthread.php?threadid=16704)
roberto a quando il codice funzionante tramite UDP?
o salvatore, illumina il mio cammino ![]()
Ecco i codici di una comunicazione UDP client-server in C.
Client.c
code:
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> main(){ int sd, rc, i; struct sockaddr_in clientAddress, serverAddress; struct hostent *hp; char buff_in[8000]; hp = gethostbyname("127.0.0.1"); serverAddress.sin_family = PF_INET; serverAddress.sin_addr.s_addr = ((struct in_addr*)(hp->h_addr))->s_addr; serverAddress.sin_port = htons(5001); sd = socket(AF_INET,SOCK_DGRAM,0); clientAddress.sin_family = PF_INET; clientAddress.sin_addr.s_addr = INADDR_ANY; clientAddress.sin_port = htons(0); rc = bind(sd, (struct sockaddr *) &clientAddress, sizeof(clientAddress)); printf("Messaggio da inviare al server UDP: "); scanf("%s", buff_in); rc = sendto(sd, buff_in, strlen(buff_in), 0, (struct sockaddr *)&serverAddress, sizeof(serverAddress)); close(sd); return EXIT_SUCCESS; }
code:
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> main(){ int sd, rc, n, clientLength; struct sockaddr_in clientAddress, serverAddress; char buff_in[8000]; sd=socket(AF_INET, SOCK_DGRAM, 0); serverAddress.sin_family = PF_INET; serverAddress.sin_addr.s_addr = INADDR_ANY; serverAddress.sin_port = htons(5001); rc = bind (sd, (struct sockaddr *) &serverAddress,sizeof(serverAddress)); while(1) { clientLength = sizeof(clientAddress); n = recvfrom(sd, buff_in, sizeof(buff_in), 0, (struct sockaddr *) &clientAddress, &clientLength); printf("Ricevuto da %s:%d: %s \n",inet_ntoa(clientAddress.sin_addr.s_addr),ntohs(clientAddress.sin_port), buff_in); } return EXIT_SUCCESS; }
__________________
E lo sceriffo americano non lo sa / ma quando parla ride tutta la città / vile fellone vieni a singolar tenzone / unza unza umpappà troppe truppe mariscià.
Non ci voleva l'eruzione della lava / e due piccioni si contendono la fava / sposa bagnata, bambina svergognata / la questione è delicata / come bolle la patata.
Basta! E' finita la festa
[L'ovile delle vanità, Folkabbestia]
| All times are GMT. The time now is 11:15. | Pages (2): « 1 [2] Show all 17 posts from this thread on one page |
Powered by: vBulletin Version 2.3.1
Copyright © Jelsoft Enterprises Limited 2000 - 2002.