flat assembler
Message board for the users of flat assembler.

Index > Windows > winsock in threads

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
zxcv
Guest




zxcv 22 Dec 2007, 09:58
im writing multithread program.
Can i call wsastartup only in main thread? Or its a mistake?
Post 22 Dec 2007, 09:58
Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak 22 Dec 2007, 10:55
No more giving back...


Last edited by kohlrak on 07 Aug 2008, 14:57; edited 1 time in total
Post 22 Dec 2007, 10:55
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20289
Location: In your JS exploiting you and your system
revolution 22 Dec 2007, 11:08
Don't ask everyone, try it out and see what happens. But, if your computer blows up then I didn't suggested it, okay.
Post 22 Dec 2007, 11:08
View user's profile Send private message Visit poster's website Reply with quote
zxcv
Guest




zxcv 22 Dec 2007, 13:01
Quote:
Don't ask everyone, try it out and see what happens.

You think i know everything? On my system it works, but how long? Will it crash?
Post 22 Dec 2007, 13:01
Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20289
Location: In your JS exploiting you and your system
revolution 22 Dec 2007, 13:12
Alright, now shift your wsastartup into just the process initialisation code and try it. If it still works as expected then I would guess that you don't need to put wsastartup in each thread. Once again, if your computer blows up because of this then I didn't suggest it, okay.
Post 22 Dec 2007, 13:12
View user's profile Send private message Visit poster's website Reply with quote
peter



Joined: 09 May 2006
Posts: 63
peter 26 Dec 2007, 01:00
Documentation says that WSACleanup terminates winsockets for all threads. It also says that WSACleanup and WSAStartup calls should be paired ("there must be a call to WSACleanup for every successful call to WSAStartup").

From these facts, you can deduce that WSAStartup can be called only in the main thread (again, if it doesn't works, then I didn't suggest it Smile.
Post 26 Dec 2007, 01:00
View user's profile Send private message Visit poster's website Reply with quote
Vasilev Vjacheslav



Joined: 11 Aug 2004
Posts: 392
Vasilev Vjacheslav 26 Dec 2007, 09:11
i used sockets in thread and everything works like a charm

pseudo-code:
Code:
void _thread()
{
WSAStartup();
send()/recv();
WSACleanup();
}
    
Post 26 Dec 2007, 09:11
View user's profile Send private message Reply with quote
Lupo1977



Joined: 02 Jan 2008
Posts: 1
Location: Germany
Lupo1977 02 Jan 2008, 18:13
however, the documentation says that you can call WSAStartup a lot of times. but it is necessary to call at least one time, then the WinSock dll intializes some necessary internal datastructures for the actual process.
if you call WSACleanup these internal structures are released.

so the original purpose is to call WSAStartup at the beginning of your main process and WSACleanup if you end using sockets.

i think its not a good idea to call WASStartup and WSACleanup inside a thread. could be a wasting ressources and maybe its not stable at all.
Post 02 Jan 2008, 18:13
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 02 Jan 2008, 21:29
I can't see why you would call winsock startup/cleanup for each thread... keep yourself to a single pair, in your main thread...

And try to use as few threads as possible. Check out EventSelect.
Post 02 Jan 2008, 21:29
View user's profile Send private message Visit poster's website Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak 02 Jan 2008, 21:34
No more giving back...


Last edited by kohlrak on 07 Aug 2008, 14:56; edited 1 time in total
Post 02 Jan 2008, 21:34
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 02 Jan 2008, 21:38
Blocking sockets = performance bottleneck.

Sure thing, non-blocking is a bit harder to program, but it's the way to go if you want to handle multiple connections (or even get decent speed with a single), unless you want to do kiddie programming.
Post 02 Jan 2008, 21:38
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 02 Jan 2008, 21:45
what's wrong with (blocking Razz) accept() method?
Post 02 Jan 2008, 21:45
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 02 Jan 2008, 23:20
Blocking isn't necessarily so bad for accept() if you expect your connect rate to be low, but once you have connections going, you do NOT want to be blocking... and you especially don't want to use protocols that impose an additional ACK layer on top of TCP, and you don't want to try and force a "packet" scheme upon TCP either.

At least not if you're going to deal with a lot of connections (p2p) or high-speed transferring (it takes some effort to saturate a gigabit LAN).

Of course if all you're trying to do is a simple IM client or echo service, who gives a fuck Smile
Post 02 Jan 2008, 23:20
View user's profile Send private message Visit poster's website Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak 03 Jan 2008, 00:50
No more giving back...


Last edited by kohlrak on 07 Aug 2008, 14:56; edited 1 time in total
Post 03 Jan 2008, 00:50
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 03 Jan 2008, 00:53
Kohlrak, do you have any clue how TCP works? Smile
Post 03 Jan 2008, 00:53
View user's profile Send private message Visit poster's website Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak 03 Jan 2008, 00:55
No more giving back...


Last edited by kohlrak on 07 Aug 2008, 14:56; edited 1 time in total
Post 03 Jan 2008, 00:55
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 03 Jan 2008, 01:00
Okay, well...

if you use UDP, you send fixed-sized packets/datagrams, which might (and likely will) appear in any order. UDP should only be used when you need really low-latency communication, and you should think long and hard before using it, for plenty of reasons; one of them being that often you end up basically re-implementing TCP on top of UDP, another being that many routers don't do proper connection tracking for UDP unless it's a protocol they know, meaning people will manually have to open ports etc... bother.

With TCP, you have a STREAM of data, and you are guaranteed the individual bytes arrive in the order they were sent. You don't have any guarantees that send(buffer, 1024) won't arrive as, say, 2x512 bytes, and small buffer sends will usually be cached for a while, since it's wasteful sending off an entire ethernet/whatever packet with small payload (google the Nagle algorithm).

Even with blocking sockets, you must always be prepared to handle whatever amount of bytes is returned to you, very likely buffering, stitching together, and splitting up as you go. So you might as well go the full mile and support async I/O, since it gives you better performance in the end.
Post 03 Jan 2008, 01:00
View user's profile Send private message Visit poster's website Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak 03 Jan 2008, 01:06
No more giving back...


Last edited by kohlrak on 07 Aug 2008, 14:56; edited 1 time in total
Post 03 Jan 2008, 01:06
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 03 Jan 2008, 01:11
kohlrak wrote:
Well, if you re-invent TCP with UDP, you can send a fixed size. Using UDP for a game or something might be considerably easier, because if it gets lost, the guy won't move, rather than locking up the server because of a bad connection.


Obviously the game server utilizing would be using async I/O to avoid having a single bad client locking up the entire show :]

And fixed sizes, while it might seem convenient, isn't necessarily a good thing for performance. Just look how miserable windows filesharing (SMB/CIFS) performs on a gigabit LAN. Hint: instead of streaming files, they use fixed-size packets.

_________________
Image - carpe noctem
Post 03 Jan 2008, 01:11
View user's profile Send private message Visit poster's website Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak 03 Jan 2008, 01:13
No more giving back...


Last edited by kohlrak on 07 Aug 2008, 14:55; edited 1 time in total
Post 03 Jan 2008, 01:13
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.