我地一次用sendto(),我不知道怎么回事,发送信息的时候老是发送不成功,高手帮我看看,下面的程序有问题么?
发送:
if(::ChatSocket.SendTo("good", 8, atoi(::ServerPort), ::ServerIP) != SOCKET_ERROR)
{
OnCancel();
}
else
{
MessageBox("发送失败");
}接收:
if(ReceiveFrom(tem, 8, FromIP, FromPort) != SOCKET_ERROR)
{
AfxMessageBox("成功收到信息");
}
else
{
AfxMessageBox("收到错误信息");
} 接收方根本没有反应,发送方也是报发送失败,能告诉我这里有什么问题么??我还想想问,windows异步socket需要注意一些什么??(顺便帮我解释一下,谢谢~~)

解决方案 »

  1.   

    int SendTo(
       const void* lpBuf,
       int nBufLen,
       UINT nHostPort,
       LPCTSTR lpszHostAddress = NULL,
       int nFlags = 0 
    );
    int SendTo(
       const void* lpBuf,
       int nBufLen,
       const SOCKADDR* lpSockAddr,
       int nSockAddrLen,
       int nFlags = 0 
    );////////////////////////////////
    //你用是UDP协议
    SendTo("good", 8, atoi(::ServerPort), ::ServerIP) //ErrorSendTo("good"),4, /*nHostPort,*/, /*lpszHostAddress */);
      

  2.   

    不好意思 只下到了e版的MSDN 凑活的用吧
    This function sends data to a specific destination.int sendto(
      SOCKET s,
      const char FAR* buf, 
      int len,
      int flags,
      const struct SOCK_ADDR* to,
      int tolen
    );
    Parameters

    [in] Descriptor identifying a possibly connected socket. 
    buf 
    [in] Buffer containing the data to be transmitted. 
    len 
    [in] Length of the data in the buf parameter. 
    flags 
    [in] Indicator specifying the way in which the call is made. 
    to 
    [in] Optional pointer to the address of the target socket. 
    tolen 
    [in] Size of the address in the to parameter. 
    Return Values
    If no error occurs, this function returns the total number of bytes sent, which can be less than the number indicated by len. If an error occurs, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError. The following table shows a list of possible error codes.Error code  Description  
    WSANOTINITIALISED  A successful WSAStartup call must occur before using this function.  
    WSAENETDOWN  The network subsystem has failed.  
    WSAEACCES  The requested address is a broadcast address, but the appropriate flag was not set. Call setsockopt (Windows Sockets) with the SO_BROADCAST parameter to allow the use of the broadcast address.  
    WSAEINVAL  An unknown flag was specified, or MSG_OOB was specified for a socket with SO_OOBINLINE enabled.  
    WSAEINTR  The socket was closed. 
    WSAEINPROGRESS  A blocking Winsock call is in progress, or the service provider is still processing a callback function.  
    WSAEFAULT  The buf or to parameter is not part of the user address space, or the tolen parameter is too small.  
    WSAENETRESET  The connection has been broken due to keep-alive activity detecting a failure while the operation was in progress.  
    WSAENOBUFS  No buffer space is available.  
    WSAENOTCONN  The socket is not connected (connection-oriented sockets only).  
    WSAENOTSOCK The descriptor is not a socket. 
    WSAEOPNOTSUPP MSG_OOB was specified, but the socket is not stream style such as type SOCK_STREAM, out of band (OOB) data is not supported in the communication domain associated with this socket, or the socket is unidirectional and supports only receive operations. 
    WSAESHUTDOWN  The socket has been shut down. It is not possible to call sendto on a socket after shutdown has been invoked with how set to SD_SEND or SD_BOTH.  
    WSAEWOULDBLOCK  The socket is ed as nonblocking and the requested operation would block. 
    WSAEMSGSIZE  The socket is message-oriented, and the message is larger than the maximum supported by the underlying transport.  
    WSAEHOSTUNREACH  The remote host cannot be reached from this host at this time.  
    WSAECONNABORTED  The virtual circuit was terminated due to a time-out or other failure. The application should close the socket because it is no longer usable.  
    WSAECONNRESET  The virtual circuit was reset by the remote side executing a hard or abortive close. For UDP sockets, the remote host was unable to deliver a previously sent UDP datagram and responded with a "Port Unreachable" ICMP packet. The application should close the socket because it is no longer usable.  
    WSAEADDRNOTAVAIL  The remote address is not a valid address, for example, ADDR_ANY.  
    WSAEAFNOSUPPORT  Addresses in the specified family cannot be used with this socket.  
    WSAEDESTADDRREQ  A destination address is required.  
    WSAENETUNREACH  The network cannot be reached from this host at this time.  
    WSAETIMEDOUT  The connection has been dropped because of a network failure or because the system on the other end went down without notice.  Res
    This function is used to write outgoing data on a socket. For message-oriented sockets, care must be taken not to exceed the maximum packet size of the underlying subnets, which can be obtained by using getsockopt (Windows Sockets) to retrieve the value of socket option SO_MAX_MSG_SIZE. If the data is too long to pass atomically through the underlying protocol, the error WSAEMSGSIZE is returned and no data is transmitted. The to parameter can be any valid address in the socket's address family, including a broadcast or any multicast address. To send to a broadcast address, an application must have used setsockopt (Windows Sockets) with SO_BROADCAST enabled. Otherwise, sendto will fail with the error code WSAEACCES. For TCP/IP, an application can send to any multicast address.If the socket is unbound, unique values are assigned to the local association by the system and the socket is then ed as bound. An application can use getsockname (Windows Sockets) to determine the local socket name in this case.The successful completion of a sendto call does not indicate that the data was successfully delivered.The sendto function is typically used on a connectionless socket to send a datagram to a specific peer socket identified by the to parameter. Even if the connectionless socket has been previously connected to a specific address, the to parameter overrides the destination address for that particular datagram only. On a connection-oriented socket, the to and tolen parameters are ignored, making sendto equivalent to send.For the sockets Using IP (Version 4)To send a broadcast (on a SOCK_DGRAM only), the address in the to parameter should be constructed using the special IP address INADDR_BROADCAST (defined in Winsock2.h), together with the intended port number. It is generally inadvisable for a broadcast datagram to exceed the size at which fragmentation can occur, which implies that the data portion of the datagram (excluding headers) should not exceed 512 bytes.If no buffer space is available within the transport system to hold the data to be transmitted, sendto will block unless the socket has been placed in a nonblocking mode. On nonblocking, stream-oriented sockets, the number of bytes written can be between 1 and the requested length, depending on buffer availability on both the client and server systems. The select or WSAEventSelect function can be used to determine when it is possible to send more data.Calling sendto with a len of zero is permissible and will return zero as a valid value. For message-oriented sockets, a zero-length transport datagram is sent.The flags parameter can be used to influence the behavior of the function invocation beyond the options specified for the associated socket. The semantics of this function are determined by the socket options and the flags parameter. The following table shows the value that is used with the bitwise OR operator to construct the flags parameter.Value  Description  
    MSG_DONTROUTE  Specifies that the data should not be subject to routing. A Windows Sockets service provider can choose to ignore this flag.  Requirements
    OS Versions: Windows CE 1.0 and later.
    Header: Winsock2.h.
    Link Library: Ws2.lib.See Also
    getsockname (Windows Sockets) | getsockopt (Windows Sockets) | recv | recvfrom | select | send | setsockopt (Windows Sockets) | shutdown | socket (Windows Sockets) | WSAEventSelect | WSAGetLastError | WSAStartup
    -------------------------------------------------------------------------------- Last updated on Wednesday, August 13, 2003© 2003 Microsoft Corporation. All rights reserved.
      

  3.   

    就想楼上说的 你用的是UDP来发送的
      在Create()的时候你是怎么做的啊?  你可以去网上查一下 很多关于这方面的文章
    相关的原代码也不少的
      

  4.   

    回复人: bluephenix(侧行者) ( ) 信誉:96  2004-06-10 17:47:00  得分: 0  
     
     
       就想楼上说的 你用的是UDP来发送的
      在Create()的时候你是怎么做的啊?  你可以去网上查一下 很多关于这方面的文章
    相关的原代码也不少的
      
    ========================================================================= 
    ChatSocket.Create(userPort, SOCK_DGRAM)
    这个有问题么?
      

  5.   

    回复人: wenxy1(周末了,极速飙车) ( ) 信誉:100 
    ////////////////////////////////
    //你用是UDP协议
    SendTo("good", 8, atoi(::ServerPort), ::ServerIP) //ErrorSendTo("good"),4, /*nHostPort,*/, /*lpszHostAddress */);==================================================================
    还是不能解决
      

  6.   

    在SERVER端有没有调用Accept()函数呢,如果在SERVER端没有调用这个肯定是接不到的。
      

  7.   

    你是怎么做的,
    不会是COPY就编译吧?
      

  8.   

    晕死~~
    上面两位~~
    我虽说是在看别人的程序~~
    但是绝对COPY就编译哈
    Accept()是用在tcp的嘛我现在做的是异步传输,应该不需要Accept()SERVER是可以接受信息的,现在关键的是客户端没有办法发送啊~~
      

  9.   

    晕死~~
    上面两位~~
    我虽说是在看别人的程序~~
    但是绝对不是COPY就编译哈
    Accept()是用在tcp的嘛我现在做的是异步传输,应该不需要Accept()SERVER是可以接受信息的,现在关键的是客户端没有办法发送啊~~
      

  10.   

    代码贴完整一点呀,create了吗?port和ip对吗?port是你要发送到的对方的port,IP也是你要发送到的对方IP。
      

  11.   

    对的~
    因为我用了同样的方法做的服务器端都能正确运行
    并能收到包但是就是发不出去有哪位好心人,能帮我看看么?留下QQ或MSN我给您传过来