Menu

Tree [a088dd] master /
 History

HTTPS access


File Date Author Commit
 doc 2018-08-11 Wiesiekdx Wiesiekdx [d987e3] The first working version.
 src 2018-08-22 Wiesiekdx Wiesiekdx [a088dd] Added getPublicAddress().
 .gitignore 2018-08-11 Wiesiekdx Wiesiekdx [d987e3] The first working version.
 CMakeLists.txt 2018-08-13 Wiesiekdx Wiesiekdx [66bf4d] Little corrections
 Doxyfile 2018-08-22 Wiesiekdx Wiesiekdx [a088dd] Added getPublicAddress().
 LICENSE.txt 2018-08-22 Wiesiekdx Wiesiekdx [a088dd] Added getPublicAddress().
 README.md 2018-08-22 Wiesiekdx Wiesiekdx [a088dd] Added getPublicAddress().
 tsock.pc 2018-08-22 Wiesiekdx Wiesiekdx [a088dd] Added getPublicAddress().

Read Me

# libTsock v1.2 - Network library

SourceForge project site

My site

Info

  • Version: 1.2
  • Platform: Linux (on Windows too soon)
  • Author: Wiktor Sołtys

Version 1.2 - what's new

  • Added new function: getPublicAddress() - returns the public IP address of host.

Version 1.1 - what's new

  • Added new function: getInterfaceAddr(std::string interface) - returns the IP address of the specified interface. Details in the documentation.

  • A new constructor for the 'msg' class has been added: msg(std::string text)

  • Operators << and >> added for the 'sock' class for sending and receiving chained objects msg.

Example

tsock::msg m1, m2, m3;
m1="one";
m2="two";
m3="three";

// Send it...
socket<<m1<<m2<<m3;

// And receive into the same objects
socket>>m1>>m2>>m3;

Build

cmake .

make

sudo make install

Uninstall

sudo xargs rm < install_manifest.txt

Guide

Let's create a simple server program:

// server.cpp
#include <iostream>
#include <unistd.h>     // sleep()

#include <tsock.hpp>

using namespace std;

// Client handling function. The parameter is new sock object.
void clientHandling(tsock::sock client){
    cout<<"Client connected!"<<endl;

    // Create message objects
    tsock::msg message, reply;
    message="Hello client!";

    for(int i=0; i<5; i++){
        cout<<"Sending: "<<message.str()<<endl;

        // Send message to client
        client.send(message);

        // Recv reply from client
        client.recv(reply);
        cout<<"Message from client is: "<<reply.str()<<endl;

        // Wait 1 second
        sleep(1);
    }

    // Shutdown client socket
    client.drop();
}

int main(){

    // Create server socket
    tsock::sock server;

    // Bind it
    server.bind(7878);
    cout<<"Server started! Waiting for clients..."<<endl;

    // After connecting the client, pass control to the new thread
    thread clientThread=server.accept(clientHandling);

    // Wait for the thread to end
    clientThread.join();

    // Shutdown socket
    server.drop();

    return EXIT_SUCCESS;
}

And client:

// client.cpp
#include <iostream>

#include <tsock.hpp>

using namespace std;

int main(){

    // Create socket
    tsock::sock client;

    // Connect it to the server
    bool res=client.connect(tsock::getHostByName("localhost"), 7878);
    if(!res){
        cout<<"Can not connect to the server."<<endl;
        return EXIT_FAILURE;
    }

    // Create message objects
    tsock::msg message, reply;
    reply="Hello server!";

    for(int i=0; i<5; i++){

        // Wait for message from server...
        client.recv(message);
        cout<<"Message from server is: "<<message.str()<<endl;


        cout<<"Sending: "<<reply.str()<<endl;

        // Send reply to the server
        client.send(reply);
    }

    // Shutdown socket
    client.drop();

    return EXIT_SUCCESS;
}

Compile it:

g++ server.cpp -o server -ltsock -lpthread -lcurl

g++ client.cpp -o client -ltsock -lpthread -lcurl

And run first server, after that run client. If the client program immediately ends after the start, something is wrong. Probably programs are run on different devices, in this case the destination address should be changed in the client's source code, on the IP address of the computer on which the server is running. For example:

// client.cpp

// ...

// If we want to use a domain:
client.connect(getHostByName("github.com"), 80);

// Or if we want to use an IP address:
client.connect("192.30.253.113", 80);

// ...

Licence

libTsock v1.2 - Simple network library.

Copyright (C) 2018 Wiktor Sołtys wiesiekdx@gmail.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Boston, MA 02110-1301, USA.

Author

Wiktor Sołtys

E-mail: wiesiekdx@gmail.com

Poland

Follow me on Twitter