/* dgread.c */ /* Read a datagram through a connection established by bind */ #define begin { #define end } #include #include #include #include #include char buf[100]; struct hostent *hp; struct hostent *gethostbyname(); struct sockaddr_in name; main(argc, argv) int argc; char *argv[]; begin int i; unsigned char c; int sock; int netaddr; int status; sock = socket(PF_INET, SOCK_DGRAM, 0); if (sock < 0) begin printf("Socket create failed \n"); exit(1); end printf("Socket id = %d \n", sock); hp = gethostbyname(argv[1]); if (hp == 0) begin printf("Host %s not found\n", argv[1]); exit(1); end bcopy((char *)hp->h_addr, (char *)&name.sin_addr, hp->h_length); name.sin_family = AF_INET; name.sin_port = htons(atoi(argv[2])); status = bind(sock, (struct sockaddr *)&name, sizeof(name)); printf("Bind status = %d \n", status); if (status < 0) exit(1); read(sock, buf, sizeof(buf)); printf("status = %d \n", status); printf("Msg = %s \n", buf); end