473,799 Members | 2,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New C++ sockets library

I've created a new C++ sockets library - small, cross-platform, IPv4
and IPv6. If anyone would like to critique it, I'd appreciate it.
Doesn't support the more exotic protocols - only TCP and UDP. It also
lacks get/setsockopt and ioctl, but those may be added in the future.

There is no webpage for it yet, I plan on making one once I'm 100% sure
of everything. For now the source is available here:
http://dev.int64.org/netx.zip

PS. Look at the socket::select code. It's the only part I don't like,
but I couldn't come up with a better solution.

Jul 22 '05 #1
10 2720
>small, cross-platform, IPv4 and IPv6

Thinly wrapped socket classes like these are the first steps toward a
cross platform sockets library. Code like

sock.send("hell o", 6);

means you still have a way to go to make working with sockets easy and
safe.
If anyone would like to critique it, I'd appreciate it.
Small things I don't like are

1. c style casts instead of static_cast
2. "using namespace std" instead of "using std::string"
3. putting underscore in front of member variables
Look at the socket::select code

I would not try and use select for multiple clients. For servers think
about an interface to make it easy to use asynchronous I/O. (On windows
look at overlapped I/O, I/O completion ports and thread pooling)
Everyone wants networking in C++, so keep going.

Jul 22 '05 #2
> Thinly wrapped socket classes like these are the first steps toward a
cross platform sockets library. Code like

sock.send("hell o", 6);

means you still have a way to go to make working with sockets easy and safe.
Yea, I've been wanting to make a steam-based interface for it - but I
don't know how :) I also fear I wouldn't use it that much.
If anyone would like to critique it, I'd appreciate it.
Small things I don't like are

1. c style casts instead of static_cast
2. "using namespace std" instead of "using std::string"
3. putting underscore in front of member variables


Agreed. I come from a C background, not used to the long cast
operators. The using can easily be fixed, but the underscores are
there for a reason - Intellisense in VS.NET can get cluttered with
private methods/variables, so I use it to seperate them.
Look at the socket::select code I would not try and use select for multiple clients. For servers

think about an interface to make it easy to use asynchronous I/O. (On windows look at overlapped I/O, I/O completion ports and thread pooling)
Everyone wants networking in C++, so keep going.


Yup, but it is good in some instances. I've already been working on a
nice wrapper for I/O Completion ports.

Jul 22 '05 #3
"Yea, I've been wanting to make a steam-based interface for it - but I
don't know how :) I also fear I wouldn't use it that much."

*stream

Jul 22 '05 #4

<ph*****@gmail. com> wrote in message news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Thinly wrapped socket classes like these are the first steps toward a
cross platform sockets library. Code like

sock.send("hell o", 6);

means you still have a way to go to make working with sockets easy

and
safe.


Yea, I've been wanting to make a steam-based interface for it - but I
don't know how :) I also fear I wouldn't use it that much.

[snip]

Look at C++ stream-compatible TCP/IP sockets at
* http://sourceforge.net/projects/cpp-sockets/
* http://alexvn.freeservers.com/s1/sock.html
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #5
Sorry if this is a double post.
3. putting underscore in front of member variables
the underscores are
there for a reason - Intellisense in VS.NET can get cluttered with
private methods/variables, so I use it to seperate them


It's just that identifiers starting with underscore are reserved for
compiler use.
How about using "m_" instead? Intellisense stops working anyway as soon
as you start using lots of angle brackets.

A few other comments:

1. You throw in your socket destructor. This makes it very hard to use
safely. In particular, you won't be able to safely put it in Standard
Library containers. Use catch(...) to keep exceptions from propagating.

2. You prevent copy construction by throwing when you can catch almost
the same errors at compile time by making the copy constructor private.

3. If I use your socket I'll find a way to avoid calling
WSAStartup/WSACleanup on every construction/destruction.

Jul 22 '05 #6

Hans Malherbe wrote:
Sorry if this is a double post.
3. putting underscore in front of member variables
the underscores are
there for a reason - Intellisense in VS.NET can get cluttered with
private methods/variables, so I use it to seperate them


It's just that identifiers starting with underscore are reserved for
compiler use.
How about using "m_" instead? Intellisense stops working anyway as

soon as you start using lots of angle brackets.
Compilers use __double underscores, single underscores should be fine.

A few other comments:

1. You throw in your socket destructor. This makes it very hard to use safely. In particular, you won't be able to safely put it in Standard
Library containers. Use catch(...) to keep exceptions from propagating.

I'd rather throw an exception than leave a socket open without letting
the developer know. It is non-copyable, so usage in STL containers
(without smart pointers) is out of the question anyway.

2. You prevent copy construction by throwing when you can catch almost the same errors at compile time by making the copy constructor private.

The copy contructor _is_ private, the throw should never happen.

3. If I use your socket I'll find a way to avoid calling
WSAStartup/WSACleanup on every construction/destruction.


If built as a DLL, WSAStartup/WSACleanup is only called once. I put it
in the ctor/dtor to avoid apps needing to call an init()/cleanup()
function when using it as a static library. Though, now that I think
of it, this could probably be accomplished with a global class that
does this on ctor/dtor.

Jul 22 '05 #7
[ ... ]
It's just that identifiers starting with underscore are reserved for compiler use.

[ ... ]
Compilers use __double underscores, single underscores should be

fine.

A leading underscore followed by either another another underscore OR a
capital letter is reserved. A leading underscore followed by something
else allowable sometimes (e.g. as a class member) but not others (e.g.
as global variable). IMO, trying to memorize all the details of when
it's allowed and when it's not is a waste of time and likely to lead to
problems anyway -- it's better to just treat all leading undrscores as
reserved.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 22 '05 #8
>I'd rather throw an exception than leave a socket open without letting
the developer know. It is non-copyable, so usage in STL containers
(without smart pointers) is out of the question anyway.
Yes, bad example. What was I thinking?
Here's a better one:
In a function I declare a socket on the stack. Somewhere in the
function I decide to exit the function with an exception. If there's
something wrong with my socket at this time and it cannot close, my
program terminates. OUCH.

I know what you're thinking...
Just catch(...) in your destructor, check for uncaught exceptions and
rethrow if clear.
No, no, no!

Go look in c.l.c++.* and you'll find all the reasons why you have to
make absolutely sure exceptions never wander aimlessly out of your
destructors.
The copy contructor _is_ private, the throw should never happen.


Oops, sorry. You're right. Your version is better because the compiler
won't complain if a member function do a copy.
You might want to do the same for copy assignment.

Jul 22 '05 #9
Hans Malherbe wrote:
I'd rather throw an exception than leave a socket open without lettingthe developer know. It is non-copyable, so usage in STL containers
(without smart pointers) is out of the question anyway.
Yes, bad example. What was I thinking?
Here's a better one:
In a function I declare a socket on the stack. Somewhere in the
function I decide to exit the function with an exception. If there's
something wrong with my socket at this time and it cannot close, my
program terminates. OUCH.


Hmm.. I think we'll have to agree to disagree here. I'll make it
non-throwing with a #define.
I know what you're thinking...
Just catch(...) in your destructor, check for uncaught exceptions and
rethrow if clear.
No, no, no!

Go look in c.l.c++.* and you'll find all the reasons why you have to
make absolutely sure exceptions never wander aimlessly out of your
destructors.
The copy contructor _is_ private, the throw should never happen.
Oops, sorry. You're right. Your version is better because the

compiler won't complain if a member function do a copy.
You might want to do the same for copy assignment.


Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3086
by: Snyke | last post by:
I'm currently looking for a good C++ Wrapper for sockets. I've already tried http://www.libsockets.net/ but I have some problems compiling it on Windows. Does anybody know a smaller (this library is pretty heavy after all) sockets wrapper?
0
1314
by: grymse | last post by:
This is a cross platform wrapper library of the berkeley sockets C API that has been around for some time now. URL: http://www.alhem.net/Sockets/ Some details: Features socket class implementations of UDP, TCP, SSL, HTTP/HTTPS, Http(Get, Put, Post). Singlethreaded concurrent operation. Tutorial (http://www.alhem.net/Sockets/tutorial) with downloadable source
5
1632
by: Aaron Gray | last post by:
Is there an comprehensive C++ library for sockets that works both on Unix/Linux and on Windows ? Many thanks in advance, Aaron
2
5154
by: Nadav | last post by:
Hi, I am about to write a performance critical application and I wonder what is the best way to implement it... Should I use the standart Win32 winsock DLL or should I rather use the new managed System.Net.Sockets library... from bench-marking tests I have done I have reached to the following conclusion: Test description: Bougth of the projects used a single IO Completion port for communication ( the managed proj used NetworkStream that use...
1
2309
by: Chad | last post by:
I have developed a Sockets application using .NET Sockets. The only problem I have with my application is that it will sometimes lose part of the TCP/IP Message. The message I am sending is 963 Bytes long. My buffer is set at 4096 Bytes. The application will always receive the entire message when I test it local. If I put the server piece on another machine and connect to it with my TeraTerm client it will randomly lose half of the...
2
1235
by: Jm | last post by:
Hi All Im trying to get a bit of background info on .net.sockets for an app that im upgrading to be .net. Under vb6 i used the winsock control and would start it listening and it would call an event once the connection attempt is made to it and you would do all you had to do, close the connection and reopen. I cant seem to find out how this works with .net.sockets. The code i have seen seems to always talk about listening only the once...
3
1697
by: call_me_anything | last post by:
Hi, We had a library linked dynamically in our 32 bit mode. Now due to capacity issues, we are planning to move on to 64 bit but the library is still 32 bit and we cannot make it 64 bit without a mammoth effort. So we decided to use IPC via sockets such that the library calls are run on a 32 bit machine while our application runs on a 64 bit machine. But that is slowing down the things by a factor of say 100.
3
1477
by: Daniel | last post by:
Hello, I can't seem to get my sockets code to work right. Here is what I have inside my RequestHandler handle() function: total_data= data = True logger_server.debug(self.__class__.__name__ + ' set data = True')
0
9686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10250
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10026
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.