Component tcpconnect
Component index
Section index

Component tcpconnect


Search

Type

Function

Summary

Manage a single TCP client session

Description

It establish the connection, calls its init functag when successful and process the data received line by line. Supports both TCP and unix domain sockets.

void end (TCPCONNECT &info)
void fail (TCPCONNECT_INFO &info)
void init (bool &end, TCPCONNECT_INFO &info)
void oneline (const char *line, bool &end, TCPCONNECT_INFO &info)
void time_out (bool &end, TCPCONNECT &info)

Prototypes

int tcpconnect (const char *host, const char *port, int timeout)

Manage a TCP connection (or a unix domain socket). If host is "unix:", the port is the unix domain socket path. Return -1 if any error

Functags

void end (TCPCONNECT &info) Optional

Called when the connection ends

void fail (TCPCONNECT_INFO &info) Optional

Called when the connection failed

void init (bool &end, TCPCONNECT_INFO &info) Optional

Called when the connection is established

You generally used this to send the first request and do various initialisation.

void oneline (const char *line, bool &end, TCPCONNECT_INFO &info) Mandatory

Called when one complete line is received

This assumes that the protocol is line oriented (ASCII).

void time_out (bool &end, TCPCONNECT &info) Optional

Is called when the specified timeout has ellapsed

No message was received for some time. The timeout is specified as an argument of the tcpconnect function.

Fhelpers

int send (const char *s)

Send a string to the server

Return the number of bytes sent or -1 if any error. This method may trigger the end() functag if the server has closed the connection.

int send (const char *buf, int len)

Send some characters to the server

Return the number of bytes sent or -1 if any error. This method may trigger the end() functag if the server has closed the connection.

int sendf (const char *ctl, ...)

Send some bytes formatted using a printf syntax

Return the number of bytes sent or -1 if any error. This method may trigger the end() functag if the server has closed the connection.

void set_timeout (int seconds)

Set the timeout (seconds) used for the next network operation

At connect time, a timeout leads to the fail functag. Later, the time_out functag is called and the application decides if it wants to end the session.

void set_timeout (int seconds, int usec)

Set the timeout (seconds,micro-seconds) used for the next network operation

At connect time, a timeout leads to the fail functag. Later, the time_out functag is called and the application decides if it wants to end the session.

void settcpnodelay (bool on)

Control the TCP_NODELAY flag of the connection

Normally, the OS sends packets only when the transmit buffer is full, after some delay, or when the program waits for the other side. This is efficient network wise. Sometime you expect the data to leave immediatly (when you call send()). This helper lets you set the flag to on or off. It is off by default