473,889 Members | 1,558 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 2728
>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
3089
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
1323
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
1637
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
5157
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
2316
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
1239
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
1702
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
1481
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
9803
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10887
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
10439
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...
0
9598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7991
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
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5825
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4642
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
4248
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.