Hi i would just like to know how wud u display the client's IP address when the client connects to the server.

This is found on the servers side while waiting for a connect. now i just need to display the client's IP address in this while loop but how:/

while(1)
    {
        textcolor(15);
        cout << "WAITING FOR CONNECTION..." << endl;
        alen = sizeof(cad);
        if((sd2 = accept(sd, (struct sockaddr *)&cad, &alen)) < 0)
        {
            textcolor(12);
            fprintf(stderr, "ACCEPT FAILED\n");
            exit(1);
        }
        s.selectQuery(query1,header);//The select Query will write to the lostLog file
        //will read from lostLog and put all data into the all variable
        ifstream infile("lostLog.doc");
        x="";
        all="";
        while(getline(infile,x))
        {
            all+=x+"\n";
        }
        textcolor(10);
        cout << "A CLIENT HAS CONNECTED TO THE SERVER! " << endl;
        //create the buffer to send over the network
        char buffer[all.size()];
        sprintf(buffer, "%s ", all.c_str());
        send(sd2, buffer,strlen(buffer),0);
        closesocket(sd2);
    }

That structure, &cad, has a member: sin_addr. Use inet_ntoa() to convert it to a string and print it.

string ip = inet_ntoa(cad.sin_addr);
thx:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.