winsock chat

This is a discussion on winsock chat within the Networking/Device Communication forums, part of the General Programming Boards category; hello,
i am going to have a go at creating a simple console chat application. now i have been trying ...

winsock chat

hello,
i am going to have a go at creating a simple console chat application. now i have been trying in the past few days but, i am stuck. when the data is recived, i want to display it. but if i am typing in a message, i dont get the recevied message displayed till i finish typing?
how do i stop this problem?
in vb, its easy because its like... 'on recv_data' blah blah..
so as soon as the data arrives it displays it..
hope it makes sense!
please help.

Well, it depends on how you are sending the data. If you wait till the end of the input to send you will obviously not receive the input on the other end character by character. If you are sending on each character, you should be receiving character by character or close enough. Sending character by character is, of course, very inefficient.

server is in the middle of typing a message, the server doesn't display the clients message.. untill he has finished typing the message. the server can only display the clients message when he presses enter, and executes the next bit of code, which would be "cout<<inmessage;"

im sorry i have no code to post at the moment, i havn't realy started.. this is just a problem i ran into when i was just messing with some code from 'game tutorials'

So, your real problem is that getting input is a blocking operation, which prevents you from processing or displaying an incoming message.

So you need to either receive messages on another thread or you need a non-blocking way of getting console input.

Can we assume you are using windows as both methods are non-portable? Note that a GUI program is probably more suitable as it is event driven, like Visual Basic. However, it should be possible in the console.

bool isReadable(SOCKET s)
{
/* Use select() to determine if data is waiting to
* be read on socket. */
}
while (TRUE)
{
if (keyHit())
{
/* A character is available for input */
char ch = getChar(); /* Get character from input */
/* Process character. If it is an enter send the message. */
}
else if (isReadable(my_socket))
{
/* An incoming message has arrived on the socket
* Read message using recv() and display it on the console */
}
}

Yes, that would work but you have to make sure you do not enter characters past the end of your buffer!

As you surmised, if no character is input then a message will not be seen.

If you like, you can replace the keyHit() and getChar() functions in the code I posted with _kbhit() and getchar() which are available as Visual C++ functions. However, _kbhit() can be somewhat CPU intensive.

but the problem is, in the third example, i dont understand any of the code, and i want to know whats going on in the program.
i am realy stuck.. i dont know what to do
and the problem with my code above, is that, its only possible to display a recieved message when typing..