SDL_Function
Name
SDLNet_TCP_Open -- Connect to the host and port using a TCP connection.
Synopsis
TCPsocket SDLNet_TCP_Open(IPaddress *ip);
ip This points to the IPaddress that contains the resolved IP address and port number to use.
Description
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.
Return Value
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.
Example
To 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);
}To 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);
}
