SDL Net: Jonathan Atkins
SDL Net: Jonathan Atkins
3 November 2009
Jonathan Atkins
Table of Contents
1
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.1
2.2
Includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Compiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.1
General. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.1.1 SDLNet Linked Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.1.2 SDLNet Init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.1.3 SDLNet Quit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.1.4 SDLNet GetError . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.1.5 SDLNet Write16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1.6 SDLNet Write32 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.1.7 SDLNet Read16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.1.8 SDLNet Read32 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.2 Name Resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.2.1 SDLNet ResolveHost. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.2.2 SDLNet ResolveIP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.3 TCP Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3.1 SDLNet TCP Open . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3.2 SDLNet TCP Close . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.3.3 SDLNet TCP Accept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.3.4 SDLNet TCP GetPeerAddress . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.3.5 SDLNet TCP Send . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.3.6 SDLNet TCP Recv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.4 UDP Sockets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.4.1 SDLNet UDP Open . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.4.2 SDLNet UDP Close . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.4.3 SDLNet UDP Bind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.4.4 SDLNet UDP Unbind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.4.5 SDLNet UDP GetPeerAddress . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.4.6 SDLNet UDP Send . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.4.7 SDLNet UDP Recv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.4.8 SDLNet UDP SendV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.4.9 SDLNet UDP RecvV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
3.5 UDP Packets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.5.1 SDLNet AllocPacket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.5.2 SDLNet ResizePacket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.5.3 SDLNet FreePacket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.5.4 SDLNet AllocPacketV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.5.5 SDLNet FreePacketV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
ii
3.6
Socket Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.6.1 SDLNet AllocSocketSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.6.2 SDLNet FreeSocketSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.6.3 SDLNet AddSocket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.6.4 SDLNet DelSocket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.6.5 SDLNet CheckSockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.6.6 SDLNet SocketReady . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
45
46
47
48
49
50
51
Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4.1
4.2
4.3
4.4
4.5
4.6
IPaddress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
TCPsocket. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
UDPsocket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
UDPpacket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SDLNet SocketSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
SDLNet GenericSocket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
54
55
56
57
58
59
Defines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Chapter 1: Overview
1 Overview
A Little Bit About Me
I am currently, as I write this document, a programmer for Raytheon. There I do all sorts
of communications, network, GUI, and other general programming tasks in C/C++ on the
Solaris and sometimes Linux Operating Systems. I have been programming network code
at work and in my free time for about 8 years. I find there is always more to learn about
network coding, but the rewards for making something comunicate over what can only be
called the most chaotic channel of information transfer are sometimes to much fun to ignore.
I have coded only a few things in SDL net that I would call complete projects. However
SDL net does make network coding easier and more portable than anything I could code
using just plain BSD sockets. I was happy that I could finally base my code on something
that is portable and small enough to be unintrusive. SDL net does sometimes seem to
oversimplify some things, but in the end I found that if I couldnt do it in SDL net then
perhaps Im not doing something that is worthwhile. Of course I have and will continue to
do things that are not so worthwhile perhaps, using SDL net. Like a Web Server library,
and an IRC client library. But I still enjoy making them anyway! So if you are making a
game, or doing a school project, or even making a no-fun application, SDL net is a viable
choice in my opinion for almost any of these tasks. If you are interested in multicast and
non-TCP/IPv4 networking then SDL net is not the right thing for you. Everyone else,
please make more network games that are fun to play, and portable enough that more
people are out there to play with. I, meanwhile, will continue writting documentation and
applications and games that will likely become vapourware before they even see the light
of day, but Ill be having fun the whole time.
Feel free to contact me: [email protected]
I am also usually on IRC at irc.freenode.net in the #SDL channel as LIM
Chapter 1: Overview
Chapter 1: Overview
And heres a bit of humor for you to look at as you deal with networking terms.
2 Getting Started
This assumes you have gotten SDL net and installed it on your system. SDL net has an
INSTALL document in the source distribution to help you get it compiled and installed.
Generally, installation consists of:
./configure
make
make install
You may also want to look at some demonstration code which may be downloaded from:
http://www.jonatkins.org/SDL_net/
2.1 Includes
To use SDL net functions in a C/C++ source code file, you must use the SDL net.h include
file:
#include "SDL_net.h"
2.2 Compiling
To link with SDL net you should use sdl-config to get the required SDL compilation options.
After that, compiling with SDL net is quite easy.
Note: Some systems may not have the SDL net library and include file in the same place
as the SDL library and includes are located, in that case you will need to add more -I and
-L paths to these command lines.
Chapter 3: Functions
3 Functions
These are the functions in the SDL net API.
Chapter 3: Functions
3.1 General
The basic API startup, cleanup and versioning methods, along with a few network byte
order data helpers.
Chapter 3: Functions
SDL_version compile_version;
const SDL_version *link_version=SDLNet_Linked_Version();
SDL_NET_VERSION(&compile_version);
printf("compiled with SDL_net version: %d.%d.%d\n",
compile_version.major,
compile_version.minor,
compile_version.patch);
printf("running with SDL_net version: %d.%d.%d\n",
link_version->major,
link_version->minor,
link_version->patch);
See Also:
Section 3.1.2 [SDLNet Init], page 10
Chapter 3: Functions
10
if(SDL_Init(0)==-1) {
printf("SDL_Init: %s\n", SDL_GetError());
exit(1);
}
if(SDLNet_Init()==-1) {
printf("SDLNet_Init: %s\n", SDLNet_GetError());
exit(2);
}
See Also:
Section 3.1.3 [SDLNet Quit], page 11
Chapter 3: Functions
11
SDLNet_Quit();
// you could SDL_Quit(); here...or not.
See Also:
Section 3.1.2 [SDLNet Init], page 10
Chapter 3: Functions
12
Chapter 3: Functions
13
area
Put the 16bit (a short on 32bit systems) value into the data buffer area in network byte
order. This helps avoid byte order differences between two systems that are talking over
the network. The value can be a signed number, the unsigned parameter type doesnt affect
the data. The area pointer need not be at the beginning of a buffer, but must have at least
2 bytes of space left, including the byte currently pointed at.
// put my number into a data buffer to prepare for sending to a remote host
char data[1024];
Sint16 number=12345;
SDLNet_Write16((Uint16)number,data);
See Also:
Section 3.1.7 [SDLNet Read16], page 15, Section 3.1.6 [SDLNet Write32], page 14
Chapter 3: Functions
14
area
Put the 32bit (a long on 32bit systems) value into the data buffer area in network byte
order. This helps avoid byte order differences between two systems that are talking over
the network. The value can be a signed number, the unsigned parameter type doesnt affect
the data. The area pointer need not be at the beginning of a buffer, but must have at least
4 bytes of space left, including the byte currently pointed at.
// put my number into a data buffer to prepare for sending to a remote host
char data[1024];
Uint32 number=0xDEADBEEF;
SDLNet_Write32(number,data);
See Also:
Section 3.1.8 [SDLNet Read32], page 16, Section 3.1.5 [SDLNet Write16], page 13
Chapter 3: Functions
15
The pointer into a data buffer, at which to get the number from
Get a 16bit (a short on 32bit systems) value from the data buffer area which is in network
byte order. This helps avoid byte order differences between two systems that are talking
over the network. The returned value can be a signed number, the unsigned parameter type
doesnt affect the data. The area pointer need not be at the beginning of a buffer, but must
have at least 2 bytes of space left, including the byte currently pointed at.
See Also:
Section 3.1.5 [SDLNet Write16], page 13, Section 3.1.8 [SDLNet Read32], page 16
Chapter 3: Functions
16
The pointer into a data buffer, at which to get the number from
Get a 32bit (a long on 32bit systems) value from the data buffer area which is in network
byte order. This helps avoid byte order differences between two systems that are talking
over the network. The returned value can be a signed number, the unsigned parameter type
doesnt affect the data. The area pointer need not be at the beginning of a buffer, but must
have at least 4 bytes of space left, including the byte currently pointed at.
See Also:
Section 3.1.6 [SDLNet Write32], page 14, Section 3.1.7 [SDLNet Read16], page 15
Chapter 3: Functions
17
Chapter 3: Functions
18
This points to the IPaddress that will be filled in. It doesnt need to be set
before calling this, but it must be allocated in memory.
host
port
For connecting to a server, this is the the servers listening port number.
For becoming a server, this is the port to listen on.
If you are just doing Domain Name Resolution functions, this can be 0.
Resolve the string host, and fill in the IPaddress pointed to by address with the resolved
IP and the port number passed in through port.
This is the best way to fill in the IPaddress struct for later use. This function does not
actually open any sockets, it is used to prepare the arguments for the socket opening functions.
WARNING: this function will put the host and port into Network Byte Order into the
address fields, so make sure you pass in the data in your hosts byte order. (normally not
an issue)
Returns: 0 on success. -1 on errors, plus address.host will be INADDR NONE. An error
would likely be that the address could not be resolved.
For a server listening on all interfaces, on port 1234:
See Also:
Section 3.2.2 [SDLNet ResolveIP], page 19, Section 4.1 [IPaddress], page 54
Chapter 3: Functions
19
This points to the IPaddress that will be resolved to a host name. The address>port is ignored.
Resolve the IPv4 numeric address in address->host, and return the hostname as a string.
Returns: a valid char pointer (string) on success. the returned hostname will have host and
domain, as in host.domain.ext. NULL is returned on errors, such as when its not able
to resolve the host name. The returned pointer is not to be freed. Each time you call this
function the previous pointers data will change to the new value, so you may have to copy
it into a local buffer to keep it around longer.
See Also:
Section 3.2.1 [SDLNet ResolveHost], page 18, Section 4.1 [IPaddress], page 54
Chapter 3: Functions
20
Chapter 3: Functions
21
This points to the IPaddress that contains the resolved IP address and port
number to use.
Chapter 3: Functions
22
See Also:
Section 3.3.3 [SDLNet TCP Accept], page 24, Section 3.3.2 [SDLNet TCP Close], page 23,
Section 4.1 [IPaddress], page 54, Section 4.2 [TCPsocket], page 55
Chapter 3: Functions
23
See Also:
Section 3.3.1 [SDLNet TCP Open], page 21, Section 4.2 [TCPsocket], page 55
Chapter 3: Functions
24
See Also:
Section 3.3.1 [SDLNet TCP Open], page 21, Section 3.3.4 [SDLNet TCP GetPeerAddress],
page 25, Section 3.3.2 [SDLNet TCP Close], page 23, Section 4.2 [TCPsocket], page 55
Chapter 3: Functions
25
Get the Peers (the other side of the connection, the remote side, not the local side) IP
address and port number.
Returns: an IPaddress. NULL is returned on errors, or when sock is a server socket.
See Also:
Section 3.3.1 [SDLNet TCP Open], page 21, Section 3.3.3 [SDLNet TCP Accept], page 24,
Section 4.1 [IPaddress], page 54, Section 4.2 [TCPsocket], page 55
Chapter 3: Functions
26
data
len
See Also:
Section 3.3.6 [SDLNet TCP Recv], page 27, Section 3.3.3 [SDLNet TCP Accept], page 24,
Section 3.3.1 [SDLNet TCP Open], page 21, Section 3.3.4 [SDLNet TCP GetPeerAddress],
page 25, Section 3.3.2 [SDLNet TCP Close], page 23, Section 4.2 [TCPsocket], page 55
Chapter 3: Functions
27
data
This is a pointer to the buffer that receives the data from sock.
maxlen
This is the maximum length (in bytes) that will be read into data.
Receive data of exactly length maxlen bytes from the socket sock, into the memory pointed
to by data.
This routine is not used for server sockets.
Unless there is an error, or the connection is closed, the buffer will read maxlen bytes. If
you read more than is sent from the other end, then it will wait until the full requested
length is sent, or until the connection is closed from the other end.
You may have to read 1 byte at a time for some applications, for instance, text applications
where blocks of text are sent, but you want to read line by line. In that case you may
want to find the newline characters yourself to break the lines up, instead of reading some
inordinate amount of text which may contain many lines, or not even a full line of text.
Returns: the number of bytes received. If the number returned is less than or equal to zero,
then an error occured, or the remote host has closed the connection.
See Also:
Section 3.3.5 [SDLNet TCP Send], page 26, Section 3.3.3 [SDLNet TCP Accept], page 24,
Section 3.3.1 [SDLNet TCP Open], page 21, Section 3.3.4 [SDLNet TCP GetPeerAddress],
page 25, Section 3.3.2 [SDLNet TCP Close], page 23, Section 4.2 [TCPsocket], page 55
Chapter 3: Functions
28
Chapter 3: Functions
29
This is the port number (in native byte order) on which to receive UDP packets.
Most servers will want to use a known port number here so that clients can
easily communicate with the server. This can also be zero, which then opens
an anonymous unused port number, to most likely be used to send UDP packets
from.
See Also:
Section 3.4.2 [SDLNet UDP Close], page 30, Section 4.3 [UDPsocket], page 56
Chapter 3: Functions
30
See Also:
Section 3.4.1 [SDLNet UDP Open], page 29, Section 4.3 [UDPsocket], page 56
Chapter 3: Functions
31
channel
address
The resolved IPaddress to assign to the sockets channel. The host and port
are both used.
It is not helpful to bind 0.0.0.0 to a channel.
See Also:
Section 3.4.4 [SDLNet UDP Unbind], page 32, Section 3.4.5 [SDLNet UDP GetPeerAddress],
page 33, Section 3.4.1 [SDLNet UDP Open], page 29, Section 4.1 [IPaddress], page 54,
Section 4.3 [UDPsocket], page 56
Chapter 3: Functions
32
channel
This removes all previously assigned (bound) addresses from a socket channel.
After this you may bind new addresses to the socket channel.
Returns: nothing, this always succeeds.
See Also:
Section 3.4.3 [SDLNet UDP Bind], page 31, Section 3.4.2 [SDLNet UDP Close], page 30,
Section 4.3 [UDPsocket], page 56
Chapter 3: Functions
33
channel
The channel to get the primary address from in the socket. This may also be
-1 to get the port which this socket is bound to on the local computer.
Get the primary address assigned to this channel. Only the first bound address is returned.
When channel is -1, get the port that this socket is bound to on the local computer, this
only means something if you opened the socket with a specific port number.
Do not free the returned IPaddress pointer.
Returns: a pointer to an IPaddress. NULL is returned for unbound channels and on any
errors.
See Also:
Section 3.4.3 [SDLNet UDP Bind], page 31, Section 3.4.4 [SDLNet UDP Unbind], page 32,
Section 4.1 [IPaddress], page 54, Section 4.3 [UDPsocket], page 56
Chapter 3: Functions
34
sock
A valid UDPsocket.
channel
packet
Send packet using the specified socket sock, use ing the specified channel or else the packets
address.
If channel is not -1 then the packet is sent to all the socket channels bound addresses. If
socket socks channel is not bound to any destinations, then the packet is not sent at all!
If the channel is -1, then the packets address is used as the destination.
Dont forget to set the length of the packet in the len element of the packet you are
sending! Note: the packet->channel will be set to the channel passed in to this function.
Note: The maximum size of the packet is limited by the MTU (Maximum Transfer Unit)
of the transport medium. It can be as low as 250 bytes for some PPP links, and as high
as 1500 bytes for ethernet. Beyond that limit the packet will fragment, and make delivery
more and more unreliable as lost fragments cause the whole packet to be discarded.
Returns: The number of destinations sent to that worked. 0 is returned on errors.
Note that since a channel can point to multiple destinations, there should be just as many
packets sent, so dont assume it will always return 1 on success. Unfortunately theres no
way to get the number of destinations bound to a channel, so either you have to remember
the number bound, or just test for the zero return value indicating all channels failed.
// send a packet using a UDPsocket, using the packets channel as the channel
//UDPsocket udpsock;
//UDPpacket *packet;
int numsent;
numsent=SDLNet_UDP_Send(udpsock, packet->channel, packet);
if(!numsent) {
printf("SDLNet_UDP_Send: %s\n", SDLNet_GetError());
// do something because we failed to send
// this may just be because no addresses are bound to the channel...
}
Heres a way of sending one packet using its internal channel setting.
This is actually what SDLNet UDP Send ends up calling for you.
Chapter 3: Functions
35
// send a packet using a UDPsocket, using the packets channel as the channel
//UDPsocket udpsock;
//UDPpacket *packet;
int numsent;
numsent=SDLNet_UDP_SendV(sock, &packet, 1);
if(!numsent) {
printf("SDLNet_UDP_SendV: %s\n", SDLNet_GetError());
// do something because we failed to send
// this may just be because no addresses are bound to the channel...
}
See Also:
Section 3.4.3 [SDLNet UDP Bind], page 31, Section 3.4.8 [SDLNet UDP SendV], page 37,
Section 3.4.7 [SDLNet UDP Recv], page 36, Section 3.4.9 [SDLNet UDP RecvV], page 38,
Section 4.4 [UDPpacket], page 57, Section 4.3 [UDPsocket], page 56
Chapter 3: Functions
36
A valid UDPsocket.
packet
See Also:
Section 3.4.3 [SDLNet UDP Bind], page 31, Section 3.4.6 [SDLNet UDP Send], page 34,
Section 3.4.8 [SDLNet UDP SendV], page 37, Section 3.4.9 [SDLNet UDP RecvV],
page 38, Section 4.4 [UDPpacket], page 57, Section 4.3 [UDPsocket], page 56
Chapter 3: Functions
37
A valid UDPsocket.
packetV
npackets
See Also:
Section 3.4.3 [SDLNet UDP Bind], page 31, Section 3.4.6 [SDLNet UDP Send], page 34,
Section 3.4.7 [SDLNet UDP Recv], page 36, Section 3.4.9 [SDLNet UDP RecvV], page 38,
Section 4.4 [UDPpacket], page 57, Section 4.3 [UDPsocket], page 56
Chapter 3: Functions
38
A valid UDPsocket.
packet
See Also:
Section 3.4.3 [SDLNet UDP Bind], page 31, Section 3.4.6 [SDLNet UDP Send], page 34,
Section 3.4.8 [SDLNet UDP SendV], page 37, Section 3.4.7 [SDLNet UDP Recv], page 36,
Section 4.4 [UDPpacket], page 57, Section 4.3 [UDPsocket], page 56
Chapter 3: Functions
39
Chapter 3: Functions
40
Create (via malloc) a new UDPpacket with a data buffer of size bytes.
The new packet should be freed using SDLNet FreePacket when you are done using it.
Returns: a pointer to a new empty UDPpacket. NULL is returned on errors, such as
out-of-memory.
See Also:
Section 3.5.4 [SDLNet AllocPacketV], page 43, Section 3.5.2 [SDLNet ResizePacket],
page 41, Section 3.5.3 [SDLNet FreePacket], page 42, Section 3.4.6 [SDLNet UDP Send],
page 34, Section 3.4.8 [SDLNet UDP SendV], page 37, Section 4.4 [UDPpacket], page 57
Chapter 3: Functions
41
size
The new desired size, in bytes, of the data buffer to be allocated in the UDPpacket.
Zero is invalid.
Resize a UDPpackets data buffer to size bytes. The old data buffer will not be retained, so
the new buffer is invalid after this call.
Returns: the new size of the data in the packet. If the number returned is less than what
you asked for, thats an error.
See Also:
Section 3.5.1 [SDLNet AllocPacket], page 40, Section 3.5.4 [SDLNet AllocPacketV],
page 43, Section 3.5.3 [SDLNet FreePacket], page 42, Section 4.4 [UDPpacket], page 57
Chapter 3: Functions
42
Free a UDPpacket from memory. Do not use this UDPpacket after this function is called
on it.
Returns: nothing, this always succeeds.
// Free a UDPpacket
//UDPpacket *packet;
SDLNet_FreePacket(packet);
packet=NULL; //just to help you know that it is freed
See Also:
Section 3.5.1 [SDLNet AllocPacket], page 40, Section 3.5.4 [SDLNet AllocPacketV],
page 43, Section 3.5.5 [SDLNet FreePacketV], page 44, Section 3.5.2 [SDLNet ResizePacket], page 41, Section 4.4 [UDPpacket], page 57
Chapter 3: Functions
43
size
Create (via malloc) a vector of new UDPpackets, each with data buffers of size bytes. The
new packet vector should be freed using SDLNet FreePacketV when you are done using it.
The returned vector is one entry longer than requested, for a terminating NULL.
Returns: a pointer to a new empty UDPpacket vector. NULL is returned on errors, such
as out-of-memory.
See Also:
Section 3.5.1 [SDLNet AllocPacket], page 40, Section 3.5.3 [SDLNet FreePacket], page 42,
Section 3.5.5 [SDLNet FreePacketV], page 44, Section 3.5.2 [SDLNet ResizePacket],
page 41, Section 4.4 [UDPpacket], page 57
Chapter 3: Functions
44
Free a UDPpacket vector from memory. Do not use this UDPpacket vector, or any UDPpacket in it, after this function is called on it.
Returns: nothing, this always succeeds.
See Also:
Section 3.5.4 [SDLNet AllocPacketV], page 43, Section 3.5.1 [SDLNet AllocPacket],
page 40, Section 3.5.3 [SDLNet FreePacket], page 42, Section 3.5.2 [SDLNet ResizePacket],
page 41, Section 4.4 [UDPpacket], page 57
Chapter 3: Functions
45
Chapter 3: Functions
46
See Also:
Section 3.6.2 [SDLNet FreeSocketSet], page 47, Section 3.6.3 [SDLNet AddSocket], page 48,
Section 4.5 [SDLNet SocketSet], page 58, Section 4.3 [UDPsocket], page 56, Section 4.2
[TCPsocket], page 55
Chapter 3: Functions
47
See Also:
Section 3.6.1 [SDLNet AllocSocketSet], page 46, Section 3.6.3 [SDLNet AddSocket],
page 48, Section 4.5 [SDLNet SocketSet], page 58, Section 4.3 [UDPsocket], page 56,
Section 4.2 [TCPsocket], page 55
Chapter 3: Functions
48
sock
Add a socket to a socket set that will be watched. TCP and UDP sockets should be added
using the corrosponding macro (as in SDLNet_TCP_AddSocket for a TCP socket). The
generic socket function will be called by the TCP and UDP macros. Both TCP and UDP
sockets may be added to the same socket set. TCP clients and servers may all be in the
same socket set. There is no limitation on the sockets in the socket set, other than they
have been opened.
Returns: the number of sockets used in the set on success. -1 is returned on errors.
See Also:
Section 3.6.1 [SDLNet AllocSocketSet], page 46, Section 3.6.4 [SDLNet DelSocket], page 49,
Section 3.6.5 [SDLNet CheckSockets], page 50, Section 4.5 [SDLNet SocketSet], page 58,
Section 4.3 [UDPsocket], page 56, Section 4.2 [TCPsocket], page 55
Chapter 3: Functions
49
sock
Remove a socket from a socket set. Use this before closing a socket that you are watching
with a socket set. This doesnt close the socket. Call the appropriate macro for TCP or
UDP sockets. The generic socket function will be called by the TCP and UDP macros.
Returns: the number of sockets used in the set on success. -1 is returned on errors.
See Also:
Section 3.6.3 [SDLNet AddSocket], page 48, Section 3.6.2 [SDLNet FreeSocketSet], page 47,
Section 4.5 [SDLNet SocketSet], page 58, Section 4.3 [UDPsocket], page 56, Section 4.2
[TCPsocket], page 55
Chapter 3: Functions
50
timeout
Check all sockets in the socket set for activity. If a non-zero timeout is given then this
function will wait for activity, or else it will wait for timeout milliseconds.
NOTE: "activity" also includes disconnections and other errors, which would be determined by a failed read/write attempt.
Returns: the number of sockets with activity. -1 is returned on errors, and you may not
get a meaningful error message. -1 is also returned for an empty set (nothing to check).
See Also:
Section 3.6.6 [SDLNet SocketReady], page 51, Section 3.6.3 [SDLNet AddSocket], page 48,
Section 3.6.4 [SDLNet DelSocket], page 49, Section 3.6.1 [SDLNet AllocSocketSet], page 46,
Section 4.5 [SDLNet SocketSet], page 58, Section 4.3 [UDPsocket], page 56, Section 4.2
[TCPsocket], page 55
Chapter 3: Functions
51
Check whether a socket has been marked as active. This function should only be used on a
socket in a socket set, and that set has to have had SDLNet CheckSockets (see Section 3.6.5
[SDLNet CheckSockets], page 50) called upon it.
Returns: non-zero for activity. zero is returned for no activity.
Chapter 3: Functions
52
See Also:
Section 3.6.5 [SDLNet CheckSockets], page 50, Section 3.6.3 [SDLNet AddSocket], page 48,
Section 3.6.4 [SDLNet DelSocket], page 49, Section 3.6.1 [SDLNet AllocSocketSet], page 46,
Section 4.5 [SDLNet SocketSet], page 58, Section 4.3 [UDPsocket], page 56, Section 4.2
[TCPsocket], page 55
Chapter 4: Types
4 Types
These types are defined and used by the SDL net API.
53
Chapter 4: Types
54
4.1 IPaddress
typedef struct {
Uint32 host ;
Uint16 port ;
} IPaddress;
host
port
This type contains the information used to form network connections and sockets.
See Also:
Section 3.2 [Name Resolution], page 17, Section 3.3.1 [SDLNet TCP Open], page 21, Section 4.4 [UDPpacket], page 57
Chapter 4: Types
55
4.2 TCPsocket
This is an opaque data type used for TCP connections. This is a pointer, and so it could
be NULL at times. NULL would indicate no socket has been established.
See Also:
Section 3.3 [TCP Sockets], page 20, Section 4.3 [UDPsocket], page 56, Section 4.6 [SDLNet GenericSocket], page 59
Chapter 4: Types
56
4.3 UDPsocket
This is an opaque data type used for UDP sockets. This is a pointer, and so it could be
NULL at times. NULL would indicate no socket has been established.
See Also:
Section 3.4 [UDP Sockets], page 28, Section 4.4 [UDPpacket], page 57, Section 4.2 [TCPsocket], page 55, Section 4.6 [SDLNet GenericSocket], page 59
Chapter 4: Types
57
4.4 UDPpacket
typedef struct {
int channel ;
Uint8 *data ;
int len ;
int maxlen ;
int status ;
IPaddress address ;
/*
/*
/*
/*
/*
/*
} UDPpacket;
channel
The (software) channel number for this packet. This can also be used as a
priority value for the packet. If no channel is assigned, the value is -1.
data
len
maxlen
This is size of the data buffer, which may be larger than the meaningful length.
This is only used for packet creation on the senders side.
status
This contains the number of bytes sent, or a -1 on errors, after sending. This
is useless for a received packet.
address
This struct is used with UDPsockets to send and receive data. It also helps keep track
of a packets sending/receiving settings and status. The channels concept helps prioritize,
or segregate differring types of data packets.
See Also:
Section 3.5 [UDP Packets], page 39, Section 4.3 [UDPsocket], page 56, Section 4.1 [IPaddress], page 54
Chapter 4: Types
58
This is an opaque data type used for socket sets. This is a pointer, and so it could be
NULL at times. NULL would indicate no socket set has been created.
See Also:
Section 3.6 [Socket Sets], page 45, Section 4.2 [TCPsocket], page 55, Section 4.3 [UDPsocket], page 56
Chapter 4: Types
59
typedef struct {
int ready ;
} *SDLNet_GenericSocket;
ready
This data type is able to be used for both UDPsocket and TCPsocket types.
After calling SDLNet CheckSockets, if this socket is in SDLNet SocketSet used, the ready
will be set according to activity on the socket. This is the only real use for this type, as it
doesnt help you know what type of socket it is.
See Also:
Section 3.6 [Socket Sets], page 45, Section 4.2 [TCPsocket], page 55, Section 4.3 [UDPsocket], page 56
Chapter 5: Defines
60
5 Defines
SDL NET MAJOR VERSION
1
SDL net library major number at compilation time
SDL NET MINOR VERSION
2
SDL net library minor number at compilation time
SDL NET PATCHLEVEL
7
SDL net library patch level at compilation time
INADDR ANY
0x00000000 (0.0.0.0)
used for listening on all network interfaces.
INADDR NONE
0xFFFFFFFF (255.255.255.255)
which has limited applications.
INADDR BROADCAST
0xFFFFFFFF (255.255.255.255)
used as destination when sending a message to all clients on a subnet that allows
broadcasts.
SDLNET MAX UDPCHANNELS
32
The maximum number of channels on a UDP socket.
SDLNET MAX UDPADDRESSES
4
The maximum number of addresses bound to a single UDP socket channel
Chapter 6: Glossary
61
6 Glossary
Index
Index
(Index is nonexistent)
62