giuliomoro@24: /* Creates a datagram server. The port giuliomoro@24: number is passed as an argument. This giuliomoro@24: server runs forever */ giuliomoro@24: giuliomoro@24: #include giuliomoro@24: #include giuliomoro@24: #include giuliomoro@24: #include giuliomoro@24: #include giuliomoro@24: #include giuliomoro@24: #include giuliomoro@24: #include giuliomoro@24: giuliomoro@24: void error(const char *msg) giuliomoro@24: { giuliomoro@24: perror(msg); giuliomoro@24: exit(0); giuliomoro@24: } giuliomoro@24: giuliomoro@24: int main(int argc, char *argv[]) giuliomoro@24: { giuliomoro@24: int sock, length, n; giuliomoro@24: socklen_t fromlen; giuliomoro@24: struct sockaddr_in server; giuliomoro@24: struct sockaddr_in from; giuliomoro@93: float buf[2048]; giuliomoro@93: int i=0; giuliomoro@93: for(i=0; i<2048; i++){ giuliomoro@93: buf[i]=0; giuliomoro@93: } giuliomoro@24: if (argc < 2) { giuliomoro@24: fprintf(stderr, "ERROR, no port provided\n"); giuliomoro@24: exit(0); giuliomoro@24: } giuliomoro@24: giuliomoro@24: sock=socket(AF_INET, SOCK_DGRAM, 0); giuliomoro@24: if (sock < 0) error("Opening socket"); giuliomoro@24: length = sizeof(server); giuliomoro@24: bzero(&server,length); giuliomoro@24: server.sin_family=AF_INET; giuliomoro@24: server.sin_addr.s_addr=INADDR_ANY; giuliomoro@24: server.sin_port=htons(atoi(argv[1])); giuliomoro@24: if (bind(sock,(struct sockaddr *)&server,length)<0) giuliomoro@24: error("binding"); giuliomoro@24: fromlen = sizeof(struct sockaddr_in); giuliomoro@24: while (1) { giuliomoro@93: n = recvfrom(sock,buf,2048,0,(struct sockaddr *)&from,&fromlen); giuliomoro@24: if (n < 0) error("recvfrom"); giuliomoro@93: printf("Received a datagram of size %d: \n", n); giuliomoro@93: for(i=0; i