Giter VIP home page Giter VIP logo

cppsockets's Introduction

This repository contains header-only C++ classes and example code for using sockets on Linux and Windows. The classes hide the details that you don't need to worry about for many applications, providing a simple API for rapid prototyping. For example:

    TcpSocketClient client("localhost", 5000);

    client.openConnection();

    char message[100] = "Hello from client!";

    client.sendData(message, strlen(message));

    client.receiveData(message, 100);

The class header code is in the sockets folder, which you can copy/paste into your own project.

Testing on Windows

% git clone -b test_windows https://github.com/simondlevy/CppSockets
% cd CppSockets
% git clone https://github.com/simondlevy/CppSockets sockets

Then open the UdpClient and UdpServer projects, and run them in Release mode.

Testing on Linux

% git clone -b test_linux https://github.com/simondlevy/CppSockets
% cd CppSockets
% git clone https://github.com/simondlevy/CppSockets sockets
% make

Then you can run one of the two pairs of resulting test programs (udpsever/udpclient, tcpsever/tcpclient).

cppsockets's People

Contributors

simondlevy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cppsockets's Issues

Ports higher than allowed by short int?

Thanks for making this nice simple library.

I have an application which needs to receive UDP packets on port 56565, and this gives me issues because I notice that the UdpSocketSocket expects short which cannot represent port numbers that are this "high":

UdpServerSocket(const short port, const uint32_t timeoutMsec=0)

Changing this to something else (e.g. unsigned short or uint16_t does not appear to help; perhaps there is some other limitation I'm not aware of?

Issue using in MSYS2 mingw_x86_64

Throws a few errors, one of them related to uint32_t not being declared (fixed that by including cstdio in SocketCompat)
Then one relating to an attempt to pass a WCHAR pointer as an argument where an LPCSTR is expected.

Log:

$ g++ ./examples/tcpserver.cpp -I./sockets -o test
In file included from ./sockets/TcpSocket.hpp:11,
from ./sockets/TcpServerSocket.hpp:11,
from ./examples/tcpserver.cpp:10:
./sockets/SocketCompat.hpp:73:28: error: 'uint32_t' has not been declared
73 | void setUdpTimeout(uint32_t msec)
| ^~~~~~~~
./sockets/SocketCompat.hpp: In member function 'void Socket::inetPton(const char*, sockaddr_in&)':
./sockets/SocketCompat.hpp:67:31: error: cannot convert 'WCHAR*' {aka 'wchar_t*'} to 'LPCSTR' {aka
const char*'}
67 | InetPton(AF_INET, wsz, &(saddr_in.sin_addr.s_addr));
| ^~~
| |
| WCHAR* {aka wchar_t*}
In file included from ./sockets/SocketCompat.hpp:17:
C:/msys64/mingw64/include/ws2tcpip.h:407:61: note: initializing argument 2 of 'INT inet_pton(INT,
LPCSTR, PVOID)'
407 | WINSOCK_API_LINKAGE INT WSAAPI InetPtonA(INT Family, LPCSTR pStringBuf, PVOID pAddr);
| ~~~~~~~^~~~~~~~~~

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.