2 cangkutou cangkutou 于 2014.05.20 14:15 提问

wince socket通信 板子掉线问题

测试程序组成:
上位机程序 vs2010 C#开发
wince程序 vs2008 C++开发
板子 tiny6410
功能:
wince每隔100ms向上位机发一次数据包,一包1000个字节,上位机接收数据包,不做任何处理
现象:
过一段时间wince就掉线了,网口灯正常,黄灯亮,绿灯闪烁,wince ping不通网关

下面是C++代码,求大神帮助

#include "stdafx.h"
#include
#include

//#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"winsock.lib")

#define HEADER_LENGTH 8

#define SERVER_IP "192.168.1.132"
#define SERVER_PORT 9100

SOCKET g_sSocketClient = INVALID_SOCKET;

bool SocketInit();
void SocketUninit();
bool ConnectServer(const char* ip, int port);
int RecvToSend(char* pSendBuffer);

int _tmain(int argc, _TCHAR* argv[])
{
SocketInit();

printf("%s\n", "> Waiting for the connection...");

char* pSendBuffer = new char[HEADER_LENGTH+1000+128];
    memset(pSendBuffer, 0, sizeof(pSendBuffer));
    //pSendBuffer = (char*)0xFFFF8813;
    *pSendBuffer =  0xFF;
    *(pSendBuffer+1) = 0xFF;
    *(pSendBuffer+2) = 0xFF;
    *(pSendBuffer+3) = 0xFF;
    *(pSendBuffer+4) = 0xE8;
    *(pSendBuffer+5) = 0x03;
    *(pSendBuffer+6) = 0x00;
    *(pSendBuffer+7) = 0x00;

//等待连接
ConnectPart: while (true)
    {
        //接收输入 ip 和 端口
        if (ConnectServer(SERVER_IP, SERVER_PORT))break;
    }

while (true)
{
    if (RecvToSend(pSendBuffer) == 0)
    {
        goto ConnectPart;
    }       
}

delete [] pSendBuffer;
return 0;

}

bool SocketInit()
{
WORD wVersionRequested;
WSADATA wsaData;

//WinSock初始化
wVersionRequested = MAKEWORD(1, 1); //希望使用的WinSock DLL的版本
if(WSAStartup(wVersionRequested, &wsaData) != 0)
{
    printf("> WSAStartup() failed!\n");
    return false;
}

//确认WinSock DLL支持版本1.1
if(LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
{
    WSACleanup();
    printf("> Invalid WinSock version!\n");
    return false;
}

return true;

}

void SocketUninit()
{
if (g_sSocketClient != INVALID_SOCKET)
{
closesocket(g_sSocketClient);
g_sSocketClient = INVALID_SOCKET;
}

WSACleanup();

}

bool ConnectServer(const char* ip, int port)
{
if((g_sSocketClient = socket(AF_INET, SOCK_STREAM,0)) < 0)
{
printf("> Init socket error!");
return false;
}

struct sockaddr_in addr;
memset((void*)&addr, 0, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip);

if(connect(g_sSocketClient, (struct sockaddr*)&addr,sizeof(addr)) <0 )
{
    closesocket(g_sSocketClient);
    g_sSocketClient = INVALID_SOCKET;
    return false;
}
else
{
    const char chOpt=1;
    int nErr = setsockopt(g_sSocketClient, IPPROTO_TCP, TCP_NODELAY, &chOpt, sizeof(char));
    printf("%s\n", "> The connection is successful!");
    return true;
}

return false;

}

int RecvToSend(char* pSendBuffer)
{
if (g_sSocketClient != INVALID_SOCKET)
{
if (send(g_sSocketClient, pSendBuffer, HEADER_LENGTH+1000, 0) == SOCKET_ERROR)
{
//delete [] pSendBuffer;
printf("%s\n", "> Send the data failed!");
return -1;
}
else
{
//delete [] pSendBuffer;
printf("%s\n", "> Send the data success!");
}

}
Sleep(100);
return -1;

}

Csdn user default icon
上传中...
上传图片
插入图片