Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 2099
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 31, 20222022-05-31T14:51:05+00:00 2022-05-31T14:51:05+00:00

c – MFC socket print out on the Listbox

  • 0

[ad_1]

I’m making a chat program with clients and servers using MFC and Winsock. The client socket is being managed by putting it in std::vector. There was a problem with the service when the client socket was accept.

I would like to receive the information of the client that is stored in the socket (vector) and print it out on the ListBox

Is there a way to extract and print out specific information in the vector? Also, I would like to print out the contents of the recv without using a repetitive sentence, what should I do?

It’s the server code

static UINT ServerFunc(LPVOID pVoid)
{
    CServerDlg *dlg = (CServerDlg *)AfxGetApp()->m_pMainWnd;

    CString slniPath;
    slniPath.Format(_T("./NetworkPath.ini"));
    TCHAR ServerPort[MAX_PATH];
    int ServerPort_Num;
    GetPrivateProfileString(_T("ServerInfo"), _T("Port"), _T(""), ServerPort, MAX_PATH, slniPath);
    ServerPort_Num = _ttoi(ServerPort);

    TCHAR ClientCount[MAX_PATH];
    int ClientCount_Num;
    GetPrivateProfileString(_T("ServerInfo"), _T("ClientCount"), _T(""), ClientCount, MAX_PATH, slniPath);
    ClientCount_Num = _ttoi(ClientCount);
    

    WSADATA wsaData;
    int retval = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (retval != 0)
    {
        AfxMessageBox("WSAStartup() Error\n");
        return 0;
    }

    //--SOCKET--
    dlg->server_sock = socket(AF_INET, SOCK_STREAM, 0); 
    if (dlg->server_sock == SOCKET_ERROR) 
    {
        AfxMessageBox("socket() Error\n");
        return 0;
    }
    dlg->server_addr = { 0 };
    dlg->server_addr.sin_family = AF_INET;
    dlg->server_addr.sin_port = htons(ServerPort_Num);
    dlg->server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    //bind
    retval = bind(dlg->server_sock, (SOCKADDR*)&dlg->server_addr, sizeof(SOCKADDR));

    if (retval == SOCKET_ERROR) 
    {
        AfxMessageBox("bind() Error\n");
        return 0;
    }
    listen(dlg->server_sock, ClientCount_Num); 

    AfxMessageBox("Server Start\n");

    //accept 
    while (1)
    {
#if 1
        dlg->client_addr = { 0 };
        dlg->client_addr.sin_family = AF_INET;
        dlg->client_addr.sin_port = htons(ServerPort_Num);
        dlg->client_addr.sin_addr.s_addr = htonl(INADDR_ANY);

        int size = sizeof(SOCKADDR_IN);
#endif
        dlg->client_sock = accept(dlg->server_sock, (SOCKADDR*)&dlg->client_addr, &size);
        if (dlg->client_sock == SOCKET_ERROR) 
        {
            AfxMessageBox("accept() Error");
            continue;
        }
        client_list.push_back(dlg->client_sock);

        CString ConnectInfo_str;
        CString ConnectInfo_str2;
        
        ConnectInfo_str.Format("IP : %s\n", inet_ntoa(dlg->client_addr.sin_addr)); 
        ConnectInfo_str2.Format("IP : %s Join \n", inet_ntoa(dlg->client_addr.sin_addr));
        dlg->m_ListClient.AddString(ConnectInfo_str);
        dlg->m_ListChat.AddString(ConnectInfo_str2);

        dlg->pThreadTcpServer_Data = AfxBeginThread(ServerRecvSendFunc, (void *)dlg->client_sock);
    }
    closesocket(dlg->server_sock);
    WSACleanup;

    return 0;

}

It’s the service code

#if 1
#define BUFSIZE 128
static UINT ServerRecvSendFunc(void *p)
{
    CServerDlg *dlg = (CServerDlg *)AfxGetApp()->m_pMainWnd;
    SOCKET socket = (SOCKET)p;


    char readbuffer[BUFSIZE + 1];
    BYTE readbuffer__[BUFSIZE + 1];
    int recvsize;

    BYTE SendBuffer_TEST[BUFSIZE + 1];

    while (1)
    {
        recvsize = recv(socket, readbuffer, BUFSIZE, 0);

        if (recvsize <= 0)
        {
            break;
        }
        readbuffer[recvsize] = '\0';

        CString strTemp;
        CString strMsg;

        char temp[60];
        memset(readbuffer__, 0x00, sizeof(readbuffer__));
        memcpy(&readbuffer__, &readbuffer[0], BUFSIZE);
        for (int i = 0; i < recvsize; i++)
        {
            strTemp.Format(_T("%c"),readbuffer[i]);
            strMsg += strTemp;
        }
        dlg->m_ListChat.AddString(strMsg);
        strMsg = "";
        strTemp = "";
#if 1
        //log
#endif
        //log
#if 1
        for (int i = 0; i < client_list.size(); i++)
        {
            if (client_list[i] == socket)
            {
                int sendsize = send(client_list[i], readbuffer, strlen(readbuffer), 0);
            }
        }
#endif
    }
#if 1
    
    CString DisConnectInfo_str;
    CString DisConnectInfo_str2;
    
    
    std::vector<SOCKET>::iterator iter = client_list.begin();
    for (int i = 0; i < client_list.size(); i++)
    {
        if (client_list[i] == socket) 
        {
            client_list.erase(iter); 
            break;
        }
        iter++;
    }
#endif  
    if (socket == NULL)
    {
    }
    closesocket(socket);
}
#endif

[ad_2]

  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 5252 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1098 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1047 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.