Game Development Reference
In-Depth Information
Return Value
The socket() function returns a valid handle for a socket if one was created or
INVALID_SOCKET if there was some kind of error.
Here
'
s an example of how to create a TCP socket handle:
SOCKET sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if ((sock == INVALID_SOCKET)
{
// handle error!
}
setsockopt()
Now that you have a socket handle, you can decide how you ' d like the socket to act
when it is open. You do this by setting the socket options through a function called
setsockopt() . There is a wide variety of options, and I
m happy to show you some
common ones, specifically the ones used in the client/server code in this chapter.
Make sure you look at the complete sockets documentation for socket options. I
'
'
m
only scratching the surface here.
int setsockopt (
SOCKET socket,
int level,
int optionName,
const char* optionValue,
int optLen );
Parameters :
n Socket: A valid socket handle.
n Level: Either SOL_SOCKET or IPPROTO_TCP , depending on the option chosen.
n Option Name: The identifier of the socket option you want to set.
n Option Value: The address of the new value for the option. For Boolean values,
you should send in a 4-byte integer set to either 1 or 0.
n Option Length: The length in bytes of the option value.
Return Value:
Returns zero if the option was set or SOCKET_ERROR if there was an error.
Here are some examples of setting socket options:
int value = 1;
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&value, sizeof(value));
Search WWH ::




Custom Search