[Top] | [Contents] | [Index] | [ ? ] |
1. Overview The README from the SDL_net distribution
2. Getting Started Using SDL_net
3. Functions Functions supported by the SDL_net API 4. Types Types used with the SDL_net API 5. Defines Defined values/macros in the SDL_net API
6. Glossary Terms used in this document
Index Index of selected words
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 couldn't do it in SDL_net then perhaps I'm 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 I'll be having fun the whole time.
Feel free to contact me: JonathanCAtkins@gmail.com
I am also usually on IRC at irc.freenode.net in the #SDL channel as LIM
This is the README in the SDL_net source archive.
SDL_net 1.2
The latest version of this library is available from: This is an example portable network library for use with SDL. It is available under the GNU Library General Public License. The API can be found in the file SDL_net.h This library supports UNIX, Windows, and BeOS. MacOS support is being written.
The demo program is a chat client and server.
The chat client requires the sample GUI library available at: Note that this isn't necessarily how you would want to write a chat program, but it demonstrates how to use the basic features of the network library. Enjoy! -Sam Lantinga and Roy Wood |
You may want to look at
Beej's Guide to Network Programming, which explains network programming using BSD sockets. You can apply the knowledge from there while using SDL_net.
And here's a bit of humor for you to look at as you deal with networking terms.
Here's an easy game to play.
If a packet hits a pocket on a socket on a port,
If your cursor finds a menu item followed by a dash,
You can't say this? What a shame, sir!
If the label on the cable on the table at your house
When the copy of your floppy's getting sloppy on the disk, |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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:
|
You may also want to look at some demonstration code which may be downloaded from:
http://www.jonatkins.org/SDL_net/
2.1 Includes The include files to use for SDL_net 2.2 Compiling Using the SDL_net library and header file.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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"
|
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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.
Simple Example for compiling an object file:
Simple Example for compiling an object file:
|
Now myprogram
is ready to run.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These are the functions in the SDL_net API.
3.1 General API activation, versioning, and byte order helpers 3.2 Name Resolution Hostname and numeric IP lookup (IPaddress functions) 3.3 TCP Sockets Working with TCP connections 3.4 UDP Sockets Working with UDP semi-connections 3.5 UDP Packets Working with the UDPpacket type 3.6 Socket Sets Working on demand with sockets
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The basic API startup, cleanup and versioning methods, along with a few network byte order data helpers.
Info
Activation
3.1.1 SDLNet_Linked_Version Get version number
Errors
3.1.2 SDLNet_Init Start up SDL_net functionality 3.1.3 SDLNet_Quit Stop SDL_net functionality
Data Byte Order Helpers
3.1.4 SDLNet_GetError Get the current error string
3.1.5 SDLNet_Write16 Put a 16bit number in network ordered data 3.1.6 SDLNet_Write32 Put a 32bit number in network ordered data 3.1.7 SDLNet_Read16 Get a 16bit number from network ordered data 3.1.8 SDLNet_Read32 Get a 32bit number from network ordered data
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
const SDL_version *SDLNet_Linked_Version()
void SDL_NET_VERSION(SDL_version *compile_version)
This works similar to SDL_Linked_Version
and SDL_VERSION.
Using these you can compare the runtime version to the version that you compiled with.
No prior library initialization is needed to call these functions/macros.
|
See Also:
3.1.2 SDLNet_Init
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
int SDLNet_Init()
Initialize the network API.
This must be called before using other functions in this library.
SDL must be initialized before this call.
Returns: 0 on success, -1 on errors
|
See Also:
3.1.3 SDLNet_Quit
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
void SDLNet_Quit()
Shutdown and cleanup the network API.
After calling this all sockets are closed, and the SDL_net functions should not be used.
You may, of course, use SDLNet_Init to use the functionality again.
|
See Also:
3.1.2 SDLNet_Init
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
char *SDLNet_GetError()
This is the same as SDL_GetError, which returns the last error set as a string which you may use to tell the user what happened when an error status has been returned from an SDLNet_function.
Returns: a char pointer (string) containing a humam readble version or the reason for the last error that occured.
|
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
void SDLNet_Write16(Uint16 value, void *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 doesn't 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:
3.1.7 SDLNet_Read16,
3.1.6 SDLNet_Write32
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
void SDLNet_Write32(Uint32 value, void *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 doesn't 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:
3.1.8 SDLNet_Read32,
3.1.5 SDLNet_Write16
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Uint16 SDLNet_Read16(void *area)
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 doesn't 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:
3.1.5 SDLNet_Write16,
3.1.8 SDLNet_Read32
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Uint32 SDLNet_Read32(void *area)
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 doesn't 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:
3.1.6 SDLNet_Write32,
3.1.7 SDLNet_Read16
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These functions are used to resolve hostnames and numerical IPv4 Address to each other.
3.2.1 SDLNet_ResolveHost 3.2.2 SDLNet_ResolveIP
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
int SDLNet_ResolveHost(IPaddress *address,
const char *host,
Uint16 port)
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:
|
For a client connecting to "host.domain.ext", at port 1234:
|
See Also:
3.2.2 SDLNet_ResolveIP,
4.1 IPaddress
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
const char *SDLNet_ResolveIP(IPaddress *address)
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 it's not able to resolve the host name. The returned pointer is not to be freed. Each time you call this function the previous pointer's 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:
3.2.1 SDLNet_ResolveHost,
4.1 IPaddress
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These functions are used to work with TCP Sockets. TCP is used with a full connection, whereas UDP is connectionless. TCP also ensures that all packets reach the destionation (when possible). TCP also ensures that packets are received in the same order as sent.
General
Server
3.3.1 SDLNet_TCP_Open Open a TCP client or server socket 3.3.2 SDLNet_TCP_Close Close a TCP socket
Information
3.3.3 SDLNet_TCP_Accept Accept a connection on a server socket
Input/Output
3.3.4 SDLNet_TCP_GetPeerAddress Get the remote host address and port number
3.3.5 SDLNet_TCP_Send Send data over a connected socket 3.3.6 SDLNet_TCP_Recv Receive data from a connected socket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
TCPsocket SDLNet_TCP_Open(IPaddress *ip)
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.
|
|
See Also:
3.3.3 SDLNet_TCP_Accept,
3.3.2 SDLNet_TCP_Close,
4.1 IPaddress,
4.2 TCPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
void SDLNet_TCP_Close(TCPsocket sock)
This shutsdown, disconnects, and closes the TCPsocket sock.
After this, you can be assured that this socket is not in use anymore.
You can reuse the sock variable after this to open a new connection with SDLNet_TCP_Open.
Do not try to use any other functions on a closed socket, as it is now invalid.
Returns: nothing, this always succeeds for all we need to know.
|
See Also:
3.3.1 SDLNet_TCP_Open,
4.2 TCPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
TCPsocket SDLNet_TCP_Accept(TCPsocket server)
Accept an incoming connection on the server TCPsocket.
Do not use this function on a connected socket. Server sockets are never connected to a remote host. What you get back is a new TCPsocket that is connected to the remote host.
This is a non-blocking call, so if no connections are there to be accepted, you will get a
NULL TCPsocket and the program will continue going.
Returns: a valid TCPsocket on success, which indicates a successful connection has been established. NULL is returned on errors, such as when it's not able to create a socket, or it cannot finish connecting to the originating host and port. There also may not be a connection attempt in progress, so of course you cannot accept nothing, and you get a NULL in this case as well.
|
See Also:
3.3.1 SDLNet_TCP_Open,
3.3.4 SDLNet_TCP_GetPeerAddress,
3.3.2 SDLNet_TCP_Close,
4.2 TCPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
IPaddress *SDLNet_TCP_GetPeerAddress(TCPsocket sock)
Get the Peer's (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:
3.3.1 SDLNet_TCP_Open,
3.3.3 SDLNet_TCP_Accept,
4.1 IPaddress,
4.2 TCPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
int SDLNet_TCP_Send(TCPsocket sock, const void *data, int len)
Send data of length len over the socket sock.
This routine is not used for server sockets.
Returns: the number of bytes sent. If the number returned is less than len, then an error occured, such as the client disconnecting.
|
See Also:
3.3.6 SDLNet_TCP_Recv,
3.3.3 SDLNet_TCP_Accept,
3.3.1 SDLNet_TCP_Open,
3.3.4 SDLNet_TCP_GetPeerAddress,
3.3.2 SDLNet_TCP_Close,
4.2 TCPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
int SDLNet_TCP_Recv(TCPsocket sock, void *data, int maxlen)
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:
3.3.5 SDLNet_TCP_Send,
3.3.3 SDLNet_TCP_Accept,
3.3.1 SDLNet_TCP_Open,
3.3.4 SDLNet_TCP_GetPeerAddress,
3.3.2 SDLNet_TCP_Close,
4.2 TCPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These functions are used to work with UDP Sockets.
UDP is connectionless, but can be used as if it is connected, in the sense that you
don't have to address every outgoing packet if you don't want to. This is done by
binding a socket to remote IP address and port pairs.
UDP has no delivery guarantees, each packet has a chance of never getting to the
destination.
UDP packets may also be received completely out of order, as compared to the order
of sending them.
All these seeming bad qualities are made up for in speed. UDP is faster than TCP, and so
many games and speed sensitive applications, which may also be sending redundant data
anyways, such as a game state, prefer to use UDP for the speed benefits.
SDL_net has a concept of channels, which help you to matchup packets to specific clients easier. These channel numbers are not transmitted in the UDP packet data, but rather when a UDP socket receives or sends packets, a channel number may be used instead of an IPaddress to refer to the source or destination. You might prefer not to use channels at all, which is fine. SDL_net provides them only as an optional convenience.
General
Channel Binding
3.4.1 SDLNet_UDP_Open Create a UDP socket 3.4.2 SDLNet_UDP_Close Close and free a UDP socket
Single UDPpacket
3.4.3 SDLNet_UDP_Bind Assign an IP address number to a socket channel 3.4.4 SDLNet_UDP_Unbind Remove all assigned IP addresses from a socket channel 3.4.5 SDLNet_UDP_GetPeerAddress Get the assigned IP address for a socket channel or get the port you opened the socket with
Many UDPpackets in a vector (array)
3.4.6 SDLNet_UDP_Send Send a UDPpacket 3.4.7 SDLNet_UDP_Recv Receive into a UDPpacket
3.4.8 SDLNet_UDP_SendV Send a UDPpacket vector 3.4.9 SDLNet_UDP_RecvV Receive into a UDPpacket vector
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
UDPsocket SDLNet_UDP_Open(Uint16 port)
Open a socket to be used for UDP packet sending and/or receiving.
If a non-zero port is given it will be used, otherwise any open port number will
be used automatically.
Unlike TCP sockets, this socket does not require a remote host IP to connect to, this is
because UDP ports are never actually connected like TCP ports are.
This socket is able to send and receive directly after this simple creation.
Returns: a valid UDPsocket on success. NULL is returned on errors, such as when it's not able to create a socket, or it cannot assign the non-zero port as requested.
Note that below I say server, but clients may also open a specific port, though it is prefered that a client be more flexible, given that the port may be already allocated by another process, such as a server. In such a case you will not be able to open the socket, and your program will be stuck, so it is better to just use whatever port you are given by using a specified port of zero. Then the client will always work. The client can inform the server what port to talk back to, or the server can just look at the source of the packets it is receiving to know where to respond to.
|
|
See Also:
3.4.2 SDLNet_UDP_Close,
4.3 UDPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
void SDLNet_UDP_Close(UDPsocket sock)
Shutdown, close, and free a UDPsocket.
Don't use the UDPsocket after calling this, except to open a new one.
Returns: nothing, this always succeeds.
|
See Also:
3.4.1 SDLNet_UDP_Open,
4.3 UDPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
int SDLNet_UDP_Bind(UDPsocket sock, int channel, IPaddress *address)
Bind an address to a channel on a socket.
Incoming packets are only allowed from bound addresses for the socket channel.
All outgoing packets on that channel, regardless of the packets internal address, will attempt to send once on each bound address on that channel.
You may assign up to SDLNET_MAX_UDPADDRESSES to each channel.
Returns: The channel number that was bound. -1 is returned on errors, such as no free channels, or this channel has SDLNET_MAX_UDPADDRESSES already assigned to it, or you have used a channel higher or equal to SDLNET_MAX_UDPCHANNELS, or lower than -1.
|
See Also:
3.4.4 SDLNet_UDP_Unbind,
3.4.5 SDLNet_UDP_GetPeerAddress,
3.4.1 SDLNet_UDP_Open,
4.1 IPaddress,
4.3 UDPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
void SDLNet_UDP_Unbind(UDPsocket sock, int 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:
3.4.3 SDLNet_UDP_Bind,
3.4.2 SDLNet_UDP_Close,
4.3 UDPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
IPaddress *SDLNet_UDP_GetPeerAddress(UDPsocket sock, int channel)
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:
3.4.3 SDLNet_UDP_Bind,
3.4.4 SDLNet_UDP_Unbind,
4.1 IPaddress,
4.3 UDPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
int SDLNet_UDP_Send(UDPsocket sock, int channel, UDPpacket *packet)
Send packet using the specified socket sock, use ing the specified channel or else the packet's address.
If channel is not -1 then the packet is sent to all the socket channels bound addresses. If socket sock's channel is not bound to any destinations, then the packet is not sent at all!
If the channel is -1, then the packet's address is used as the destination.
Don't 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 there's 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.
|
Here's a way of sending one packet using it's internal channel setting.
This is actually what SDLNet_UDP_Send ends up calling for you.
|
See Also:
3.4.3 SDLNet_UDP_Bind,
3.4.8 SDLNet_UDP_SendV,
3.4.7 SDLNet_UDP_Recv,
3.4.9 SDLNet_UDP_RecvV,
4.4 UDPpacket,
4.3 UDPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
int SDLNet_UDP_Recv(UDPsocket sock, UDPpacket *packet)
Receive a packet on the specified sock socket.
The packet you pass in must have enough of a data size allocated for the incoming packet data to fit into. This means you should have knowledge of your size needs before trying to receive UDP packets.
The packet will have it's address set to the remote sender's address.
The socket's channels are checked in highest to lowest order, so if an address is bound to multiple channels, the highest channel with the source address bound will be retreived before the lower bound channels. So, the packets channel will also be set to the highest numbered channel that has the remote address and port assigned to it. Otherwise the channel will -1, which you can filter out easily if you want to ignore unbound source address.
Note that the local and remote channel numbers do not have to, and probably won't, match, as they are only local settings, they are not sent in the packet.
This is a non-blocking call, meaning if there's no data ready to be received the function will return.
Returns: 1 is returned when a packet is received. 0 is returned when no packets are received. -1 is returned on errors.
|
See Also:
3.4.3 SDLNet_UDP_Bind,
3.4.6 SDLNet_UDP_Send,
3.4.8 SDLNet_UDP_SendV,
3.4.9 SDLNet_UDP_RecvV,
4.4 UDPpacket,
4.3 UDPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
int SDLNet_UDP_SendV(UDPsocket sock, UDPpacket **packetV, int npackets)
Send npackets of packetV using the specified sock socket.
Each packet is sent in the same way as in SDLNet_UDP_Send (see section 3.4.6 SDLNet_UDP_Send).
Don't forget to set the length of the packets in the len element of the packets you are sending!
Returns: The number of destinations sent to that worked, for each packet in the vector, all summed up. 0 is returned on errors.
|
See Also:
3.4.3 SDLNet_UDP_Bind,
3.4.6 SDLNet_UDP_Send,
3.4.7 SDLNet_UDP_Recv,
3.4.9 SDLNet_UDP_RecvV,
4.4 UDPpacket,
4.3 UDPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
int SDLNet_UDP_RecvV(UDPsocket sock, UDPpacket **packetV)
Receive into a packet vector on the specified socket sock.
packetV is a NULL terminated array. Packets will be received until the NULL is reached, or there are none ready to be received.
This call is otherwise the same as SDLNet_UDP_Recv (see section 3.4.7 SDLNet_UDP_Recv).
Returns: the number of packets received. 0 is returned when no packets are received. -1 is returned on errors.
|
See Also:
3.4.3 SDLNet_UDP_Bind,
3.4.6 SDLNet_UDP_Send,
3.4.8 SDLNet_UDP_SendV,
3.4.7 SDLNet_UDP_Recv,
4.4 UDPpacket,
4.3 UDPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These functions are used to work with the UDPpacket type. This type is used with UDP sockets to transmit and receive data.
Single UDPpacket
Many UDPpackets in a vector (array)
3.5.1 SDLNet_AllocPacket Allocate a new UDP packet with a data buffer 3.5.2 SDLNet_ResizePacket Resize the data buffer in a UDPpacket 3.5.3 SDLNet_FreePacket Free a previously allocated UDPpacket
3.5.4 SDLNet_AllocPacketV Allocate a vector of UDPpackets 3.5.5 SDLNet_FreePacketV Free a vector of UDPpackets
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
UDPpacket *SDLNet_AllocPacket(int size)
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:
3.5.4 SDLNet_AllocPacketV,
3.5.2 SDLNet_ResizePacket,
3.5.3 SDLNet_FreePacket,
3.4.6 SDLNet_UDP_Send,
3.4.8 SDLNet_UDP_SendV,
4.4 UDPpacket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
int SDLNet_ResizePacket(UDPpacket *packet, int size)
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, that's an error.
|
See Also:
3.5.1 SDLNet_AllocPacket,
3.5.4 SDLNet_AllocPacketV,
3.5.3 SDLNet_FreePacket,
4.4 UDPpacket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
void SDLNet_FreePacket(UDPpacket *packet)
Free a UDPpacket from memory. Do not use this UDPpacket after this function is called on it.
Returns: nothing, this always succeeds.
|
See Also:
3.5.1 SDLNet_AllocPacket,
3.5.4 SDLNet_AllocPacketV,
3.5.5 SDLNet_FreePacketV,
3.5.2 SDLNet_ResizePacket,
4.4 UDPpacket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
UDPpacket **SDLNet_AllocPacketV(int howmany, int 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:
3.5.1 SDLNet_AllocPacket,
3.5.3 SDLNet_FreePacket,
3.5.5 SDLNet_FreePacketV,
3.5.2 SDLNet_ResizePacket,
4.4 UDPpacket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
void SDLNet_FreePacketV(UDPpacket **packetV)
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:
3.5.4 SDLNet_AllocPacketV,
3.5.1 SDLNet_AllocPacket,
3.5.3 SDLNet_FreePacket,
3.5.2 SDLNet_ResizePacket,
4.4 UDPpacket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These functions are used to work with multiple sockets. They allow you to determine when a socket has data or a connection waiting to be processed. This is analogous to polling, or the select function.
General
Setup
3.6.1 SDLNet_AllocSocketSet Create a new socket set 3.6.2 SDLNet_FreeSocketSet Free a socket set
Activity Checking
3.6.3 SDLNet_AddSocket Add a socket to a socket set 3.6.4 SDLNet_DelSocket Remove a socket from a socket set
3.6.5 SDLNet_CheckSockets Check and wait for sockets in a set to have activity 3.6.6 SDLNet_SocketReady See if a socket has activity
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
SDLNet_SocketSet SDLNet_AllocSocketSet(int maxsockets)
Create a socket set that will be able to watch up to maxsockets number of sockets. The same socket set can be used for both UDP and TCP sockets.
Returns: A new, empty, SDLNet_SocketSet. NULL is returned on errors, such as out-of-memory.
|
See Also:
3.6.2 SDLNet_FreeSocketSet,
3.6.3 SDLNet_AddSocket,
4.5 SDLNet_SocketSet,
4.3 UDPsocket,
4.2 TCPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
void SDLNet_FreeSocketSet(SDLNet_SocketSet set)
Free the socket set from memory.
Do not reference the set after this call, except to allocate a new one.
Returns: nothing, this call always succeeds.
|
See Also:
3.6.1 SDLNet_AllocSocketSet,
3.6.3 SDLNet_AddSocket,
4.5 SDLNet_SocketSet,
4.3 UDPsocket,
4.2 TCPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
int SDLNet_AddSocket(SDLNet_SocketSet set, SDLNet_GenericSocket sock)
int SDLNet_TCP_AddSocket(SDLNet_SocketSet set, TCPsocket sock)
int SDLNet_UDP_AddSocket(SDLNet_SocketSet set, UDPsocket 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:
3.6.1 SDLNet_AllocSocketSet,
3.6.4 SDLNet_DelSocket,
3.6.5 SDLNet_CheckSockets,
4.5 SDLNet_SocketSet,
4.3 UDPsocket,
4.2 TCPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
int SDLNet_DelSocket(SDLNet_SocketSet set, SDLNet_GenericSocket sock)
int SDLNet_TCP_DelSocket(SDLNet_SocketSet set, TCPsocket sock)
int SDLNet_UDP_DelSocket(SDLNet_SocketSet set, UDPsocket sock)
Remove a socket from a socket set. Use this before closing a socket that you are watching with a socket set. This doesn't 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:
3.6.3 SDLNet_AddSocket,
3.6.2 SDLNet_FreeSocketSet,
4.5 SDLNet_SocketSet,
4.3 UDPsocket,
4.2 TCPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
int SDLNet_CheckSockets(SDLNet_SocketSet set, Uint32 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:
3.6.6 SDLNet_SocketReady,
3.6.3 SDLNet_AddSocket,
3.6.4 SDLNet_DelSocket,
3.6.1 SDLNet_AllocSocketSet,
4.5 SDLNet_SocketSet,
4.3 UDPsocket,
4.2 TCPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
int SDLNet_SocketReady(sock)
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) called upon it.
Returns: non-zero for activity. zero is returned for no activity.
|
To just quickly do network handling with no waiting, we do this.
|
See Also:
3.6.5 SDLNet_CheckSockets,
3.6.3 SDLNet_AddSocket,
3.6.4 SDLNet_DelSocket,
3.6.1 SDLNet_AllocSocketSet,
4.5 SDLNet_SocketSet,
4.3 UDPsocket,
4.2 TCPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These types are defined and used by the SDL_net API.
4.1 IPaddress IP Address and Port number 4.2 TCPsocket TCP socket type (opaque) 4.3 UDPsocket UDP socket type (opaque) 4.4 UDPpacket UDP packet data encapsulation 4.5 SDLNet_SocketSet Socket Set type (opaque) 4.6 SDLNet_GenericSocket A generic type for UDP and TCP sockets
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
|
This type contains the information used to form network connections and sockets.
See Also:
3.2 Name Resolution,
3.3.1 SDLNet_TCP_Open,
4.4 UDPpacket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
|
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:
3.3 TCP Sockets,
4.3 UDPsocket,
4.6 SDLNet_GenericSocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
|
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:
3.4 UDP Sockets,
4.4 UDPpacket,
4.2 TCPsocket,
4.6 SDLNet_GenericSocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
|
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:
3.5 UDP Packets,
4.3 UDPsocket,
4.1 IPaddress
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
|
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:
3.6 Socket Sets,
4.2 TCPsocket,
4.3 UDPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
|
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 doesn't help you know what type of socket it is.
See Also:
3.6 Socket Sets,
4.2 TCPsocket,
4.3 UDPsocket
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
1
2
7
0x00000000 (0.0.0.0)
0xFFFFFFFF (255.255.255.255)
0xFFFFFFFF (255.255.255.255)
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Jump to: | B I L M N R S T U |
---|
Jump to: | B I L M N R S T U |
---|
[Top] | [Contents] | [Index] | [ ? ] |
[Top] | [Contents] | [Index] | [ ? ] |
1. Overview
2. Getting Started
3. Functions
4. Types
5. Defines
6. Glossary
Index
[Top] | [Contents] | [Index] | [ ? ] |
Button | Name | Go to | From 1.2.3 go to |
---|---|---|---|
[ < ] | Back | previous section in reading order | 1.2.2 |
[ > ] | Forward | next section in reading order | 1.2.4 |
[ << ] | FastBack | previous or up-and-previous section | 1.1 |
[ Up ] | Up | up section | 1.2 |
[ >> ] | FastForward | next or up-and-next section | 1.3 |
[Top] | Top | cover (top) of document | |
[Contents] | Contents | table of contents | |
[Index] | Index | concept index | |
[ ? ] | About | this page |