Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

WinSock w/ pthreads, C++

Name: Anonymous 2008-10-13 5:49

I'm testing a concept so excuse the messy code, it's my first time really using pthreads at all. Running into a bit of a problem; it handles the first client that connects just fine, but none afterward. At first I thought it had something to do with my accept socket, because if I closed the accept socket after setting it to the socket in my thread's object, it would close the connection, so I made an array of accept sockets. Still not working.

In short, I don't want bitching about my coding style, etcetera, as I just threw this together to see how it works. It's most certainly not going to be used in anything; it's just a learning tool of sorts. If anyone can find where and why it's not working as I intend it to, I'd greatly appreciate an explanation.

Code: http://pastebin.com/m47bff01e

Thanks.

Name: Anonymous 2008-10-13 8:03

if I closed the accept socket
wat

so I made an array of accept sockets
oh god

man accept.  Read the first goddamn paragraph.

Name: Anonymous 2008-10-13 14:09

>>2
man, accept.

Name: Anonymous 2008-10-13 15:46

accept SICP

Name: Anonymous 2008-10-13 15:48

>>2
accept instructs the printing system to accept print jobs to the specified destinations.
Also,
Apple Inc.

Name: Anonymous 2008-10-13 16:39

>>5
man man

Name: Anonymous 2008-10-13 17:19

gimme a break, man!

Name: Anonymous 2008-10-13 17:39

>>6
This manual page explains the groff an.tmac macro package (often called
the man macro package).  This macro package should be used by  develop-
ers when writing or porting man pages for Linux.  It is fairly compati-
ble with other versions of this macro package,  so  porting  man  pages
should  not  be  a  major  problem  (exceptions  include  the NET-2 BSD
release, which uses a totally different macro package called mdoc;  see
mdoc(7)).
You really ought to be more specific.

Name: Anonymous 2008-10-13 17:41

If you're going to write sepples, do sepples, not mutant 'C'

http://pastebin.com/m8df956d

Name: Anonymous 2008-10-13 17:54

FUCK OFF EVERY1

Name: Anonymous 2008-10-13 20:31

cout<<tinfo->thread_id<<" > "<<string(buf)<<endl;

This is a clusterfuck of non-whitespace characters. I can see what it's doing, but I don't want to know because it's so unintelligible. I want you to stop doing this. You are ignoring whitespace for her mentally retarded younger brother. I want you to stop doing that.

Name: Anonymous 2008-10-13 20:50

Throughout the UNIX manual pages, a manual entry is simply referred to as a man page, regardless of actual length and without sexist intention.

Name: Anonymous 2008-10-13 22:26

Name: Anonymous 2008-10-14 5:52

Yeah, OP here.

Love how you all comment on everything BUT the problem... you know, the exact thing I asked you not to do? I'm not looking for criticism on the writing style, etcetera. I know it's bad, I know how to improve it already. It's just some trash thrown together to try out pthreads. I'm trying to figure out why it's only handling the first socket and the first thread at a time. It almost seems as if something is blocking -- which, I've been led to believe, should not be the case with threads created using pthread_create()... and I don't see anything blocking other than listen and the accept loop in the main thread, and those are intended to.

>>2
As you can see, after the accept socket picks up a connection, it gets passed to a socket in the struct array. If I close the accept socket, it closes the connection that was passed as well, effectively closing both the accept socket and the socket in the struct.

The array of accept sockets was just for me to look at their behavior a little further. It doesn't really have a need here.

It's called "learning."

Name: Anonymous 2008-10-14 6:26

>>14
Welcome to /prog/.

Name: The Sussman 2008-10-14 7:19

I'll help you if you convert your code to Scheme, the one and only algorithmic language.

Name: Anonymous 2008-10-14 7:23

>>16
Algorithmic language? What the fuck does that mean? Aren't all programming languages by default algorithmic, wiseass?

Name: Anonymous 2008-10-14 9:15

>>17
Obviously not.

Name: Anonymous 2008-10-14 9:53

>>16
ALGOL

Name: Anonymous 2008-10-14 9:53

gtfo The Sussman, you're not an EXPERT BBCODE PROGRAMMER

Name: 2 2008-10-14 10:17

>>14
If I close the accept socket, it closes the connection that was passed as well
Retarded Windows behavior, whatever, but either way WHY WOULD YOU DO THAT.  Stop blaming pthreads when your network code is so broken.

Name: Anonymous 2008-10-14 10:32

If you're going to use WinSock, why the hell are you using pthreads? Just use CreateThread(), your code isn't going to be portable anyway.

if I closed the accept socket after setting it to the socket in my thread's object, it would close the connection
...that's like pulling the phone cord out of the socket in the middle of a phone call.

Name: Anonymous 2008-10-14 10:39

...that's like putting too much air in a balloon.
Not really.  In a proper socket implementation, received connections are in no way tied to the original listener.

Name: Anonymous 2008-10-14 12:39

OP, assuming you just want something with moderate throughput, rather than being industrial strength (for which you need messy asynchronous I/O constructs, or you write it in Erlang, like God intended), the structure you want is

Main listening thread -- a socket in a loop, blocking on accept(), ideally with time-wait as well to take the occasional look around to see if the process is shutting down
Connection threads -- when accept returns, pass the new socket into a thread that you spin off a new thread at that point with the socket as data, do your per-connection I/O in that (a select() loop is simplest, rather than trying to coordinate up-link and down-link on two separate threads), and when the connection is closes, just exit the thread procedure.

Things like thread-pooling are probably too cumbersome for the first learning exercise.

Name: Anonymous 2008-10-14 16:54

>>23
proper socket implementation
Windows

Name: Anonymous 2008-10-25 2:54

>>24
Thanks for the advice, but I'm more or less wanting to know why it's behaving this way.

For the others who have replied... rather than discussing why its behavior is as it is, you're poking at irrelevant portions. Why?

Name: Anonymous 2008-10-25 3:05

>>26
why are you assuming that your broken network code is irrelevant when trying to figure out why your program isn't working the way you expect it to?

Name: Anonymous 2008-10-25 3:16

>>27

Because it's as if the other threads don't even launch. If I put a printf before pthread_create and one at the start of the procedure it launches, it doesn't make it to the second printf the second time around.

Name: Anonymous 2008-10-25 3:35

>>28
Have you considered using Erlang?

Name: Anonymous 2008-10-25 7:40

>>28
Just use a single socket to listen for connections. Try checking the return code of bind in line 114 to see why using multiple listen sockets is a bad idea.

Name: Anonymous 2008-10-25 14:59

>>30
He's not using more than one listen socket. It also has the same behavior whether he has his accept socket array or not. Says so in the beginning.

Name: Anonymous 2008-10-25 15:32

>>30
My bad, he is. (This is >>31)

Name: Anonymous 2008-10-25 15:36

Op here.

>>30
Thanks. You're the only one to really give me any useful information so far. First thing I should've done was put in  some error checking to try and catch this sort of thing. I'll poke around a bit more now.

Name: 33 2008-10-25 15:37

>>32
God you know there is a reason for a name field. It's so you can use it.

Name: Anonymous 2008-10-25 15:43

It's working now! Thanks a bunch >>30

http://pastebin.com/m1bd6af33

Needs a lot of cleaning up, but fixed that issue. Exactly what you said, issue with bind. I kept rebinding every time the loop iterated, and it failed because the address was already in use. Cut down now to an accept socket, a listening socket, and the client structure array's sockets.

Name: Anonymous 2008-10-25 16:17

>>35
So basically exactly what we've been saying all along, only you're too stupid to read.

Name: Anonymous 2008-10-25 16:53

>>36
No, exactly what you folks haven't been saying all along. The majority of what's been said was ridiculously vague or not concerning the problem at all. You've made it quite obvious that you're the one who's too stupid to read; your statement has no backing.

I was wrong in that it was my network code causing the problem, but that doesn't make everyone that said "LOLOL YOU SHOULD USE SOMETHING ELSE" or "LOLOL YOU SHOULD USE MORE C++ FUNCTIONS" or flat out didn't address the problem at all, right. Yes, someone finally said it was my broken network code - but that's too vague to be of much use. >>30 was the first to actually give me an idea of where things went wrong.

Note the following that either didn't help or were irrelevant:

>>2
>>3
>>4
>>5
>>6
>>7
>>8
>>9
>>10
>>11
>>12
>>13
>>15
>>16
>>17
>>18
>>19
>>20
>>21
>>22
>>23
>>25
>>27
>>29
>>31
>>32
>>34

and oh look >>36


So yeah... saying all along? Hardly.

Name: Anonymous 2008-10-25 17:08

Use libevent.

Name: Anonymous 2008-10-25 17:31

>>9

Name: Anonymous 2008-10-25 17:45

I stopped reading when I found out it was Sepples.

Name: Anonymous 2008-10-25 18:44

>>37
>>2 already pointed out that the problem was that your using multiple accept sockets, so I wouldn't call it irrelevant.

Name: Anonymous 2008-10-25 22:22

>>41
Except that wasn't the problem.

The problem was trying to rebind a listening socket.

So yes, I'd call that irrelevant.

Name: Anonymous 2008-10-26 1:57

I am proud to have contributed one or more irrelevant posts to this wonderful thread.

I am >>11 and proud of it.

Also, OP can choke on my nutsack for being a whiny douche.

Name: Anonymous 2008-10-26 7:53

>>42
And you were rebinding a listening socket because you were using multiple accept sockets. You just didn't understand >>2's answer until >>30 pointed out why using multiple accept sockets wouldn't work.

Name: Anonymous 2008-10-26 14:16

>>44
No, I was not rebinding a listening socket because I was using multiple accept sockets. I was rebinding a listening socket because it was inside a loop that it should have been outside of. If I put multiple accept sockets back in, it works fine; it's just redundant and useless.

>>43
Whiny douche? Why, because I, oh I don't know, like it when people actually mention something useful or relevant in reference to my questions? Troll harder, >>43.

Also, in reference to >>11 ... it's easily readable. Very easily. Unless you're dyslexic or something.

Name: Bro 2008-10-26 14:19

Bro, boring thread, bro.

Name: Anonymous 2008-10-26 14:35

>>45
If you want useful and relevant answers try asking your questions on stackoverflow.com instead of /prog/ next time.

Name: Anonymous 2008-10-26 14:36

OP here, I can't program and should give up.

Name: Bro 2008-10-26 15:10

Bro, OP here, bro.

Name: Anonymous 2008-10-26 22:47

>>45
SAME PERSON AND YOU HAVE BEEN TROLLED CONSTANTLY

Whiny nutfucker, go to /pr/ if you want real answers. We masturbate all over our copies of SICP while currying Haskell nomads with SUAVE LISP TOADS here in /prog/.

Quit getting butthurt over the internet, it makes you look like you don't know what's going on.

Name: Anonymous 2008-10-26 23:30

>>50
I think you need to work on your meme lore, bro.

Name: Anonymous 2008-10-26 23:54

>>51
Totally, bro. (no homo)

Name: Anonymous 2008-10-27 12:33

>>50
Sage for this post.

Name: Bro 2008-10-27 13:25

Bro, Age, bro.

Name: Anonymous 2009-08-16 17:24


Don't change these.
Name: Email:
Entire Thread Thread List