[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.3.1 SDLNet_TCP_Open

TCPsocket SDLNet_TCP_Open(IPaddress *ip)

ip
This points to the IPaddress that contains the resolved IP address and port number to use.

Connect to the host and port contained in ip using a TCP connection.
If the host is INADDR_ANY, then only the port number is used, and a socket is created that can be used to later accept incoming TCP connections.

Returns: a valid TCPsocket on success, which indicates a successful connection has been established, or a socket has been created that is valid to accept incoming TCP connections. NULL is returned on errors, such as when it's not able to create a socket, or it cannot connect to host and/or port contained in ip.

 
// connect to localhost at port 9999 using TCP (client)
IPaddress ip;
TCPsocket tcpsock;

if(SDLNet_ResolveHost(&ip,"localhost",9999)==-1) {
    printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
    exit(1);
}

tcpsock=SDLNet_TCP_Open(&ip);
if(!tcpsock) {
    printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
    exit(2);
}

 
// create a listening TCP socket on port 9999 (server)
IPaddress ip;
TCPsocket tcpsock;

if(SDLNet_ResolveHost(&ip,NULL,9999)==-1) {
    printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
    exit(1);
}

tcpsock=SDLNet_TCP_Open(&ip);
if(!tcpsock) {
    printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
    exit(2);
}

See Also:
3.3.3 SDLNet_TCP_Accept, 3.3.2 SDLNet_TCP_Close, 4.1 IPaddress, 4.2 TCPsocket



This document was generated on November, 3 2009 using texi2html