开发者

Socket connect() always succeeds (TCP over ActiveSync)

开发者 https://www.devze.com 2022-12-21 21:40 出处:网络
I\'m using TCP/IP over ActiveSync to connect from Windows CE device to Windows XP desktop. The WinSock connect() function always succeeds, no matter whether desktop server application is actually runn

I'm using TCP/IP over ActiveSync to connect from Windows CE device to Windows XP desktop. The WinSock connect() function always succeeds, no matter whether desktop server application is actually running.

The following simplified code demonstrates this issue:

#include "stdafx.h"
#include <Winsock2.h>

int _tmain(int argc, _TCHAR* argv[])
{
    const int Port = 5555;
    const char * HostName = "ppp_peer";  

    WSADATA wsadata;
    if (WSAStartup(MAKEWORD(1, 1), &wsadata) != 0)
        return 1;

    struct hostent * hp = gethostbyname(HostName);
    if (hp == NULL)
        return 1;

    struct sockaddr_in sockaddr;
    memset(&sockaddr, 0, sizeof(sockaddr));
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
    sockaddr.sin_port = htons(Port);    

    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == SOCKET_ERROR)
        return 1;

    int result = connect(sock, (struct sockaddr*)&sockaddr, sizeof(sockaddr));
    // result always 0 (success) here

    closesocket(sock);

    return 0;
} 

Is this a bug? If not, what is a correct way to determine that the server is actually online? Only to try to use the e开发者_如何学JAVAstablished connection (recv/send data)?

Device: Windows CE 5.0, WinSock 2.2; Desktop: Windows XP, SP3, ActiveSync 4.5.


From what IIRC, there is a bug in the ActiveSync in that the WM 5.0 thinks its still connected up to the ActiveSync server on the Windows desktop pc, see this answer here on SO which might provide a clue and/or insight into this and could explain why the socket connect always succeed...


So, I did not find the way to check if this is 'real' connection, other than to ignore this issue and try to use this connection. If it is not 'real', the communication will fail.

0

精彩评论

暂无评论...
验证码 换一张
取 消