qLibc
qsocket.c File Reference

Socket handling APIs. More...

Go to the source code of this file.

Functions

int qsocket_open (const char *hostname, int port, int timeoutms)
 Create a TCP socket for a remote host and port.
bool qsocket_close (int sockfd, int timeoutms)
 Close socket.
bool qsocket_get_addr (struct sockaddr_in *addr, const char *hostname, int port)
 Convert a hostname to a sockaddr_in structure.
char * qsocket_get_localaddr (char *buf, size_t bufsize)
 Get the local IP address.

Detailed Description

Socket handling APIs.

Definition in file qsocket.c.

Function Documentation

◆ qsocket_open()

int qsocket_open ( const char * hostname,
int port,
int timeoutms )

Create a TCP socket for a remote host and port.

Parameters
hostnameremote hostname
portremote port
timeoutmswait timeout in milliseconds. If negative, wait forever.
Returns
the new socket descriptor, or: -1 if the hostname is invalid -2 if socket creation fails -3 if the connection fails

Definition at line 64 of file qsocket.c.

◆ qsocket_close()

bool qsocket_close ( int sockfd,
int timeoutms )

Close socket.

Parameters
sockfdsocket descriptor
timeoutmsif timeoutms >= 0, shut down the write side first, then wait and discard incoming data. Set to -1 to close the socket immediately.
Returns
true on success, or false if an error occurred.

Definition at line 108 of file qsocket.c.

◆ qsocket_get_addr()

bool qsocket_get_addr ( struct sockaddr_in * addr,
const char * hostname,
int port )

Convert a hostname to a sockaddr_in structure.

Parameters
addrsockaddr_in output pointer
hostnameIP address string or hostname
portport number
Returns
true on success, otherwise false.

Definition at line 134 of file qsocket.c.

◆ qsocket_get_localaddr()

char * qsocket_get_localaddr ( char * buf,
size_t bufsize )

Get the local IP address.

Returns
pointer to buf on success, or NULL on failure.

Definition at line 154 of file qsocket.c.