• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

C++ sockets help

jcvd

n00b
Joined
Apr 21, 2002
Messages
33
I have the following code which was taken directly from a book:
http://misc.ug-pro.com/code.txt

It opens the socket, but its not seeing any incoming connections. I'm not sure if the code is somehow wrong, or if perhaps it just doesn't like my compiler (bcb).

BTW if anyone tests this, I believe you have to change the __WIN32__ to just WIN32 if you are using a non borland compiler.
 
I just compiled and ran it, it runs and opens a socket, I can connect to the socket with telnet alright but it doesnt recv anything I try to send. I havent messed with sockets enough to help ya much though, which book are you using? (curious) Most of them have an online errata.

I'll give your code a good look through later though, maybe ill find it.
 
The book is MUD Game Programming. I'm not making a game, just trying to learn sockets really. Anyways the select always returns -1 and I don't believe it should be doing that.
 
The status function is returning SOCKET_ERROR (-1) and a infinite loop results in the while (!done) loop. I'll tell you why as soon as I find out.

edit:
Seems you are placing an uninitialized value to rset (lsock) and then looping through an empty socketlist int vector and setting effectively nothing else to rset.

Then you pass this into select, which checks the status of a socket, it returns socket_error, -1, which slips by unchecked and enters an infinite loop.
 
It seems that the author of the book meant for lsock and sock to be the same thing. When I change my code accordingly, it finally works. Thanks for the help. :)
 
Google for "Beej's guide to sockets" of "...guide to network kung-foo" or something. You might find it a worthwhile addition to your reference material.
 
Ok another problem I am having. It doesnt have to do with sockets but it will be used in that code so I will ask that here as well. I am going through part of the loop where I am iterating through my sockets. Now I want to be able to have it return an int and a string each time the loop completes. I don't see how I can do this while letting the loop complete still. What I thought of is using a map but that seems like a poor idea. Basically what I want to do is build a queue by running the socket loop in 1 thread, and in another thread I will be reading the queue. Im sorry if this is vague.. I put the socket stuff into a class to keep it away from the other stuff, but I dont know how to handle the text since its received inside the sockets loop and I cant just stop at the first incase there are more then one :confused:
 
threads -and- select?

This doesn't make a whole lot of sense. If you're going to start dicking around with multiple threads/procs, don't bother with threads, just have a thread for each connection and look into using proper IPC.



Just wanna toss in one thing - when doing network programming -
Python + Twisted = life is good

http://www.python.org
http://twistedmatrix.com/
 
ameoba said:
just have a thread for each connection and look into using proper IPC.

One thread per connection? So, with 500 connections, you have 500 threads? That's not going to work very well.

.B ekiM
 
ameoba said:
threads -and- select?

This doesn't make a whole lot of sense. If you're going to start dicking around with multiple threads/procs, don't bother with threads, just have a thread for each connection and look into using proper IPC.



Just wanna toss in one thing - when doing network programming -
Python + Twisted = life is good

http://www.python.org
http://twistedmatrix.com/
Its not a bad idea.. 1 thread to handle the socket loop while the other thread processes the information from the socket loop. It doesn't matter how I go about accomplishing that though.. I dont know how to return something each time I get to the end of a for loop (which is inside the socket loop) without exiting the function.
 
You can't return values without exiting the loop. Return values aren't going to help you anyways if the doStuff() is in another thread. If you're using multiple threads, you need to have some sort of IPC mechanism in place to get information from one to the other & back.
 
jcvd said:
Its not a bad idea.. 1 thread to handle the socket loop while the other thread processes the information from the socket loop.

If you have exactly one connection, it's a reasonable approach.

If you have multiple connections, it's a terrible idea. Threads aren't free; past a dozen active connections, you're going to waste an appreciable amount of time context switching instead of getting work done. And you'll never scale to more than a couple hundred connections on a single-processor box.

.B ekiM
 
Back
Top