qLibc
qsocket.c File Reference

Socket dandling 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 the 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 hostname to sockaddr_in structure.
 
char * qsocket_get_localaddr (char *buf, size_t bufsize)
 Return local IP address.
 

Detailed Description

Socket dandling 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 the remote host and port.

Parameters
hostnameremote hostname
portremote port
timeoutmswait timeout milliseconds. if set to negative value, wait indefinitely.
Returns
the new socket descriptor, or -1 in case of invalid hostname, -2 in case of socket creation failure, -3 in case of connection failure.

Definition at line 65 of file qsocket.c.

◆ qsocket_close()

bool qsocket_close ( int  sockfd,
int  timeoutms 
)

Close socket.

Parameters
sockfdsocket descriptor
timeoutmsif timeoutms >= 0, shut down write connection first then wait and throw out input stream data. set to -1 to close socket immediately.
Returns
true on success, or false if an error occurred.

Definition at line 109 of file qsocket.c.

◆ qsocket_get_addr()

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

Convert hostname to sockaddr_in structure.

Parameters
addrsockaddr_in structure pointer
hostnameIP string address or hostname
portport number
Returns
true if successful, otherwise returns false.

Definition at line 135 of file qsocket.c.

◆ qsocket_get_localaddr()

char * qsocket_get_localaddr ( char *  buf,
size_t  bufsize 
)

Return local IP address.

Returns
malloced string pointer which contains IP address string if successful, otherwise returns NULL

Definition at line 156 of file qsocket.c.