Jump to content
  • Advertisement
Sign in to follow this  
Antonym

Std::cin alternative?

This topic is 2996 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 trying to write a sort of chat program. However, right now the client can only recieve messages until it has sent one because std::cin must wait until the client has pressed enter. Is there an alternative to this?

Thanks.


#include <SFML/Network.hpp>
#include <iostream>

void DoClientTCP(unsigned short Port)
{
// Create a TCP socket for communicating with server
sf::SocketTCP Client;
sf::IPAddress Address = sf::IPAddress::GetPublicAddress();

// Connect to the specified server
if(Client.Connect(Port, Address) == sf::Socket::Done){
std::cout << "Connected to server " << Address << std::endl;
}

std::string message;
while(1){
std::cin >> message;

// Send the message
sf::Packet packet;

packet << message;
if(Client.Send(packet) == sf::Socket::Done){
std::cout << "Message sent to server : \"" << message << "\"" << std::endl;
}

if(Client.Receive(packet) == sf::Socket::Done){
packet >> message;

// Show it
std::cout << "Message received from server : \"" << message << "\"" << std::endl;
}
}


// Close the socket when we're done
Client.Close();
}

Share this post


Link to post
Share on other sites
Advertisement
Append individual keypresses to the message and send when character==Return.

You'll also have to poll for receiving or you won't be able to send multiple messages until a message is received.

EDIT: I understand, if you've never done threads, writing multiple thread code is not a fun thing to learn/test. However, if you're going to play with sockets, that's really the best path to take.


[Edited by - Buckeye on July 9, 2010 7:19:43 AM]

Share this post


Link to post
Share on other sites
Thanks for the replies. I've tried google but I am a little confused. What is the standard function for handling key presses in console?

About the polling, I think SFML does that for me, the Send and Recieve functions don't block.

What a pain. Would you mind pointing me to a resource for getting started with threads?

Thanks again.

Share this post


Link to post
Share on other sites
If you're using Windows you can catch the keypress events as they happen and append those characters to the message until the user presses return or whatever is your send key at which point you can send the message and clear the current message buffer.

Share this post


Link to post
Share on other sites
You can use the curses.h (=conio.h in Windows) to get non-stopping keyboard input, and then you just check when enter was pressed to send the string:
#include <stdio.h>
#ifdef WIN32
#include <conio.h>
#else
#include <curses.h>
#endif
#include <string>
using namespace std;

int main()
{
string s;
char c;
while(true)
{
if(kbhit())
{
c=getch();
if(27==c)
break;
else
if(13==c)
{
printf("SENDING s=%s\n",s.c_str());
s="";
}
else
{
s+=c;
printf("s=%s\n",s.c_str());
}
}
}
}

Share this post


Link to post
Share on other sites
I don't think it's that easy on non-Windows systems. The header <curses.h> does exist on many systems, but as far as I know it's not compatible with <conio.h> (e.g., no kbhit(), you have to change the terminal to non-delay mode...).

If platform independence is important, it can probably be achieved by using SDL.

What systems are being targeted here?

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!