tags:

views:

182

answers:

3

I have a client using select() to check if there is anything to be received, else it times out and the user is able to send(). Which works well enough. However, the program locks up waiting for user input, so it cant recv() again until the user has sent something. I am not having much luck with using threads either, as I cannot seem to find a good resource, which shows me how to use them.

I have tried a creating two threads (using CreateThread) for send and recv functions which are basically two functions using while loops to keep sending and receiving. And then the two CreateThreads() are wrapped up in a while loop, because otherwise it just seemed to drop out.

I have basically no previous experience with threads, so my description of what i've been doing will probably sound ridiculous. But I would appreciate any help, in using them properly for this kind of use, or an alternative method would also be great.

+2  A: 

Can't find a good resource on socket programming? Bah. Read the bible:

Frank Krueger
Hey, what if he's not religious :P? Also, since he said "winsock," I'm guessing that the Unix Network Programming bible ain't what he's looking for.
Matt Ball
It's exactly what he's looking for. Winsock is just an implementation of BSD sockets - exactly what are covered in that book. socket, connect, listen, send, recv - those are all BSD sockets and in Winsock.
Frank Krueger
While UNP is indeed awesome and useful to Winsock programmers, it covers maybe 1/4 of Winsock. Winsock extends BSD sockets quite a lot to make it more useful on Windows, with its fundamental philosophical differences relative to Unix programming. Writing pure BSD sockets code isn't always the right way to do something on Windows. So, Rick should get a Winsock book, possibly in addition to UNP. It appears they're mostly out of print right now, but a reprint run of Quinn and Shute is due in January. Meantime, check libraries and the used book market.
Warren Young
@Warren, excellent points. I just prefer portable software over platform-specific stuff.
Frank Krueger
A: 

It sounds like you could use a good primer on network programming and on threads as well. Trying to learn both at once is not easy (read: drinking from two fire hoses) but not impossible. A really good place to start is with Beej's Guides.

In particular, his guide to network programming is top-notch and does address network programming in Windows (though the guide is primarily POSIX-oriented).

Threads are a different beast. I've only worked with pthreads, so unless you're using a Windows pthreads API, I can't help you there.

Matt Ball
A: 

this has a strong feel of assignment work...

I am not too much into Windows programming but when I did do some earlier, there used to be a kbhit (probably) function that allowed you to check if the user had sent any inputs. You could look for something similar and try a non-blocking user input check before starting your select again.

You could then get this working without multi-threading
(unless, er, you have to use that).

nik