Leftovers, MMmmm...

Started by
2 comments, last by GameDev.net 17 years, 12 months ago
Ok, so I'm attempting to take my last look at buffer "leftovers" as a problem. I'm trying to deal with sending ip's over a network, dynamic ips. That means I need to be able to send an 11 character ip, 15 character ip, then 13 character ip. The problem I'm having is whenever i request to see my ip with my command "-ip"(doesn't really matter but whatever), the output is as such:
Quote:192.168.1.1 2.168.1.
there is apparently 19 characters(20 including '\0' character?), so what I'm thinking is that the client is requesting more data than I want it to, causing the server to send the extra "2.168.1." off the first ip. Here's what I have:
//server recv cmd
void Server::Recv_msg()
{
    // Receive buffer on character array recvbuf
    if ((bytes = recv(i, recvbuf, 10, 0)) <= 0)
    {
        // If no data received, remove client from userlist
        Remove_client();
    }else if(!strcmp(recvbuf, "-ip")){
        for(int h = 0; h < my_vec.size(); h++){
            tempuser2 = my_vec.at(h);
            if(tempuser2.name=="client")
            for(j = 0; j <= fdmax; j++)
            {
                // if socket j is in the set, send to that socket
                if (FD_ISSET(j, &master))
                {

                    // Send recvbuf to all users
                    send(j, tempuser2.ip.c_str(), strlen(tempuser2.ip.c_str()), 0);

                }
            }
        }
    }else{

        // If data was received, send to all users
        Send_msg(); // Doesnt matter to us though
    }
}
//client recv
void Client::RecvMessage()
{
    // sockfd: Socket in which data is received
    // recvbuf: Char in which data is received (as a string)
    // 256: Max bytes to receive from the server
    while (bytes = recv(sockfd,
                     recvbuf,
                     10,
                     0) == -1)
                     // Output error message if error occurs
                     { Msges.ErrorMsg("Error while receiving."); }

    myLog.open(logname.c_str(), ios::app);
    myLog << recvbuf;
    myLog.close();

    cout << recvbuf;
}
I know the problem is with the client receiving because he won't always receive 10 bytes of data. This is what I'm trying to figure out.
Hello?
Advertisement
Is this TCP? If so, you can't guarantee that messages will arrive in one recv() call; TCP is a streaming protocol so you need to make sure you've for the whole message.
The Forum FAQ deals with this question. You need to frame your messages, either using "length" fields, or using delimiters. You need to parse the data you get back to figure out what a well-formed packet it, and keep the rest for the next packet.
enum Bool { True, False, FileNotFound };
I'm trying to deal with sending ip's over a network, dynamic ips.

Static or dynamic, the IP you are getting is not representative of the machine's IP as seen by the network. It's only the IP that has been assigned to your machine by the nearest router. If you want to know what is your real IP on the net, check out "www.whatismyip.com". If you decide to send the port number also, routers between your local machine and your target are likely to remap the port number you think you are getting to another one.

This topic is closed to new replies.

Advertisement