Jump to content
  • Advertisement
Sign in to follow this  
gandolf1212

and operators to stream data

This topic is 4916 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a server and want to send data to a client using the >> and << operators. I already have it working a little:
Inside Client Class:

void operator<<(char *data){
	string msg = data;
	msg += "\n";
			
	if(send(socket, msg.c_str(), strlen(msg.c_str()), 0) == SOCKET_ERROR)
		disconnect();
	};
}
The problem is that it only works with one operator. I need to be able to send in more than one feild. I also want it to concatinate all the feilds together before sending them. So, if I called: client<<"Your value is "<<x<<endl; It would put all the arguments together and only call one send command. I'm thinking what I need to do is make a new class called cstream, and overload it's << operator to return the address of a cstream and to take types like char*, int, double, anything I want serilized, ect. Then I need to overload the clients << operator for types of cstream, as well as any other type that can be single called. Is that right or would there be a better way not needing an intermediate class? If I just write Client::operator<< to accept and return a Client it would stream, but then I don't think it could tell if it was the last call or not. Even thought about writing something like: client.send()<<"Hello"<<endl; client.send() would be called after the data was all put together, and it would work because it would return a refrence to the calling class. I kindof like that method because it allows input to be gathered without being sent, and means only one class is needed. Which way do you think is best?

Share this post


Link to post
Share on other sites
Advertisement
You should really create a new streambuf type for that. Overloading the << and >> operators should be reserved for creating new types. Also you're supposed to return a reference to the stream you're operating on.

I wrote a socket class for streambuf, but I haven't tested it yet, it's part of a project I'm trying to make and I'm still putting the basic pieces together at the moment. Working on a thread class at the moment. If you want it I'll share it but no guarentees that it'll work right.

Share this post


Link to post
Share on other sites
socket.tar.bz2

Example program:

#include <iostream>
#include <string>
#include "socket.hpp"

int main()
{
SocketStream sstream(80, "www.google.com");

sstream << "GET / HTTP/1.0\n" << std::endl;

while(sstream.good())
{
std::string line;
std::getline(sstream, line);
std::cout << "Read: " << line << std::endl;
}

std::cout << "Done!" << std::endl;
std::cin.get();

return 0;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!