首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Windows Mobile >

各位大侠,救啊基于连接的UDP客户端,能发却收不到服务器发回的ack!解决办法

2012-01-12 
各位大侠,急救啊!!基于连接的UDP客户端,能发却收不到服务器发回的ack!问题陈述:1. 我在windows mobile 6.0

各位大侠,急救啊!!基于连接的UDP客户端,能发却收不到服务器发回的ack!
问题陈述:
  1. 我在windows mobile 6.0 平台上做了个基于连接的UDP客服端,然后向PC上一个UDP服务器发信息,可以发信息到服务器上,但收不到服务器回发的ack.
  2. 我做的客服端是绑定了端口18587,但我在服务器的上用抓包工具抓到手机发过来的数据包时,端口号却不是18587,而且会变动!
  3. 有没有抓手机的UDP包的方法?
下面的源码:
[code=C/C++][/code]

// udp_client_connect.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <connmgr.h>
#include <connmgr_proxy.h>
#include <Winsock2.h> 
#include <stdio.h> 
#include <windows.h>
#include <iostream>
#pragma comment (lib,"ws2.lib") 
#pragma comment( lib, "cellcore.lib" )
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
//建立GPRS连接
GUID IID_DestNetInternet = {0x436ef144, 0xb4fb, 0x4863, 0xa0, 0x41, 0x8f, 0x90, 0x5a, 0x62, 0xc5, 0x72};
CONNMGR_CONNECTIONINFO pConnInfo;
memset(&pConnInfo, 0, sizeof(CONNMGR_CONNECTIONINFO));

pConnInfo.cbSize=sizeof(CONNMGR_CONNECTIONINFO);
pConnInfo.dwParams=CONNMGR_PARAM_GUIDDESTNET;
pConnInfo.dwFlags=CONNMGR_FLAG_PROXY_WAP | CONNMGR_FLAG_PROXY_HTTP | CONNMGR_FLAG_PROXY_SOCKS4 | CONNMGR_FLAG_PROXY_SOCKS5;
pConnInfo.dwPriority=CONNMGR_PRIORITY_USERINTERACTIVE;
pConnInfo.bExclusive=FALSE;
pConnInfo.bDisabled=FALSE;
// pConnInfo.hWnd=m_hWnd;
pConnInfo.guidDestNet= IID_DestNetInternet;

DWORD pdwStatus=0;
HANDLE m_wapHandle;
HRESULT hr=ConnMgrEstablishConnectionSync(&pConnInfo, &m_wapHandle, 10000, &pdwStatus);

printf("ConnMgrEstablishConnectionSync = %d\n",hr);



//1.启动SOCKET库,版本为1.0

WORD wVersionRequested;  
WSADATA wsaData;

int err;
wVersionRequested=MAKEWORD(1,1);
err=WSAStartup(wVersionRequested,&wsaData);
if(err!=0)
{
return 0;
}
if(LOBYTE(wsaData.wVersion)!=1||HIBYTE(wsaData.wVersion)!=1)
{
WSACleanup();
return 0;
}



//创建socket
SOCKET sockClient=socket(AF_INET,SOCK_DGRAM,0); //AF_INET: internetwork: UDP, TCP, etc; SOCK_DGRAM说明是UDP类型
SOCKADDR_IN addrSrv,addrLocal;

//用于循环控制
int flag = 1;

while(flag)
{
char serv_addr[100];
cout << "please input the severaddr : "<<endl;
cin >> serv_addr;
cout<<endl;


addrSrv.sin_addr.S_un.S_addr =inet_addr(serv_addr); //inet_addr()将IP地址从点数格式转换成无符号长整型
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(18586);

addrLocal.sin_family=AF_INET;//bind to a local port
addrLocal.sin_addr.s_addr=htonl(INADDR_ANY);
addrLocal.sin_port=htons(18587);

//绑定端口
if(bind(sockClient,(struct sockaddr*)&addrLocal,sizeof(struct sockaddr))==-1)
{
printf("error!");
}

//建立连接
if(connect(sockClient,(struct sockaddr*)&addrSrv,sizeof(addrSrv))==-1)
{
printf("error!");
}


struct TRAN_PACKET
{
char buffer[60]; 
};
struct TRAN_PACKET tran_packet = {{0x00, 0x3c, 0x00 ,0x08,0xDB, 0x22,0x4e, 0x4f, 0x03, 0x00, 0x80, 0x00, 0x41, 0x32, 0x31, 0x33, 0x30, 0x36, 0x38, 0x39, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x00, 0x33, 0x35, 0x36, 0x37, 0x38, 0x36, 0x30, 0x31, 0x30, 0x32, 0x39, 0x37, 0x37, 0x32, 0x33, 0x00, 0x34, 0x36, 0x30, 0x30, 0x32, 0x31, 0x31, 0x36, 0x39, 0x31, 0x31, 0x38, 0x39, 0x38, 0x31}};

//发送数据给服务端
int send_data_len = send(sockClient,(char*) &tran_packet,(int)sizeof(struct TRAN_PACKET),0);

//输出发送的数据
cout << "向服务器端发送数据如下:" <<endl;
for(int i=0;i<send_data_len;i++)
{
printf("0x%.2X ",tran_packet.buffer[i]);
}
cout<<endl <<"send_end"<<endl;




char ser_ackbuffer[100];

/* 接收server端返回的字符串*/ //recvfrom()函数返回值为接收到的字节长度
int recv_data_len = recv(sockClient,ser_ackbuffer,sizeof(ser_ackbuffer),0);

//输出从服务器端收到的数据
printf("收到的字节数为: %d B\n 内容为:\n",recv_data_len );
for(int i=0;i<recv_data_len ;i++)
{
printf("0x%.2X ",ser_ackbuffer[i]);

}

cout<<endl<<"coutinue? Y/N"<<endl;
char flag_char;
cin >> flag_char;
if(flag_char == 'Y'|| flag_char == 'y')
{
flag = 1;
}
else if(flag_char == 'N'|| flag_char == 'n' )
{
flag = 0;
}
else
{
cout<<"input error!! input again"<<endl;
cin >> flag_char;
}
}
closesocket(sockClient);
WSACleanup();

//在所有socket连接结束后需要断开连接
if (m_wapHandle != NULL)
{
LONG lCache = 0;
ConnMgrReleaseConnection(m_wapHandle, lCache);
m_wapHandle = NULL;
}
return 0;
}
[code=C/C++][/code]

[解决办法]
一般来说用模拟器做,然后抓PC上的包。

热点排行