giuliomoro@24: /* UDP client in the internet domain */ 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: #include giuliomoro@24: giuliomoro@24: void error(const char *); giuliomoro@24: int main(int argc, char *argv[]) giuliomoro@24: { giuliomoro@24: int sock, n; giuliomoro@24: unsigned int length; giuliomoro@24: struct sockaddr_in server, from; giuliomoro@24: struct hostent *hp; giuliomoro@24: char buffer[256]; giuliomoro@24: giuliomoro@24: if (argc != 3) { printf("Usage: server port\n"); giuliomoro@24: exit(1); giuliomoro@24: } giuliomoro@24: giuliomoro@24: server.sin_family = AF_INET; giuliomoro@24: hp = gethostbyname(argv[1]); giuliomoro@24: if (hp==0) error("Unknown host"); giuliomoro@24: giuliomoro@24: bcopy((char *)hp->h_addr, giuliomoro@24: (char *)&server.sin_addr, giuliomoro@24: hp->h_length); giuliomoro@24: server.sin_port = htons(atoi(argv[2])); giuliomoro@24: length=sizeof(struct sockaddr_in); giuliomoro@24: while (1){ giuliomoro@24: sock= socket(AF_INET, SOCK_DGRAM, 0); giuliomoro@24: if (sock < 0) error("socket"); giuliomoro@24: bzero(buffer,256); giuliomoro@24: printf("Please enter the message: "); giuliomoro@24: fgets(buffer,255,stdin); giuliomoro@24: n=sendto(sock,buffer, giuliomoro@24: strlen(buffer),0,(const struct sockaddr *)&server,length); giuliomoro@24: if (n < 0) error("Sendto"); giuliomoro@24: } giuliomoro@24: close(sock); giuliomoro@24: return 0; giuliomoro@24: } giuliomoro@24: giuliomoro@24: void error(const char *msg) giuliomoro@24: { giuliomoro@24: perror(msg); giuliomoro@24: exit(0); giuliomoro@24: }