我写了个udp发送程序,但程序调试一直都处于发送不成功那一行!程序的倒数第四行!麻烦大家给看看!
         WSADATA wsaData;
int ret=WSAStartup(0x0202, &wsaData);
if(ret!=0)
{
return false;
}
ClientSock=socket(AF_INET,SOCK_DGRAM,0);
if(ClientSock==INVALID_SOCKET)
{
WSACleanup();
return FALSE;
}
int iError;
struct sockaddr_in Local_addr_in;
Local_addr_in.sin_family =AF_INET;
Local_addr_in.sin_port =htons(4001);
Local_addr_in.sin_addr.s_addr =htonl( INADDR_ANY );
iError = bind(ClientSock,(const struct sockaddr*)&Local_addr_in,\
sizeof(Local_addr_in));
if( SOCKET_ERROR == iError)
{
closesocket(ClientSock);
WSACleanup();
return false;
}
//绑定ip和端口到socket
struct sockaddr_in Local_addr_in;
Local_addr_in.sin_family =AF_INET;
Local_addr_in.sin_port =htons(4001);
Local_addr_in.sin_addr.s_addr =htonl( INADDR_ANY );
         int iBufSize=1000;
iError = setsockopt(ClientSock, SOL_SOCKET, SO_SNDBUF,(char *)&iBufSize, sizeof(int));
if( SOCKET_ERROR == iError )
{
iError = WSAGetLastError();
closesocket(ClientSock);
WSACleanup();
return;
}
char sendBuf[100];
int iLenData;
for(int i=0;i<100;i++)
{
    sendBuf[i]='1';
}
         struct sockaddr_in SockSend;
int iRet = sendto(ClientSock, sendBuf, strlen(sendBuf), 0,(const struct sockaddr *)&SockSend,iLenData);
if( SOCKET_ERROR == iRet )
{
    int iRet = WSAGetLastError();
    AfxMessageBox(_T("发送不成功!"));
}
else
    AfxMessageBox(_T("发送成功!"));

解决方案 »

  1.   

    int iRet = sendto(ClientSock, sendBuf, strlen(sendBuf), 0,(const struct sockaddr *)&SockSend,iLenData);中的 strlen(sendBuf) 不正确,因为在前面少了一句 sendBuf[100] = '\0'。
      

  2.   


    int iLenData=sizeof(sockaddr_in );
    sendBuf[100] = '\0';
    int iRet = sendto(ClientSock, sendBuf, strlen(sendBuf), 0,(const struct sockaddr *)&SockSend,&iLenData);
      

  3.   

    不是sendBuf的问题!而且程序也很混乱!为你提供个例子!
    /*--------Sender-----------*/
    #pragma comment (lib,"Ws2_32.lib")
    #include <winsock2.h>
    #include <stdio.h>void main(void)
    {
       WSADATA              wsaData;
       SOCKET               ReceivingSocket;
       SOCKADDR_IN          ReceiverAddr;
       int                  Port = 5150;
       char                 ReceiveBuf[1024];
       int                  BufLength = 1024;
       SOCKADDR_IN          SenderAddr;
       int                  SenderAddrSize = sizeof(SenderAddr);
       int                  Ret;
       // Initialize Winsock version 2.2   if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
       {
          // NOTE: Since Winsock failed to load we cannot use WSAGetLastError 
          // to determine the error code as is normally done when a Winsock 
          // API fails. We have to report the return status of the function.      printf("ERROR: WSAStartup failed with error %d\n", Ret);
          return;
       }   
       // Create a new socket to receive datagrams on.
     
       if ((ReceivingSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))
           == INVALID_SOCKET)
       {
          printf("ERROR: socket failed with error %d\n", WSAGetLastError());
          WSACleanup();
          return;
       }    // Setup a SOCKADDR_IN structure that will tell bind that we
       // want to receive datagrams from all interfaces using port
       // 5150.   ReceiverAddr.sin_family = AF_INET;
       ReceiverAddr.sin_port = htons(Port);    
       ReceiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);   // Associate the address information with the socket using bind.   if (bind(ReceivingSocket, (SOCKADDR *)&ReceiverAddr, sizeof(ReceiverAddr))
           == SOCKET_ERROR)
       {
          printf("ERROR: bind failed with error %d\n", WSAGetLastError());
          closesocket(ReceivingSocket);
          WSACleanup();
          return;
       }   printf("We are ready to receive 1 datagram from any interface on port %d...\n",
              Port);   // At this point you can receive datagrams on your bound socket.   if ((Ret = recvfrom(ReceivingSocket, ReceiveBuf, BufLength, 0,
           (SOCKADDR *)&SenderAddr, &SenderAddrSize)) == SOCKET_ERROR)
       {
          printf("ERROR: recvfrom failed with error %d\n", WSAGetLastError());
          closesocket(ReceivingSocket);
          WSACleanup();
          return;
       }   printf("We successfully received %d bytes from address %s:%d.\n", Ret,
              inet_ntoa(SenderAddr.sin_addr), ntohs(SenderAddr.sin_port));
       // When your application is finished receiving datagrams close
       // the socket.   closesocket(ReceivingSocket);   // When your application is finished call WSACleanup.   WSACleanup();
    }/*--------Receiver  -----------*/#pragma comment (lib,"Ws2_32.lib")
    #include <winsock2.h>
    #include <stdio.h>void main(int argc, char **argv)
    {
       WSADATA              wsaData;
       SOCKET               SendingSocket;
       SOCKADDR_IN          ReceiverAddr;
       int                  Port = 5150;
       int                  Ret;   if (argc <= 1)
       {
          printf("USAGE: udpsender <receiver IP address>.\n");
          return;
       }   // Initialize Winsock version 2.2   if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0)
       {
          // NOTE: Since Winsock failed to load we cannot use WSAGetLastError 
          // to determine the error code as is normally done when a Winsock 
          // API fails. We have to report the return status of the function.      printf("ERROR: WSAStartup failed with error %d\n", Ret);
          return;
       }
       
       // Create a new socket to receive datagrams on.
     
       if ((SendingSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))
           == INVALID_SOCKET)
       {
          printf("ERROR: socket failed with error %d\n", WSAGetLastError());
          WSACleanup();
          return;
       }   // Setup a SOCKADDR_IN structure that will identify who we
       // will send datagrams to. For demonstration purposes, let's
       // assume our receiver's IP address is 136.149.3.29 and waits
       // for datagrams on port 5150. Obviously you will want to prompt 
       // the user for an IP address and port number and fill these 
       // fields in with the data from the user.   ReceiverAddr.sin_family = AF_INET;
       ReceiverAddr.sin_port = htons(Port);    
       ReceiverAddr.sin_addr.s_addr = inet_addr(argv[1]);   // Send a datagram to the receiver.   if ((Ret = sendto(SendingSocket, "Hello", 5, 0, 
           (SOCKADDR *)&ReceiverAddr, sizeof(ReceiverAddr))) == SOCKET_ERROR)
       {
          printf("ERROR: sendto failed with error %d\n", WSAGetLastError());
          closesocket(SendingSocket);
          WSACleanup();
          return;
       }   // When your application is finished sending datagrams close
       // the socket.   printf("We successfully sent %d byte(s) to %s:%d.\n", Ret,
              inet_ntoa(ReceiverAddr.sin_addr), htons(ReceiverAddr.sin_port));   closesocket(SendingSocket);   // When your application is finished call WSACleanup.   WSACleanup();
    }
      

  4.   

    iRet 值是什么?在msdn里看看就知道什么原因了!