c – I don’t know why UDP winnsock Chat Program returns -1 value from recvfrom and sendto

Currently, we are producing UDP chat programs using MFC and C. I’m working on both bind because I have to send incoming messages from the server and client An error occurs when recvfrom and sendto return -1. I don’t know what the problem is I searched it and did what I did, but it’s not working

Why does sendto and recvfrom return -1?

I don’t know what the problem is.

server Code

static UINT UdpServerFunc(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);
    //------------------------------------------------------

    //Startup Winsock 
    WSADATA wsaData; 
    int retval = WSAStartup(MAKEWORD(2, 2), &wsaData); 
    if (retval != 0) 
    {
        AfxMessageBox("WSAStartup() Errorn");
        return 0;
    }

    //SOCKET
    dlg->server_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);//IPPROTO_UDP or PPROTO_HOPOPTS
    if (dlg->server_sock == SOCKET_ERROR)
    {
        AfxMessageBox("Socket() Errorn");
        return 0;
    }

    ZeroMemory(&dlg->server_addr, sizeof(dlg-> server_addr));
    dlg->server_addr.sin_family = AF_INET;
    dlg->server_addr.sin_port = htons(ServerPort_Num); //30112
    dlg->server_addr.sin_addr.s_addr = htonl(INADDR_ANY); 
    
    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);
    

    retval = bind(dlg->server_sock, (SOCKADDR*)&dlg->server_addr, sizeof(dlg->server_addr));

    AfxMessageBox("server start");
    if (retval == SOCKET_ERROR) {
        AfxMessageBox("bind() ERRORn");
        return -1;
    }
    //Data Communication
    int addrlength;
    char buf[BUFFER_SIZE];
    int recv_size;
    while (1)
    {       
        //recvfrom()
        addrlength = sizeof(dlg->client_addr);  
        recv_size = recvfrom(dlg->server_sock, reinterpret_cast<char*>(buf), BUFFER_SIZE, 0, (SOCKADDR*)&dlg->client_addr, &addrlength);
        if (recv_size == SOCKET_ERROR)
        {
            AfxMessageBox("recvfrom() Error");
            break;
        }
        //Data print
        buf[recv_size] = '';
        CString strMsg;
        strMsg = CString(buf, recv_size);
        dlg->m_ListChat.AddString(strMsg);
        strMsg = "";

    }
    closesocket(dlg->server_sock);
    WSACleanup;
}

Server Sendto Code

void CServerDlg::OnBnClickedButton2()
{
    CServerDlg *dlg = (CServerDlg *)AfxGetApp()->m_pMainWnd;
    
    CString strText = _T("");
    char Text[BUFFER_SIZE];
    dlg->m_edit_chat.GetWindowText(strText);
    strcpy(Text, strText);
    int send_size = sendto(dlg->server_sock, Text, strlen(Text), 0, (sockaddr*)&dlg->client_addr, sizeof(dlg->client_addr));
    if (send_size == SOCKET_ERROR)
    {
        AfxMessageBox("sendto() Error");
    }
    else
    {
        dlg->m_ListChat.AddString(strText);
    }

}

Client Code

static UINT ClientUdpThreadFunc(LPVOID pVOID) {

    CClientTestDlg *dlg = (CClientTestDlg *)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); //30112

    TCHAR ServerIp[MAX_PATH];
    int ServerIp_Num;
    GetPrivateProfileString(_T("ServerInfo"), _T("IP"), _T(""), ServerIp, MAX_PATH, slniPath);
    ServerIp_Num = _ttoi(ServerIp); //127.0.0.1
    //--------------------------------------------------

    //StartUp Winsock
    WSADATA wsaData;
    int retval = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (retval != 0)
    {
        AfxMessageBox("WSAStartup() Errorn");
        return 0;
    }

    //socket
    dlg->clnt_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 
    if (dlg->clnt_sock == SOCKET_ERROR) {
        AfxMessageBox(_T("socket() Error"));
        return 0;
    }

    ZeroMemory(&dlg->ServerAddr, sizeof(dlg->ServerAddr));
    dlg->ServerAddr.sin_family = AF_INET; 
    dlg->ServerAddr.sin_port = htons(ServerPort_Num); 
    dlg->ServerAddr.sin_addr.s_addr = inet_addr(SERVERIP); 
    /*
    dlg->FromServer.sin_family = AF_INET;
    dlg->FromServer.sin_port = htons(ServerPort_Num);
    dlg->FromServer.sin_addr.s_addr = inet_addr(SERVERIP);
    */
    retval = bind(dlg->clnt_sock, (SOCKADDR*)&dlg->ServerAddr, sizeof(dlg->ServerAddr));
    
    if (retval == SOCKET_ERROR) {
        AfxMessageBox("bind() ERRORn");
        return -1;
    }
    
    //Data Communication
    CString strMsg;
    int addrlength;
    char buf[BUF_SIZE];
    int recv_size;

    while (1) {
        addrlength = sizeof(dlg->FromServer);
        recv_size = recvfrom(dlg->clnt_sock, reinterpret_cast<char*>(buf), BUF_SIZE, 0, (sockaddr*)&dlg->FromServer, &addrlength);
        if (recv_size == SOCKET_ERROR)
        {
            AfxMessageBox("recvfrom() ERROR");
            break;
        }
        else 
        {
            //Data print
            buf[recv_size] = '';
            strMsg = CString(buf, recv_size);
            dlg->m_listChat.AddString(strMsg);
            strMsg = "";
        }
    

    }       
    
    closesocket(dlg->clnt_sock);
    WSACleanup;
}

Client Sendto Code

void CClientTestDlg::OnBnClickedButtonSend()
{

    CClientTestDlg *dlg = (CClientTestDlg *)AfxGetApp()->m_pMainWnd;
    
    CString UdpStrText = _T("");
    char Text[BUF_SIZE];
    m_editChat.GetWindowText(UdpStrText);
    strcpy(Text, UdpStrText);
    int UdpRtn;
    UdpRtn = sendto(dlg->clnt_sock, Text, strlen(Text), 0, (SOCKADDR*)&dlg->ServerAddr, sizeof(dlg->ServerAddr));
    if (UdpRtn == SOCKET_ERROR)
    {
        AfxMessageBox("sendto() ERRORn %s");
        
    }
    closesocket(dlg->clnt_sock);
    
}

Leave a Comment