/* aiprecv.c */ /* Demonstates the use of recvfrom for datagram transfer */ #include #include #include #include #include #include char buf[16 * 1024]; struct sockaddr_in name; struct sockaddr_in sname; int namelen; int namesize; char hnamebuf[80]; main( int argc, char** argv) { int i, me, you; unsigned char c; int sock; int netaddr; int status; unsigned dummy; int msgcount; int msock; /* Create datagram socket */ sock = socket(PF_INET, SOCK_STREAM, 0); if (sock < 0) { printf("Socket create failed \n"); exit(1); } dummy = 0; bcopy((char *)&dummy, (char *)&name.sin_addr, sizeof(dummy)); /* Fill in protocol family and port # from command line then */ /* bind the socket to the specified address. */ name.sin_family = AF_INET; name.sin_port = htons(33456); status = bind(sock, (struct sockaddr *)&name, sizeof(name)); printf("Bind status = %d\n", status); if (status < 0) { perror("bind error"); exit(1); } /* Wait for someone to send me a datagram */ namelen = sizeof(struct sockaddr_in); status = listen(sock, 4); if (status < 0) { printf("Listen failed with code %d. \n", errno); exit(4); } status = accept(sock, (struct sockaddr *)&name, &namesize); msock = status; /* Wait for someone to send me a datagram */ status = read(msock, buf, sizeof(buf)); while (status > 0) { write(1, buf, status); status = read(msock, buf, sizeof(buf)); } }