Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 8th, 2007, 12:28 AM   #1
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 372
Rep Power: 0 King is an unknown quantity at this point
Sockets Lib

I am in the middle of creating a cross platform (Win & Lin) sockets library and I will probably have a few questions over the next couple days. The first one is how should I handle errors. I will have functions such as send, recv, connect, accept, listen, and a bunch more. Most of these functions call the corresponding WinSock or Posix functions which all return an error code to say if it worked or not. In Windows you can call WSAGetLastError() (there is also a similar function in Linux) to get the string of the last socket error from the sockets. So I was simply going to return the error code from these functions and maybe provide a method that returns the last error string if they wish to call it. Does this seem like the right thing to do? Or should I maybe throw exceptions from my classes when an error happens? I am not quite sure how I should go about this.
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Apr 8th, 2007, 1:38 AM   #2
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,010
Rep Power: 5 lectricpharaoh will become famous soon enough
My experience with sockets programming is very limited, but from what I've read, sockets in Winsock closely parallel *nix sockets, from a programming point of view. If this is indeed the case, it seems that for portability, there is not a whole lot to be gained from your library, if all you plan to do is thunk down to the corresponding functions.

However, if you plan to add functionality or ease of use through a class library, it could prove well worth the effort. For example, you could inherit from iostream, and treat sockets as regular streams.

Back to your original question regarding error codes, I'd suggest mapping them separately for Winsock and *nix. After all, there's no guarantee that error code 7 means the same thing in both environments, so you can map them to a 'normalized' set of error codes, then (if you so choose) map those to a set of descriptive error strings.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Apr 8th, 2007, 8:01 AM   #3
l2u
Newbie
 
Join Date: Mar 2007
Posts: 24
Rep Power: 0 l2u is on a distinguished road
You should go for socket wrapper.. It will be the best and easiest way to make your program portable and efficient.

I highly recomment asio socket wrapper (also available in boost).
l2u is offline   Reply With Quote
Old Apr 8th, 2007, 11:16 AM   #4
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 372
Rep Power: 0 King is an unknown quantity at this point
Its a school project actually. We have to create a cross platform sockets library and then create a music program with that library. I have both the Linux Windows sockets coded, the library pretty much just wraps a lot of the functions of WinSock and the Posix functions, simplifying the calls for the user (nothing spectacular here). I just have to add the error handling and bridge it together. So any thoughts with the error handling (I might do what lectricpharaoh has suggested) or how to pull the 2 libraries together so when I instantiate my object it news the appropriate object would be great. Thanks.
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Apr 8th, 2007, 11:50 AM   #5
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
Actually I find almost always that using a socket library is very effective, usually because you can handle a lot more of the higher level coding with sockets in the library, such as using pluggable factories to handle the messages that will be sent from client to server.

Additionally using a library helps prevent copying an pasting code, packet structures can be defined in the library, and linked by both the client and the server.

Windows is very close to UNIX, since they both support BSD style sockets. You might want to try consider writing a few exception classes and/or a function which maps exceptions or error numbers into actual error strings that can be used within your application. I usually do this anyway since the Windows error string isn't very usefull, and I don't think Linux provides such a function (well that I have ever used anyway).

Looks at the TcpListener and TcpClient classes used in C#, they might give you some good insight to how to design the library, and I suspect error handling will just come naturally once you get an idea as to how those classes work.
__________________
Clifford Matthew Roche <geek@cliffordroche.com>
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Apr 10th, 2007, 12:16 AM   #6
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 372
Rep Power: 0 King is an unknown quantity at this point
I have completed my first iteration of the windows portion of my library. I have attached a zip file (didn't feel like posting a couple hundred lines) of the class with a sample client and server main. If you guys could take a close look at the send and receive functions that would be great; they work, but I don't know if I set the buffer sizes properly and all that stuff. If you want me to post the code in the thread instead of a zip file let me know. It's not that big so it shouldn't take a long time to skim through and have a look. Any comments on this at all are welcome. Thanks.
Attached Files
File Type: zip WinSockClass.zip (10.4 KB, 7 views)
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Apr 10th, 2007, 6:59 AM   #7
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
Hi King, I haven't looked at your code but thought of something which may be of interest to you.

The library libtorrent by Arvid Norberg currently uses ASIO, but earlier versions used his own wrapper code around berkeley sockets and winsock. I believe this revision of socket.hpp would be worth looking at, or alternatively download the release of v 0.9.1. Hope that proves helpful.
__________________
Visit my website BinaryNotions.
Eoin is offline   Reply With Quote
Old Apr 10th, 2007, 7:17 AM   #8
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
They are vary similar like its been said before. The only difference I found in my early days of sockets programming which didn't involve trying to control things down application layer such as multicasting was the difference in fd_set struct and ioctl instead of ioctlsocket(windows). if you set a macro for them and use FD_* macros to handle fd_set, they should be alright. btw, the platform sdk for windows (if u have it) explains these functions really well. I also remember coming across a really good tutorial in msdn.
__________________
Spread your wings and fly! Chicken!
rsnd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mulitple Sockets or multiple connections? Booooze Software Design and Algorithms 4 Feb 3rd, 2007 1:49 AM
Raw sockets in python? ShadowAssasin Python 7 Aug 25th, 2006 3:28 PM
using sockets to connect between ms and *nix linuxpimp20 C 8 Sep 30th, 2005 8:23 AM
help with sockets, having a client recieve data as well as send. cypherkronis Python 7 Jul 1st, 2005 5:59 PM
Windows sockets programming davidsiaw C 1 Jun 15th, 2005 6:17 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:24 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC