Jump to content
  • Advertisement
Sign in to follow this  
Toadhead

IRC client using winsock

This topic is 4809 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'm creating a simple IRC client using winsock and SDL. The problem, however is how I'm going to receive data and send data at the same time? I don't want a gameloop were I re-draw the whole screen every frame because this will slow up the client and the client is supposed to use as less of the CPU as possible since you need to run other applications too. So I only want to redraw the screen when typing a message, (and after I send it to the other people), and when I receive an imcoming message. The recv() function blocks when you call it and only returns when it receives data from the server. This is good but it will be a problem when I'm typing a message. Does anyone knows a solution? Thanks in advance, Toadhead

Share this post


Link to post
Share on other sites
Advertisement
hm..
But what if I'm typing something?
It should store the letters it in a char array and it should print the letters on the sdl screen. Could you show an example how to use select() in this case since I have no idea how to use it here..

Share this post


Link to post
Share on other sites
Well monitoring input would have to be non-blocking. I'm not up to scratch with this but it must be possible with the streams.

Here's your example.


SOCKET TCPStream::AcceptConnections( sockaddr_in& addr )
{
if ( socketOpen )
{
Sleep(1000);
// first check the listening socket to see if any connections are waiting
fd_set socketSet;
socketSet.fd_count = 1;
socketSet.fd_array[0] = listeningSocket;

timeval delay;

delay.tv_sec = 0;
delay.tv_usec = 1000;

unsigned int numReady = select( NULL, &socketSet, NULL, NULL, &delay );

if ( numReady != 0 )
{
int size = sizeof( sockaddr_in );
clientSocket = accept( listeningSocket,
(sockaddr*)&addr, // Address of a sockaddr structure (see explanation below)
&size); // Address of a variable containing size of sockaddr struct
if ( clientSocket > 0 )
{
connectedToClient = true;
return clientSocket;
}
}
}
return 0;
}




The above is a function from a TCPStream class i wrote. It polls the listening port for data using select and if data is waiting attempts to make a connection.

Hope it helps!

ace

Share this post


Link to post
Share on other sites
Well this code is from a server I guess.
Look I know how to use select(), but not how to use it in my case. I need to check recv() for incoming connections but I also need to be able to write code.

Maybe I can use CreateThread() for this. Just let recv() check for incoming data while i'm busy in a while loop were I check for key presses all the time.


Share this post


Link to post
Share on other sites
Sounds like you don't know how to use select() (no offense)
What it does is inform you which sockets have incoming data, whether there are new incoming connection requests, which sockets are ready to write, and so on. Which means you know where you can call recv, accept or send *without* blocking.
One of the parameters for select() sets how long it should block for. Set that to 0 if you want to return immediately (for example so you can check for input)

On a side note, being "busy in a while loop checking for keypresses" is baaaad.
That way, you'll end up using 99% cpu. Since this isn't a game, that's probably not what you want.

Share this post


Link to post
Share on other sites
uhm.. yes but select() will tell me when I use send. But before I use send I need to typ something write?

I mean if I press on the 'e' button it should appair in the send bar (not sended yet but just were you typ all stuff before you it enter). But select() won't notice this since i'm not sending anything. This means select() wont return and so there wont happen anything (no e will appair on the screen). Only when the complete text is ready and I press enter it should send this text.

Or am I getting it wrong now?

Share this post


Link to post
Share on other sites
I still don't get it. I have to check for keypresses right? I mean how else can I show those letters on the screen. If someone presses a key it should be saved in an array and printed on the screen and when he or she presses send it should be used in the send() function and send it to the server. It should check for keypresses whole the time and it should receive incoming data using recv() whole the time without using too much of the CPU. Does anyone knows a solution?

Share this post


Link to post
Share on other sites
I don't know how to use SDL, so I can't help with that, but I am sure it can handle keystrokes, which you will want to keep track of so that when they click send, you can send it.

You don't need to use select() for sending data, only for recv'ing it without blocking. For my game client (which the chat is similar to IRC)
I removed the recv code because mine will differ with yours. read_fds is the name of my socket. If there is no data to read, it will not stop your program.

void CheckData()
{
timeval timetemp;
timetemp.tv_sec = 0;
timetemp.tv_usec = 0;

if (select(0, &read_fds, NULL, NULL, &timetemp) == SOCKET_ERROR)
{
if (WSAGetLastError() == 10038) // socket was closed, it isn't a socket anymore
return;
std::cout << WSAGetLastError() << std::endl;
}
else
{
if (FD_ISSET(sockfd, &read_fds))
{ // we got something to receive, handle recv here

} // else there is no new data
FD_ZERO(&read_fds); /* Initializes the set to NULL */
FD_SET(sockfd, &read_fds); /* Without this line, select() returns */


}
}


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!