Defines | |
#define | IP_ADDRESS(a, b, c, d) ( ( (int)a << 24 ) + ( (int)b << 16 ) + ( (int)c << 8 ) + (int)d ) |
Generate an address appropriate for Socket functions from 4 integers. | |
Functions | |
void * | Socket (int address, int port) |
Create a new TCP socket connected to the address and port specified. | |
int | SocketBytesAvailable (void *socket) |
Get the number of bytes available in a TCP socket. | |
int | SocketRead (void *socket, char *data, int length) |
Read from a TCP socket. | |
int | SocketReadLine (void *socket, char *data, int length) |
Read a line from a TCP socket terminated by CR LF (0x0D 0x0A). | |
int | SocketWrite (void *socket, char *data, int length) |
Write to a TCP socket. | |
void | SocketClose (void *socket) |
Close an existing TCP socket. | |
void * | ServerSocket (int port) |
Create a new TCP server socket and start listening for connections. | |
void * | ServerSocketAccept (void *serverSocket) |
Accept an incoming connection to a ServerSocket that you have created. | |
int | ServerSocketClose (void *serverSocket) |
Close a ServerSocket that you have created. | |
void * | DatagramSocket (int port) |
Create a socket to read and write UDP packets. | |
int | DatagramSocketSend (void *datagramSocket, int address, int port, void *data, int length) |
Send a UDP packet to a specified address. | |
int | DatagramSocketReceive (void *datagramSocket, int incomingPort, int *address, int *port, void *data, int length) |
Receive a UDP packet. | |
void | DatagramSocketClose (void *socket) |
Close a DatagramSocket(). |
This subsystem is a light wrapper around LwIP, the open source TCP/IP stack used by the Make Controller Kit. There are 3 groups of socket functions:
#define IP_ADDRESS | ( | a, | |||
b, | |||||
c, | |||||
d | ) | ( ( (int)a << 24 ) + ( (int)b << 16 ) + ( (int)c << 8 ) + (int)d ) |
Generate an address appropriate for Socket functions from 4 integers.
Example
void* sock = Socket( IP_ADDRESS( 192, 168, 0, 200 ), 80 );
void* DatagramSocket | ( | int | port | ) |
Create a socket to read and write UDP packets.
port | An integer specifying the port to open. |
// create a new UDP socket on port 10101 struct netconn* udpSocket = DatagramSocket( 10101 ); // now read and write to it using DatagramSocketSend( ) and DatagramSocketReceive( )
void DatagramSocketClose | ( | void * | socket | ) |
Close a DatagramSocket().
socket | A pointer to the DatagramSocket() to close. |
struct netconn* udpSocket = DatagramSocket( 10101 ); // create our socket // now close it DatagramSocketClose( udpSocket );
int DatagramSocketReceive | ( | void * | datagramSocket, | |
int | incomingPort, | |||
int * | address, | |||
int * | port, | |||
void * | data, | |||
int | length | |||
) |
Receive a UDP packet.
This function will block until a packet is received. The address and port of the sender are returned in the locations pointed to by the address and port parameters. If the incoming packet is larger than the specified size of the buffer, it will be truncated.
datagramSocket | A pointer to the DatagramSocket() you're using to read. | |
incomingPort | An integer specifying the port to listen on. | |
address | A pointer to the IP address that sent the packet. | |
port | A pointer to the port of the sender. | |
data | A pointer to the buffer to read into. | |
length | An integer specifying the number of bytes in the packet being read. |
struct netconn* udpSocket = DatagramSocket( 10101 ); // our socket int address, port; int sent = DatagramSocketReceive( udpSocket, &address, &port, myBuffer, myLength );
int DatagramSocketSend | ( | void * | datagramSocket, | |
int | address, | |||
int | port, | |||
void * | data, | |||
int | length | |||
) |
Send a UDP packet to a specified address.
datagramSocket | A pointer to the DatagramSocket() you're using to write. | |
address | An integer specifying the IP address to write to. | |
port | An integer specifying the port to write to. | |
data | A pointer to the packet to send. | |
length | An integer specifying the number of bytes in the packet being sent. |
struct netconn* udpSocket = DatagramSocket( 10101 ); // our socket int address = IP_ADDRESS( 192, 168, 0, 200 ); int sent = DatagramSocketSend( udpSocket, address, 10101, myBuffer, myLength );
void* ServerSocket | ( | int | port | ) |
Create a new TCP server socket and start listening for connections.
port | An integer specifying the port to listen on. |
// create a socket and start listening on port 10101 struct netconn* server = ServerSocket( 10101 ); // ServerSocketAccept( ) will block until an incoming connection is made struct netconn* newConnection = ServerSocketAccept( server ); // now grab the data from the new connection
void* ServerSocketAccept | ( | void * | serverSocket | ) |
Accept an incoming connection to a ServerSocket that you have created.
This function will block until a new connection is waiting to be serviced. It returns a regular socket on which you can use SocketWrite(), SocketRead() and SocketClose().
serverSocket | a pointer to a ServerSocket that you created |
int ServerSocketClose | ( | void * | serverSocket | ) |
Close a ServerSocket that you have created.
serverSocket | A pointer to a ServerSocket. |
// we created a server socket at some point struct netconn* server = ServerSocket( 10101 ); // now close it ServerSocketClose( server );
void* Socket | ( | int | address, | |
int | port | |||
) |
Create a new TCP socket connected to the address and port specified.
address | An integer specifying the IP address to connect to. | |
port | An integer specifying the port to connect on. |
// use the IP_ADDRESS macro to format the address properly int addr = IP_ADDRESS( 192, 168, 0, 54 ); // then create the socket, connecting on port 10101 struct netconn* socket = Socket( addr, 10101 );
int SocketBytesAvailable | ( | void * | socket | ) |
Get the number of bytes available in a TCP socket.
Handy before calling SocketRead() so you know how many to read.
socket | The socket. |
struct netconn* mysocket = Socket(IP_ADDRESS( 192, 168, 0, 54 ), 10101); // ... reading and writing ... int avail = Socket_BytesAvailable(mysocket);
void SocketClose | ( | void * | socket | ) |
Close an existing TCP socket.
Anytime you get an error when trying to read or write, it's best to close the socket and reopen it to make sure that the connection is corrently configured.
socket | A pointer to the existing socket. |
// we should already have created a socket 'sock' with Socket(). struct netconn* sock = Socket( addr, 10101 ); // now close it SocketClose( sock )
int SocketRead | ( | void * | socket, | |
char * | data, | |||
int | length | |||
) |
Read from a TCP socket.
Make sure you have an open socket before trying to read from it. This function will block until the requested number of bytes are read. See Socket_BytesAvailable() to get the number of bytes waiting to be read.
socket | A pointer to the existing socket. | |
data | A pointer to the buffer to read to. | |
length | An integer specifying the maximum length in bytes that can be read. |
// we should already have created a socket sock with Socket(). struct netconn* sock = Socket( addr, 10101 ); int length_read = SocketRead( sock, data, length ) // if 0 bytes were read, there was some sort of error if( length_read == 0 ) SocketClose( sock );
int SocketReadLine | ( | void * | socket, | |
char * | data, | |||
int | length | |||
) |
Read a line from a TCP socket terminated by CR LF (0x0D 0x0A).
Make sure you have an open socket before trying to read from it.
socket | A pointer to the existing socket. | |
data | A pointer to the buffer to read to. | |
length | An integer specifying the maximum length in bytes to read. |
int SocketWrite | ( | void * | socket, | |
char * | data, | |||
int | length | |||
) |
Write to a TCP socket.
Not surprisingly, we need an existing socket before we can write to it.
socket | A pointer to the existing socket. | |
data | A pointer to the buffer to write from. | |
length | An integer specifying the length in bytes of how much data should be written. |
// we should already have created a socket with Socket() struct netconn* sock = Socket( addr, 10101 ); int length_written = SocketWrite( sock, data, length ) // if 0 bytes were written, there was some sort of error if( length_written == 0 ) SocketClose( sock );