Update: Changed to version 2011.10.13 due to a bug in previous version in the WriteAll function (thx to . at the PL side of this blog for pointing it out).
Download: NetSock-2011.10.13.zip (5kb, src C++)
License : Apache License, Version 2.0 (open source)
I'm using this library both on Windows and on GNU/Linux. Maybe it will (can) work also on other similar systems (*nix family, etc) - not sure, I haven't checked.
Features:
- basic support for TCP (Connect, Listen, ListenAll, Accept, Read, Write, WriteAll, etc)
- basic support for UDP (WriteUDP, BroadcastUDP, ReadUDP, etc)
- basic support for asynchronous/non-blocking sockets (SetMode, WriteAll)
- InitNetworking does WinSock initialization for you
- host name resolving support in Connect/etc
However, NetSock is not fit for projects that need to use the network in a more advance way (like setting up other socket options, using different socket protocols like IrDA, etc).
Also, I didn't plan to create an universal library; on the contrary, the features were added rather organically (i.e. if I needed certain feature, I've added it to the lib), so some things might be missing, other might be slightly irritating (like using "unsigned char *" in Write/Read arguments; this will be fixed in the unspecified future).
Nevertheless, I hope someone will find it useful (e.g. for small tools or prototyping).
An example of usage:
// Windows : g++ test.cpp NetSock.cpp -lws2_32
// GNU/Linux: g++ test.cpp
#include <stdio.h>
#include "NetSock.h"
int main() {
NetSock::InitNetworking(); // Initialize WinSock
NetSock s;
int ret;
unsigned char buffer[8] = {0};
if (!s.Connect("127.0.0.1", 1333))
return 1; // Some error handling.
// Write some ASCII string.
ret = s.Write((unsigned char*)"asdf", 4);
if (ret != 4)
return 2; // Some error handling.
// Read some ASCII string.
ret = s.Read(buffer, sizeof(buffer) - 1);
if (ret <= 0)
return 3; // Some error handling.
// Write out the string.
puts((char*)buffer);
s.Disconnect();
return 0;
}
And that's that.
Add a comment: