Sockets 即使未启用WIFI且服务器只能在无线网络中访问-Windows Mobile 6.5-C/C++;
我编写了一个小型C/C++Windows Mobile 6.5客户端应用程序,该应用程序正在连接到服务器,并向该服务器发送一些数据。服务器位于我的内部无线网络中,无法在外部访问 我的怪异行为: 1) 即使未在我的移动设备上启动WIFI,客户端应用程序的connect()也会返回成功(!=SOCKET_ERROR),但情况并非如此。b/c只有在无线网络中才能访问服务器 2) 如果我的移动设备上未启动WIFI,如果connect()和send()之间存在睡眠(1000),则send()会使用WSAECONNRESET失败,但如果connect()和send()之间没有睡眠(),则send()会成功!(只有在执行read()时,我才最终得到WSAECONNRESET错误) 有人能告诉我为什么我会有这种行为吗。非常可怕的是,在无法实际访问服务器的情况下,我仍然成功地完成了connect()和send()( 根据要求,以下是示例代码:Sockets 即使未启用WIFI且服务器只能在无线网络中访问-Windows Mobile 6.5-C/C++;,sockets,windows-mobile,wifi,connect,send,Sockets,Windows Mobile,Wifi,Connect,Send,我编写了一个小型C/C++Windows Mobile 6.5客户端应用程序,该应用程序正在连接到服务器,并向该服务器发送一些数据。服务器位于我的内部无线网络中,无法在外部访问 我的怪异行为: 1) 即使未在我的移动设备上启动WIFI,客户端应用程序的connect()也会返回成功(!=SOCKET_ERROR),但情况并非如此。b/c只有在无线网络中才能访问服务器 2) 如果我的移动设备上未启动WIFI,如果connect()和send()之间存在睡眠(1000),则send()会使用WSAE
#include <windows.h>
#include <Winsock2.h>
#include "dbgview.h"
# define FxMemZero(buf,len) RtlZeroMemory ((VOID*)(buf),(SIZE_T)(len))
# define FxMemCopy(dst,src,len) RtlCopyMemory ((VOID*)(dst),(CONST VOID*)(src),(SIZE_T)(len))
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
SOCKET proxy_connection;
WSADATA wsadata;
if( 0 != WSAStartup (MAKEWORD(1, 1), &wsadata))
return -1;
proxy_connection = WSASocket (AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
if(proxy_connection == INVALID_SOCKET) {
// error creating the socket
DbgViewTraceError((L"main", L"error creating socket."));
return -1;
}
// try to connect
UINT proxy_ip_ = 0x00000000;
CHAR* proxy_0_ = "192.168.1.105";
UINT proxy_port = 3100;
// get the proxy ip
{
struct hostent *he_;
if((he_ = gethostbyname(proxy_0_)) == NULL) {
DbgViewTraceWarning((L"main", L"error %d resolving hostname %hs", WSAGetLastError(), proxy_0_));
return -1;
}
FxMemCopy((PBYTE)&proxy_ip_, (PBYTE)he_->h_addr, he_->h_length);
}
// prepare the connection data
sockaddr_in saddr_;
FxMemZero(&saddr_,sizeof(sockaddr_in));
saddr_.sin_family = AF_INET;
saddr_.sin_addr.S_un.S_addr = proxy_ip_;// address
saddr_.sin_port = htons((USHORT)proxy_port);
// do the conection
if(SOCKET_ERROR == connect(proxy_connection, (SOCKADDR*) &saddr_, sizeof(saddr_))) {
// error connecting to the proxy
DbgViewTraceWarning(( L"main", L"error %d connecting to %hs:%d", WSAGetLastError(), proxy_0_, proxy_port));
closesocket(proxy_connection);
proxy_connection = INVALID_SOCKET;
return -1;
}
DbgViewTraceInfo(( L"main", L"SUCCESS. connected to %hs:%d.", proxy_0_, proxy_port));
CHAR* buffer_ = "Momo";
UINT count_ = strlen(buffer_);
DWORD total_ = 0;
DWORD sent_ = 0;
while(total_ < count_) {
// ISSUE: IF the WIFI is not started on the mobile, the connect returns success AND the send() returns success, even though with putty
// on the mobile, a telnet on 192.168.1.105:3100 will fail with: "Network error: Connection reset by peer"
// IF I add a long-enough Sleep() between the connect() and the send(), the send() will fail with: WSAECONNRESET
//Sleep(5000);
if(SOCKET_ERROR == (sent_ = send(proxy_connection, (const char*)buffer_ + total_, count_ - total_, 0))) {
// error sending data to the socket
DbgViewTraceError((L"main", L"error %d sending data to proxy", WSAGetLastError()));
return -1;
}
total_ += sent_;
}
DbgViewTraceInfo((L"main", L"send() SUCCESS"));
return 0;
}
#包括
#包括
#包括“dbgview.h”
#定义FxMemZero(buf,len)RtlZeroMemory((VOID*)(buf),(SIZE_T)(len))
#定义FxMemCopy(dst,src,len)RtlCopyMemory((VOID*)(dst),(CONST VOID*)(src),(SIZE_T)(len))
int WINAPI WinMain(HINSTANCE HINSTANCE,
HINSTANCE HPPrevenstance,
LPTSTR lpCmdLine,
国际展览(nCmdShow)
{
套接字代理连接;
WSADATA WSADATA;
如果(0!=WSAStartup(MAKEWORD(1,1),&wsadata))
返回-1;
代理连接=WSASocket(自动链接、SOCK流、IPPROTO\u TCP、NULL、0、0);
if(代理\u连接==无效的\u套接字){
//创建套接字时出错
DbgViewTraceError((L“main”,L“创建套接字时出错”);
返回-1;
}
//尝试连接
UINT-proxy\u-ip\u=0x00000000;
CHAR*proxy_0=“192.168.1.105”;
UINT proxy_port=3100;
//获取代理ip
{
结构宿主*他;
如果((他=gethostbyname(代理0))==NULL){
DbgViewTraceWarning((L“main”,L“error%d解析主机名%hs”,WSAGetLastError(),proxy_0));
返回-1;
}
FxMemCopy((PBYTE)和proxy_ip_,(PBYTE)he_->h_addr,he_->h_length);
}
//准备连接数据
saddr_中的sockaddr_;
fxmenzero(&saddr_,sizeof(sockaddr_in));
saddr_u.sin_family=AF_INET;
saddr\u.sin\u addr.S\u un.S\u addr=proxy\u ip;//地址
saddr_u.sin_port=htons((USHORT)代理_port);
//做连接
if(SOCKET_ERROR==connect(proxy_connection,(SOCKADDR*)和saddr_,sizeof(saddr_))){
//连接到代理时出错
DbgViewTraceWarning((L“main”,L“连接到%hs:%d的错误%d”,WSAGetLastError(),代理0,代理端口));
closesocket(代理连接);
代理\u连接=无效的\u套接字;
返回-1;
}
DbgViewTraceInfo((L“main”,L“SUCCESS.connected to%hs:%d.”,proxy_0_,proxy_port));
CHAR*buffer=“Momo”;
UINT计数=strlen(缓冲区);
德沃德总数=0;
德沃德发送=0;
while(总计<计数){
//问题:如果未在手机上启动WIFI,则connect返回成功,send()返回成功,即使使用putty
//在移动设备上,192.168.1.105:3100上的telnet将失败,并显示:“网络错误:对等方重置连接”
//如果在connect()和send()之间添加足够长的Sleep(),send()将失败:WSAECONNRESET
//睡眠(5000);
如果(套接字错误==(发送=发送(代理连接,(常量字符*)缓冲区+总数,计数-总数,0))){
//向套接字发送数据时出错
DbgViewTraceError((L“main”,L“将数据发送到代理的错误%d”,WSAGetLastError());
返回-1;
}
总数u+=已发送u;
}
DbgViewTraceInfo((L“main”,L“send()SUCCESS”);
返回0;
}
结果是:
1) 不睡觉():
主[信息]成功。已连接到192.168.1.105:3100
main[INFO]发送()成功
2) 带Sleep():
主[信息]成功。已连接到192.168.1.105:3100
主[错误]错误10054向代理发送数据
因此,问题是:
1) 为什么connect()会成功?我怎样才能确定两者之间有真正的联系
2) 为什么send()会成功
3) 为什么在connect()和send()之间使用Sleep()时,行为会有所不同
问题似乎是ActiveSync。如果ActiveSync正在运行,我将获得上述行为(connect()和send()报告成功,即使它们没有)。如果ActiveSync未运行,gethostbyname()将失败,原因是:
- WSAENETDOWN->如果禁用WIFI
- WSAHOST\u未找到->如果启用了WIFI
MeCoco看来你至少误用了。尝试使用更现代的API进行地址转换—Windows有—看看这是否解决了问题。您能发布一些实际的代码吗?