![]() |
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.
|
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. |
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). |
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.
|
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. |
1 Attachment(s)
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.
|
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. |
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.
|
| All times are GMT -5. The time now is 2:06 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC