如何从client-web发送消息到服务器-MFC-app?

时间:2014-03-22 16:10:38

标签: websocket webclient

我的任务是创建一个客户端 - 网络与server-mfc-app的通信。这意味着我不允许更改server-mfc-app代码,但必须让web可以与该server-mfc-app对话。 server-mfc-app使用套接字,可以与client-mfc-app进行通信。

我创建了一个client-web可以成功连接到server-mfc-app(server-mfc-app接受连接),但是当client-web向server-mfc-app发送消息时出现问题:server-MFC-应用程序知道客户端Web发送消息,但它可以通过函数void CChatServerDlg :: ReceiveData(SOCKET hSocket)获取消息(我运行调试模式server-mfc-app来监视所有功能)。

Server-MFC-app代码:

    ON_MESSAGE(MSG_ASYNC, OnAsyncSelect)
    LRESULT CChatServerDlg::OnAsyncSelect(WPARAM wParam, LPARAM lParam)
    {
        if (WSAGETSELECTERROR(lParam) != 0)
        {
        }
        else
        {
            switch(WSAGETSELECTEVENT(lParam))
            {
                case FD_READ:
                    ReceiveData(wParam);
                    break;
                case FD_ACCEPT:
                    {
                        CUserSocket *pClient = AcceptConnection();
                        if(pClient)
                        {
                            CString sReport;
                            sReport.Format(_T("Có kết nối từ IP: %s , Port: %d"),
                            pClient->GetRemoteIpAddr(),pClient->GetRemotePort());
                            m_lbStatus.AddString(sReport);
                            SendData(pClient->GetSocket(),_T("Server sẵn sàng"));
                            m_groupNewUsers.AddUser(pClient);
                        }
                    }
                    break;
                case FD_CLOSE:
                    CUserGroup *gr = m_groupList.FindGroup(wParam);
                    if (gr!=NULL)
                        gr->RemoveUser(wParam);
                    closesocket(wParam);
                    OnLbnSelchangeGrouplist();
                    m_lbStatus.AddString(_T("Đã đóng kết nối"));
                    break;
            }
        }
        return 0L;
    }
    void CChatServerDlg::ReceiveData(SOCKET hSocket)
    {
        char szBuf[MAX_LEN];
        int nByteRe = recv(hSocket, szBuf, MAX_LEN, 0);//always successful 
        // with client-mfc-app, and always unsuccessful with client-web below.
        if (nByteRe<=0) 
            return;
        wchar_t wch[MAX_LEN];
        CString s = _T("");
        MultiByteToWideChar(CP_UTF8,0,szBuf,nByteRe,wch,nByteRe/2);
        wch[nByteRe/2] = '\0';
        s += wch;
        SolveRequest(hSocket,s);
    }

客户端网络代码:

<?php
        error_reporting(E_ALL);
        $port = 2012;
        $address = "127.0.0.1";
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if ($socket === false) {
            echo "socket_create() failed: reason: "
            .socket_strerror(socket_last_error())."<br/>";
        }
        $result = socket_connect($socket, $address, $port);
        if ($result === false) {
                echo "socket_connect() failed.\nReason: ($result) "
            .socket_strerror(socket_last_error($socket)) ."<br/>";
        }else{
            echo "Connecting successful."."<br/>";
        }
        $msg = "ULIST";
            $len = strlen($msg);
        $flag=0;
        socket_sendto($socket, $msg, $len, $flag, $address, $port);
        //socket_close($socket);
?>

1 个答案:

答案 0 :(得分:-1)

请尝试使用socket_write($socket, $msg, $len );代替socket_sendto连接套接字