Thread: Simple Winsock Wrapper

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    Simple Winsock Wrapper

    I've been working a while at making a simple Asynchronous Winsock Wrapper and I'm pretty satisfied with what I have. Any other wrappers I've looked at seemed too complicated for simple purposes. I would love some help with it; right now the big thing is with classes and sending/receiving data. They work, but not very well and was wondering if someone would be willing to work on this project with me. Thanks for any help.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    I just finished a similar project for a cross-platform wrapper class. I would love to chat with you and swap some code.


    Hope to hear from you.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    What kind of asynchronous sockets are you using? Event, message, overlapped event, completion routine?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    DeepBlackMagic, get my contact info from any of my posts.
    Hunter2, uhm, I'm guessing events. They're basic Winsock Asynchronous sockets. They work on events.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>uhm, I'm guessing events... They work on events.
    A logical guess
    **EDIT** Uh, do you mean network events, or WSAEVENT objects?

    Cool, actually I'm working on a wrapper too right now, using async. winsock with events. Did you add support for more than 64 sockets (i.e. auto thread management)?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    No, mine is very simple, using the FD_READ, etc. events. I'm definately willing to share code, and even more to work with someone on this kind of thing. My biggest problem is handling data that is sent/received and such.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>using the FD_READ, etc. events
    There's 2 (main?) ways of doing 'Async.' Winsock, you can use WSAAsyncSelect() and WSAEventSelect(). Which one are you using?

    >>My biggest problem is handling data that is sent/received and such.
    Hmm, what do you mean by that?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    Hey hunter howzit goin. If you recall, your early socket wrapper was the inspiration for my massive socket and thread (and soon mutex) cross platform wrapper system. Ill post a chunk of my CVS code on a website and reply to this thread with a link. It will have the bulk of the wrapper classes and a sample application (IRC Bot i think) that uses them. For practical perposes they are still a beta because some features arent completely tested, but i havent seen any major bugs in the implimentations i have used them for.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    I posted a chunk of the CVS directory in a directory visible to the web, remember its still somewhat untested, however im always looking for complaints, suggestions, and contributers.

    http://www.SocketPimps.net/cvs/

    If there are any problems with the link or you want to get in contact with me, my aim name is SocketPimp and im going to be around until about noon on thursday, then im going out of town until monday. Work, work, work, no weekend for me.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    //DISABLE GAY STL WARNINGS
    #pragma warning(disable:4786)
    LMAO that is the greatest thing I've ever seen in my life!!! Where'd you learn how to do that?

    Looking at sockets_win.h, I'm thinking it looks rather familiar. Flattered, I am Interesting idea about the UNIX vs. Win including though, and I'm going to have to look at your Thread class carefully - looks interesting.

    Just thinking though, you should probably fix up your "Usage" comment block, I believe something should be mentioned about Socket::networkInit() when the program starts, and Socket::networkShutdown() when the program ends as opposed to after you're finished using the socket
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    <just got back from trip, exhausted>

    Your wrapper worked so well i just had to customize it, tweek a few things, and start cranking out a unix version of it. because it lacks a proper header your not credited in it, however i think i mentioned you in the botclass's header file. And sockets arent useful in single threaded apps. And multi-threaded apps need mutexes and semaphores.... well it kinda snowballed a littlebit and i had to slow down and think it all out.

    Yes, my commenting isnt up to snuff. I was planning to go through everything and auctually comment on use once it was at a stage it would really be used. At the time the comments were writen I wasnt totally sure about exact implimentations and necessary functions for socket functions on both sides, so i kinda left it open.

    Auctually, right now i think networkinit is hardwired into the constructor <naughty me> so its not entirely necessary to call. Networkshutdown on the otherhand is...<checks> doesnt seem to be in the destructor, which is probablly a smart move now that i think about it.

    The thread wrapper uses Pthreads on the unix side and windows threading on the windows side. There might be a pthread implimentation for windows (vaporware). I was going to impliment ways to pause and resume and other standard thread functions but im still working on how to do that properly. Platform specific stuff is a pain sometimes. The threading class has alot of trippy void pointer trickery going on, bug me if something doesnt make sense. I think windows passed 2 void pointers, while pthreads wanted a void pointer pointer or something. Had to create a static member function to redirect the function calls.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>sockets arent useful in single threaded apps
    NetMaggotz is single-threaded, and so is ICerView and a 'chat' server/client I made

    networkinit is hardwired into the constructor <naughty me> so its not entirely necessary to call. Networkshutdown on the otherhand is...<checks> doesnt seem to be in the destructor, which is probablly a smart move now that i think about it.
    Errrr, I don't know how smart a move that is actually (I didn't notice it when I looked at the code lol). Take a look at this from MSDN:
    An application must call one WSACleanup call for every successful WSAStartup call to allow third-party DLLs to make use of a WS2_32.DLL on behalf of an application. This means, for example, that if an application calls WSAStartup three times, it must call WSACleanup three times.
    That's why I made it a separate static function

    **EDIT**
    >>because it lacks a proper header your not credited in it
    That's ok, I'm just flattered that you used it at all
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    I understand some apps can function as a single thread, however ideally there is always a thread running for the user interface, so the window doesnt hang, and one thread to process data, and one thread to recieve.

    If the WSA* functions must meet up 1 to 1 then ill have to hard code network shutdown into the destructor as well. One good hack deserves another. Ill have to finalize the interface when i finally get around to releasing the code publicly. I need to make a site for people to download all of my little projects from one of these days

    I guess it was just the voice of my old CS instructors echoing around in my head to credit and header everything properly. Its that sense of shame and pain they etch into you if you ever forget to include a little detail like that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 08:32 AM
  2. winsock help
    By Anubis208 in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2008, 07:46 AM
  3. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 08:42 AM
  4. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM