|
各位专家:
看看我的Wince5.0下的UDP客户端程序,哪里有问题,调试显示数据已经发送出去了,但是服务器没有收到数据,代码如下:
SOCKADDR_IN m_sinServer;
int ret=0;
WSADATA wsd;
ret=WSAStartup(MAKEWORD(1,1),&wsd);
int m_socket;
m_socket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if (m_socket == INVALID_SOCKET)
{
int iSocketError = WSAGetLastError();
return FALSE;
}
memset(&m_sinServer, 0, sizeof(SOCKADDR_IN));
m_sinServer.sin_family = AF_INET;
m_sinServer.sin_port = htons(9991);
CString m_remoteHost=TEXT("10.21.1.114");
//此处要将双字节转换成单字节
char ansiRemoteHost[255];
ZeroMemory(ansiRemoteHost,255);
WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,m_remoteHost,wcslen(m_remoteHost)
,ansiRemoteHost,wcslen(m_remoteHost),NULL,NULL);
m_sinServer.sin_addr.s_addr=inet_addr(ansiRemoteHost);
int nBytes = 0;
int nSendBytes=0;
char test[20]="this is test";
int len=20;
while (nSendBytes < len)
{
nBytes = sendto(m_socket,test+nSendBytes,len-nSendBytes,0,
(SOCKADDR *) &m_sinServer,sizeof(SOCKADDR_IN));
if (nBytes==SOCKET_ERROR )
{
int iErrorCode = WSAGetLastError();
//关闭socket
closesocket(m_socket);
return FALSE;
}
nSendBytes = nSendBytes + nBytes;
if (nSendBytes < len)
{
Sleep(1000);
}
}
closesocket(m_socket);
WSACleanup( );
|
|