TcpConnect

#include <lthread_cpp/socket.h>
Socket TcpConnect(const std::string& host_or_ip, short int port, int timeout_ms=1000)

Connects to a remote host.

Parameters:
  • const std::string& host_or_ip – Host to connect to.
  • short port – Tcp port.
  • int timeout_ms(1000) – Milliseconds to wait connecting.
Returns:

Socket for the new connection

Throws:

SocketException on socket failure.

Examples

#include <lthread_cpp/lthread.h>
#include <lthread_cpp/socket.h>

using namespace lthread_cpp;
using namespace lthread_cpp::net;

void Run()
{
 Socket s = TcpConnect("127.0.0.1", 80);
 s.Send("GET / HTTP/1.1\r\n\r\n");

 char response[1024];
 s.Recv(response, 1024, 0);
 s.Send("Cool!");
 // s closes as it goes out of scope
}

int main()
{
  Lthread t{&Run};
  t.Detach();
  Lthread::Run();
}
cc -std=c++11 test.cc -o test -llthread_cpp -llthread -lpthread -lstdc++ && ./test