昱仁
Lv 5
昱仁 asked in 電腦與網際網路程式設計 · 1 decade ago

sendto() 這個函式怎麼用呢? 有沒有原型和說明...

sendto() 這個函式怎麼用呢? 有沒有原型和說明...

MSDN看不懂...MSDN要怎麼找函式說明放在哪裡呢??

謝謝了

Update:

嗯嗯,winsock裡面的~!!

Update 2:

謝了~龍大@@!

2 Answers

Rating
  • Wow
    Lv 6
    1 decade ago
    Favorite Answer

    The sendto function sends data to a specific destination.

    int sendto(

    SOCKET s,

    const char* buf,

    int len,

    int flags,

    const struct sockaddr* to,

    int tolen

    );

    Parameters

    s

    [in] Descriptor identifying a (possibly connected) socket.

    buf

    [in] Buffer containing the data to be transmitted.

    len

    [in] Length of the data in buf, in bytes.

    flags

    [in] Indicator specifying the way in which the call is made.

    to

    [in] Optional pointer to a sockaddr structure that contains the address of the target socket.

    tolen

    [in] Size of the address in to, in bytes.

    Return Values

    If no error occurs, sendto returns the total number of bytes sent, which can be less than the number indicated by len. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

    The following example demonstrates the use of the sendto function.

    #include <stdio.h>

    #include "winsock2.h"

    void main() {

    WSADATA wsaData;

    SOCKET SendSocket;

    sockaddr_in RecvAddr;

    int Port = 27015;

    char SendBuf[1024];

    int BufLen = 1024;

    //---------------------------------------------

    // Initialize Winsock

    WSAStartup(MAKEWORD(2,2), &wsaData);

    //---------------------------------------------

    // Create a socket for sending data

    SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    //---------------------------------------------

    // Set up the RecvAddr structure with the IP address of

    // the receiver (in this example case "123.456.789.1")

    // and the specified port number.

    RecvAddr.sin_family = AF_INET;

    RecvAddr.sin_port = htons(Port);

    RecvAddr.sin_addr.s_addr = inet_addr("123.456.789.1");

    //---------------------------------------------

    // Send a datagram to the receiver

    printf("Sending a datagram to the receiver...\n");

    sendto(SendSocket,

    SendBuf,

    BufLen,

    0,

    (SOCKADDR *) &RecvAddr,

    sizeof(RecvAddr));

    //---------------------------------------------

    // When the application is finished sending, close the socket.

    printf("Finished sending. Closing socket.\n");

    closesocket(SendSocket);

    //---------------------------------------------

    // Clean up and quit.

    printf("Exiting.\n");

    WSACleanup();

    return;

    }

  • Lv 7
    1 decade ago

    你是在講Socket這個型態裡的SendTo()?

    2006-04-03 03:04:42 補充:

    參考一下

    http://msdn2.microsoft.com/en-us/library/beez6ewa....

Still have questions? Get your answers by asking now.