0

我正在做一个套接字程序,通过发送十六进制消息与视频服务器控件进行通信。我尝试了很多次弄清楚发生了什么,但我还没有找到错误。

这是我完成的代码:

void UsingWinSock()
{
    u_short port;
    int ret;
    struct sockaddr_in my_addr;
    int lenAddrs;
    CString remoteAddress;

    b1 = 192;
    b2 = 168;
    b3 = 11;
    b4 = 2;

    remoteAddress.Format("%u.%u.%u.%u", b1, b2, b3, b4);

    port = (u_short)(portNumber);

    // remote address
    memset(&remote_addr, 0, sizeof(remote_addr));
    remote_addr.sin_family = PF_INET;
    remote_addr.sin_port = htons(port);
    inet_pton(AF_INET, remoteAddress, (&remote_addr.sin_addr)); 

    // socket 
    sckSloMo = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (sckSloMo == INVALID_SOCKET)
    {
        AfxMessageBox("sckSloMo is invalid!!");
        return;
    }

    if (sckSloMo != SOCKET_ERROR)
    {
        ret = connect(sckSloMo, (const sockaddr*) &remote_addr, sizeof(remote_addr));

        if (ret != SOCKET_ERROR)
        {
            m_status = "Connected!!";
            SetDlgItemText(IDC_STATIC_STATUS, m_status);
        }
        else
        {
            m_status = "connect error!!!";
            SetDlgItemText(IDC_STATIC_STATUS, m_status);
            OnBnClickedButtonCancel();
        }
    }
    else if (ret == SOCKET_ERROR)
    {
        AfxMessageBox("socket not Ok");
        return;
    }

}

void StartSendingMessages()
{
    int retorno;
    const int bufLen = 7;
    CString TextRetorno;
    CString messageSend;

    HEX_bufferMessage = new CHAR[bufLen]; // alocando espaço

    // specifies the led state
    HEX_bufferMessage[0] = 0xF0;
    HEX_bufferMessage[1] = 0x15;
    HEX_bufferMessage[2] = 0x31;
    HEX_bufferMessage[3] = 0x04;
    HEX_bufferMessage[4] = 0x02; // green
    HEX_bufferMessage[5] = 0x0E; // C1
    HEX_bufferMessage[6] = 0xF7;

    retorno = send(sckSloMo, HEX_bufferMessage, bufLen, 0); 

    if (retorno == SOCKET_ERROR || retorno < 0 || retorno == INVALID_SOCKET)
    {
        AfxMessageBox("Error Send!!");
        return;
    }
    else if(retorno != 0)
    {
        AfxMessageBox("Send() OK !!");
    }
}

没有引发错误,但视频服务器控制器不执行任何操作。任何人都知道我的程序发生了什么?

4

1 回答 1

0

尝试设置套接字选项“TCP_NODELAY”。您的消息太小,可能会延迟一段时间。

于 2013-11-04T20:40:16.503 回答