int sendto(int sock, void *data, int len, // Transmitted data int flags, // Transmit options struct sockaddr *to, int to_len); // Destination network address |
Meaining of the parameters:
sock = socket variable (should be bounded) data = pointer to data buffer location len = number bytes in data buffer to be sent flag = flags (indicate send options) E.g., Set MSG_OOB bit for "out-of-band" (high priority) data to = Internet address (typed) variable contains: network address of the destination to_len = length of the "to" data structure |
|
Graphically:
Error: returns -1 Otherwise: returns len (≥ 0) |
General error processing:
|
char data[] = "Hello"; // String has 6 characters: H e l l o \0 !!! struct sockaddr_in dest_addr; // Internet address type /* ----------------------------------------- Set up destination: (IPaddress, Port#) ----------------------------------------- */ dest_addr.sin_family = AF_INET; dest_addr.sin_addr.s_addr = htonl(DestIPaddr); dest_addr.sin_port = htons(DestPosrtNumber); if ( sendto(s, data, 6, 0 /* no flags */, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) == -1 ) { perrer("sendto()"); } |
How to run the program:
|
Note:
|
|
Analogy:
|
|
|