views:

99

answers:

2

I am learning how to network program using c/c++ and I have created a server(TCP) that is suppose to respond in specific ways to messages from a client in order to do this I created a class that the server class passes the message to and returns a string to report back to the client.

Here is my problem sometimes it reports the correct string back other times if just repeats what I sent to the message handler. Which no where in the code do I have it return what was passed in. So I am wondering am I handling getting the message correctly?

Secondly, I am unsure of how to keep a connection open in a while loop to continually pass messages back and forth. You can see how I did it in the code below but I am pretty sure this is incorrect, any help on this would be great. Thanks!

if (!fork())
    { // this is the child process
        close(sockfd); // child doesn't need the listener

        while ((numbytes = recv(new_fd, buf, MAXDATASIZE-1, 0)) > 0)
        {
            //numbytes = recv(new_fd, buf, MAXDATASIZE-1, 0);

            buf[numbytes-1] = '\0';
            const char* temp = ash.handleMessage(buf).c_str();
            int size_of_temp = ash.handleMessage(buf).length();
            send(new_fd, temp, size_of_temp, 0);

            //send(new_fd, temp, size_of_temp+1, 0);
        }
    }//end if

Please excuse my ghetto code

Handles the message

Class Method handler uses

+1  A: 

If your learning about sockets you should also know that you can't assume that what you send() as a "complete message", will be delivered as a complete message.

If you send() some big data from your client you might need to use multiple recv() on the server (or vice versa) to read it all. This is a big difference of how files usually work...

If you'r designing your own protocol you can opt to also send the messages length, like [LEN][message]. An easy example would be if the strings you send are limited to 256 bytes you can start with send()ing a short representing the strings length,

Or easier, decide that you use line-feeds (newline - \n) to terminate messages. The the protocol would look like

"msg1\nmsg2\n"

then you would have to recv(), and append the data, until you get a newline. This is all I can muster right now, there are a lot of great examples on the internet, but I would recommend getting the source of some "real" program and look at how it handles its network.

Sven Almgren
also, don't assume send() can send your entire message, you have to call send() in a loop until all your data has been sent...
Sven Almgren
Yup, my protocol is very short and I am just testing it right now. If you wouldn't mind sending me some links of good code examples that would be great. I can only find echo server/client examples which don't really help all that much.
Pieces
It works the other way too, if you send 10 messages using 10 send() call, you might read 9 and a half of them with one recv() call.
nos
+1  A: 

You are calling handleMessage twice. You didn't post the code, but it looks like you're returning a string. It might be better to do:

        string temp = ash.handleMessage(buf);
        int size_of_temp = temp.length();

This would avoid repeating any action that takes place in handleMessage.

bstpierre
yeah I switched this before you said anything but that was the problem not sure why calling it twice would do this but also not sure why I was calling it poorly like that in the first place. Working on code for 5+ hours while hungry might be a cause. Thanks
Pieces
It could also have something to do with a dangling pointer to a temporary object that is then destroyed. Take a hard second look at the code in `handleMessage()`.
bstpierre