jmatt110 0 Newbie Poster

Hey all,

Trying to get a simple LAN based chat program going (required to use UDP and multicasting). I've tried both client/server and p2p implementations, yet I still can't seem to get it working.

The best I've gotten with client/server is that clients on pc1 and pc2 will connect to the server and can send data to the server and receive data back. However I couldn't get the server to send the data to all clients.

With p2p, I can get each client to send a message, which is then received by itself, not the other clients however.

Here is my current implementation of p2p (removed error checking for readability). Any help would be greatly appreciated.

char *dest = "224.1.2.3";
    unsigned short port = 31337;
    struct sockaddr_in m_castAddress, m_recAddress;
    SOCKET m_sendSocket, m_recSocket;
    fd_set m_checkSockets;
    struct timeval t;

    m_castAddress.sin_family = AF_INET;
    m_castAddress.sin_port = htons(port);
    m_castAddress.sin_addr.s_addr = inet_addr(dest);

    m_recAddress.sin_family = AF_INET;
    m_recAddress.sin_addr.s_addr = INADDR_ANY;
    m_recAddress.sin_port = htons(port);

    m_sendSocket = socket(AF_INET, SOCK_DGRAM, 0);

    m_recSocket = socket(AF_INET, SOCK_DGRAM, 0);

    if (bind (m_recSocket, (SOCKADDR*) &m_recAddress, sizeof

    ip_mreq mreq;
    mreq.imr_multiaddr.s_addr = inet_addr(dest);
    mreq.imr_interface.s_addr = INADDR_ANY;

    setsockopt(m_sendSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char *)&mreq, sizeof(mreq));
    setsockopt(m_recSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char *)&mreq, sizeof(mreq));

    DWORD dwBytesReturned = 0;
    BOOL bNewBehavior = FALSE;
    // disable an annoying feature of 2000 and XP which causes many problems
    WSAIoctl(m_sendSocket, SIO_UDP_CONNRESET, &bNewBehavior, sizeof(bNewBehavior), NULL, 0, &dwBytesReturned, NULL, NULL);

    int result;
    int length = sizeof(m_recAddress);
    while(1)
    {
        //Initialise m_checkSockets to look at only the receiving socket.
        m_checkSockets.fd_count = 1;
        m_checkSockets.fd_array[0] = m_recSocket;
        char buffer[1000];
        //Ensure the client does not spend any time waiting for messages to arive.
        t.tv_sec = 0;
        t.tv_usec = 0;

        int waiting = select(NULL, &m_checkSockets, NULL, NULL, &t);
        //If a message has been received
        if(waiting>0)
        {
            //Process message
            result = recvfrom(m_recSocket, buffer, 10000, 0, (SOCKADDR*) &m_recAddress, &length);

            std::cout << std::endl << "Packet from Address:" << inet_ntoa(m_recAddress.sin_addr) << "  m_port:" << m_recAddress.sin_port <<  "  Size:" << result << "  Data:" << std::endl << buffer << std::endl;

            sprintf(buffer,"ACK");
            result = sendto(m_recSocket, buffer, (int)strlen(buffer)+1, 0, (SOCKADDR*)&m_recAddress, sizeof(m_recAddress));
        }
        else
        {
            //Else, check to see if the user has input a message.
            int result;
            std::cin.getline(buffer, 1000);
            result = sendto(m_sendSocket, (char *)(buffer), sizeof(buffer), 0, (SOCKADDR*)&m_castAddress, sizeof(m_castAddress));
           
            int length = sizeof(m_recAddress);

            if(memcmp(buffer,"QUIT",4) == 0)
            {
                break;
            }
            Sleep(1000);
        }
    }