简体   繁体   中英

how to display info in a listbox instead of edit box?

i want to create a client server program in mfc i found some really good source code from this website: http://www.softwareandfinance.com/Visual_CPP/TCP_Client_Server.html

ok can someone help me display the information in a listbox instead of the edit box?

here's the code to process the client:

static void f(void *p)
{
    CSocketTestServerDlg *pDlg = reinterpret_cast<CSocketTestServerDlg*>(p);
    pDlg->ProcessClientRequest();
}

void CSocketTestServerDlg::ProcessClientRequest()
{
    SOCKADDR_IN clientaddr;
    struct hostent *hostentry;
    int len = sizeof(clientaddr);
    SOCKET clientsocket = accept(m_serversocket, (sockaddr*)&clientaddr, &len);

    if(len == -1)
    {
        AfxMessageBox("Error accpeting the client socket");
    }
    else
    {
        char *p = inet_ntoa(clientaddr.sin_addr);
        int portno = ntohs(clientaddr.sin_port);
        // int inet_pton(int af, const char *restrict src, void *restrict dst);

        char rbuf[1024];
        recv(clientsocket, rbuf, 1024, 0);
        for(int i = 1024; i >= 1; i--)
        {
            if(rbuf[i] == '\n' && rbuf[i - 1] == '\r')
            {
                rbuf[i-1] = '\0';
                break;
            }
        }


        CString strRecvData;

        strRecvData.Format("%s\r\n%s %d\r\n%s\r\n\r\n", (const char*)CTime::GetCurrentTime().Format("%B %d, %Y %H:%M:%S"), p, portno, rbuf);
        m_recvData += strRecvData;
        m_bRefershData = true;
        strcat(rbuf, "\r\n");
        send(clientsocket, rbuf, 1024, 0);
        closesocket(clientsocket);
    }
}

so how can i just get the ip address from the client to display in a list box? i don't need all the other information

Well, you already have the IP as a string in p , don't you?

You could create a CString from it to avoid UNICODE problems. Then use CListBox::AddString to output your string:

char *p = inet_ntoa(clientaddr.sin_addr);

CString str(p);

//CListBox listbox;

listbox.AddString(str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM