Sign in to follow this  
Gavinl

winsock

Recommended Posts

Hey i just looked into winsock and tried to make a simple IRC client. So far i got it conneting to an IRC server, and it can recieve messages. Here's my problem: right now i have the recieve function in a infinte loop, checking to see if there's any messages while(1){ bytesRecv = recv( m_socket, recvbuf, 513, 0 ); ... } is this the only way i can recieve messages, putting the recv function in an infinite loop? the problem is that my CPU heats up very fast, its a P4 3gig, 1meg cache 800fsb. I've noticed mIRC doesn't cause my CPU temperature to rise, but my client does, so i'm assuming mIRC is not in some kind of loop. Anyone know how its done? Thanks

Share this post


Link to post
Share on other sites
Are you using windows? If so, insert this somewhere in your loop (preferably at the end) Sleep(1);.

That right there pauses your loop for 1 millsecond? (I know that setting it to 1000 is a one second pause) or so and thus your program consumes WAY less CPU power.

Hope that helps.

Share this post


Link to post
Share on other sites
What you need to do is take a look into threads. Networking is one of the main uses of threads. Your program will create another process that will listen for input in the background, thus saving your program from looping so long. I'm pretty sure that this is how mIRC is done. Sorry I do not have any good links though. I've been looking for a few simple ones myself.

- Drew

Share this post


Link to post
Share on other sites
well i do have it in a thread, what I have done was created a class that inherits from a thread, my goal is to make a simple irc class that lets the person just do things like:

IRC myIRC(server, port, nickname);
myIRC.connect();
while(myIRC.messages()){
do_something( myIRC.getNextMessage )
}

bascially my connect function tells the IRC thread to start running, and the run function that is looped infinitely is what checks the server for incomming messages.

Whats a good thread class to use? I'm currently using a very simple one i found online

Share this post


Link to post
Share on other sites
The correct solution is usually to use select(), where you can specify a time-out to wait for data to arrive. If you want to also service typing within the same thread, then you should use the typical event loop of waiting for messages and handling them, and then set a timer that runs, say, 10 times a second, where you can poll the network.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this