GNet Network Library Reference Manual | |||
---|---|---|---|
<<< Previous Page | Home | Up | Next Page >>> |
TcpSocket represents an open TCP (stream) socket. Create a TcpSocket by calling tcp_socket_new or tcp_socket_port_new.
GTcpSocket* gnet_tcp_socket_connect (const gchar *hostname, gint port); |
A quick and easy GTcpSocket constructor. This connects to the specified address and port. This function does block (gnet_tcp_socket_connect_async() does not). Use this function when you're a client connecting to a server and you don't mind blocking and don't want to mess with GInetAddr's. You can get the InetAddr of the socket by calling gnet_tcp_socket_get_inetaddr().
GTcpSocketConnectAsyncID gnet_tcp_socket_connect_async (const gchar *hostname, gint port, GTcpSocketConnectAsyncFunc func, gpointer data); |
A quick and easy asynchronous GTcpSocket constructor. This connects to the specified address and port and then calls the callback with the data. Use this function when you're a client connecting to a server and you don't want to block or mess with GInetAddr's. It may call the callback before the function returns. It will call the callback if there is a failure.
hostname : | Name of host to connect to |
port : | Port to connect to |
func : | Callback function |
data : | User data passed when callback function is called. |
Returns : | ID of the connection which can be used with gnet_tcp_socket_connect_async_cancel() to cancel it; NULL on failure. |
void gnet_tcp_socket_connect_async_cancel (GTcpSocketConnectAsyncID id); |
Cancel an asynchronous connection that was started with gnet_tcp_socket_connect_async().
ID of an asynchronous connection started with gnet_tcp_socket_connect_async(). The connection can be canceled by calling gnet_tcp_socket_connect_async_cancel() with the ID.
void (*GTcpSocketConnectAsyncFunc) (GTcpSocket *socket, GInetAddr *ia, GTcpSocketConnectAsyncStatus status, gpointer data); |
Callback for gnet_tcp_socket_connect_async().
typedef enum { GTCP_SOCKET_CONNECT_ASYNC_STATUS_OK, GTCP_SOCKET_CONNECT_ASYNC_STATUS_INETADDR_ERROR, GTCP_SOCKET_CONNECT_ASYNC_STATUS_TCP_ERROR } GTcpSocketConnectAsyncStatus; |
Status for connecting via gnet_tcp_socket_connect_async(), passed by GTcpSocketConnectAsyncFunc. More errors may be added in the future, so it's best to compare against GTCP_SOCKET_CONNECT_ASYNC_STATUS_OK.
GTcpSocket* gnet_tcp_socket_new (const GInetAddr *addr); |
Connect to a specified address. Use this sort of socket when you're a client connecting to a server. This function will block to connect. SOCKS is used if SOCKS is enabled.
GTcpSocketNewAsyncID gnet_tcp_socket_new_async (const GInetAddr *addr, GTcpSocketNewAsyncFunc func, gpointer data); |
Connect to a specifed address asynchronously. When the connection is complete or there is an error, it will call the callback. It may call the callback before the function returns. It will call the callback if there is a failure. SOCKS is used if SOCKS is enabled. The SOCKS negotiation will block.
addr : | Address to connect to. |
func : | Callback function. |
data : | User data passed when callback function is called. |
Returns : | ID of the connection which can be used with gnet_tcp_socket_connect_async_cancel() to cancel it; NULL on failure. |
void gnet_tcp_socket_new_async_cancel (GTcpSocketNewAsyncID id); |
Cancel an asynchronous connection that was started with gnet_tcp_socket_new_async().
ID of an asynchronous tcp socket creation started with gnet_tcp_socket_new_async(). The creation can be canceled by calling gnet_tcp_socket_new_async_cancel() with the ID.
void (*GTcpSocketNewAsyncFunc) (GTcpSocket *socket, GTcpSocketNewAsyncStatus status, gpointer data); |
Callback for gnet_tcp_socket_new_async().
typedef enum { GTCP_SOCKET_NEW_ASYNC_STATUS_OK, GTCP_SOCKET_NEW_ASYNC_STATUS_ERROR } GTcpSocketNewAsyncStatus; |
void gnet_tcp_socket_ref (GTcpSocket *s); |
Increment the reference counter of the GTcpSocket.
void gnet_tcp_socket_unref (GTcpSocket *s); |
Remove a reference from the GTcpSocket. When reference count reaches 0, the socket is deleted.
GIOChannel* gnet_tcp_socket_get_iochannel (GTcpSocket *socket); |
Get the GIOChannel for the GTcpSocket.
For a client socket, the GIOChannel represents the data stream. Use it like you would any other GIOChannel.
For a server socket however, the GIOChannel represents incoming connections. If you can read from it, there's a connection waiting.
There is one channel for every socket. This function refs the channel before returning it. You should unref the channel when you are done with it. However, you should not close the channel - this is done when you delete the socket.
GInetAddr* gnet_tcp_socket_get_inetaddr (const GTcpSocket *socket); |
Get the address of the socket. If the socket is client socket, the address is the address of the remote host it is connected to. If the socket is a server socket, the address is the address of the local host. (Though you should use gnet_inetaddr_gethostaddr() to get the GInetAddr of the local host.)
gint gnet_tcp_socket_get_port (const GTcpSocket *socket); |
Get the port number the socket is bound to.
typedef enum { GNET_TOS_NONE, GNET_TOS_LOWDELAY, GNET_TOS_THROUGHPUT, GNET_TOS_RELIABILITY, GNET_TOS_LOWCOST } GNetTOS; |
void gnet_tcp_socket_set_tos (GTcpSocket *socket, GNetTOS tos); |
Set the type-of-service of the socket. Usually, routers don't honor this. Some systems don't support this function. If the operating system does not support it, the function does nothing.
GTcpSocket* gnet_tcp_socket_server_new (gint port); |
Create and open a new GTcpSocket with the specified port number. Use this sort of socket when you are a server and you know what the port number should be (or pass 0 if you don't care what the port is). SOCKS is used if SOCKS is enabled.
GTcpSocket* gnet_tcp_socket_server_new_interface (const GInetAddr *iface); |
Create and open a new GTcpSocket bound to the specified interface. Use this sort of socket when your are a server and have a specific address the server must be bound to. If the port number of the interface address is 0, the OS will use the interface specified but choose the port itself. If the interface address is NULL, the OS will choose both the interface and port. If the interface address was created by gnet_inetaddr_new_any(), the OS will use all interfaces. If the port number is also set, it will use that port number. SOCKS is used if SOCKS is enabled and the interface is NULL.
GTcpSocket* gnet_tcp_socket_server_accept (GTcpSocket *socket); |
Accept a connection from the socket. The socket must have been created using gnet_tcp_socket_server_new(). This function will block (use gnet_tcp_socket_server_accept_nonblock() if you don't want to block). If the socket's GIOChannel is readable, it DOES NOT mean that this function will not block.
GTcpSocket* gnet_tcp_socket_server_accept_nonblock (GTcpSocket *socket); |
Accept a connection from the socket without blocking. The socket must have been created using gnet_tcp_socket_server_new(). This function is best used with the socket's GIOChannel. If the channel is readable, then you PROBABLY have a connection. It is possible for the connection to close by the time you call this, so it may return NULL even if the channel was readable.
void gnet_tcp_socket_server_accept_async (GTcpSocket *socket, GTcpSocketAcceptFunc accept_func, gpointer user_data); |
Accept a connection from the socket asynchronously. The callback is called when a client has connection or the socket has an error.