EE458 - Embedded Systems Intro to Network Programming ● Outline – – ● TCP versus UDP Introduction to Sockets References – – Netburner Network Prog. Manual (Chs 11-12) Netburner Runtime Library (Ch 21) 1 Intro to Network Programming TCP and UDP ● ● ● There are two TCP/IP transport protocols: TCP (Transport Control Protocol) and UDP (User Datagram Protocol). UDP is a simple, unreliable, datagram protocol. UDP is faster than TCP. TCP is a sophisticated, reliable, byte-stream (unlimited length) protocol. 2 Intro to Network Programming TCP and UDP ● ● There is no guarantee that a UDP datagram will reach its destination. To ensure delivery our application must handle timeouts, acknowledgments, retransmissions, etc. UDP is a connectionless protocol. TCP provides a connection between two machines. When data is sent via TCP to the other end, it is acknowledged. If it is not then received the data will be resent. 3 Intro to Network Programming TCP and UDP ● ● ● TCP provides sequencing. When a lot of data is sent via TCP, the data will be sent in segments. If they arrive out of order, they will automatically be reordered. TCP provides flow control. A TCP peer advertises how many bytes it can accept. This prevents receive buffer overflow. A TCP connection is full-duplex. Data can be sent and received on the same socket. 4 Intro to Network Programming TCP and UDP Ports ● ● Multiple processes can use TCP or UDP simultaneously. 16-bit integer port numbers are used to distinguish processes. Servers listen for connections on wellknown ports (ftp - port 21, ssh - port 22, telnet - port 23, http - port 80). 5 Intro to Network Programming Client/Server Architecture ● Network applications are divided into client and server parts. A particular client usually communicates with a single server. While a server communicates with several clients. client client server client 6 Intro to Network Programming BSD Sockets ● ● The Berkeley or BSD Sockets API is the standard for network communication. It is supported by all modern OSes (Windows, Linux, BSD, and Mac OS). It is also supported by the Netburner. Most real-time OSes (RTEMS and uC/OS for example) support the BSD sockets API as the default networking API. 7 Intro to Network Programming BSD Sockets ● ● The client uses the connect() routine to establish a connection. read() and write() are used to send data across the connection. Sockets are bidirectional. The server uses the listen() routine to prepare for an incoming connection. The accept() routine is then used to wait for a client connection. The server uses read() and write() to transfer data. 8 Intro to Network Programming Client Routines ● A client uses the connect() routine to establish a connection to a listening server. int fd = connect( IPADDR addr, WORD localport, WORD remoteport, DWORD timeout); ● This routine returns a file descriptor that is used with read() and write() routines to transfer data. 9 Intro to Network Programming Client Routines ● ● addr is the binary IP address of the server, localport is the port to be used on the client (use 0 for automatic assignment). remoteport is the server port to connect to and timeout is the timeout in ticks to wait for the connection (0 waits forever). 10 Intro to Network Programming Server Routines ● The server first calls listen() to set up a queue for client connections: listfd = listen( IPADDR addr, WORD port, BYTE maxpend); ● A file descriptor is returned. You must accept() connections before you can communicate with the client. 11 Intro to Network Programming Server Routines ● ● addr is the binary IP address from which to accept connections. Use the macro INADDR_ANY to accept connections from any address. port is the port to listen on. (This is the port the client connects to.) maxpend is the number of pending connections to store on this socket. A value of 5 is typical. Client connections are held in the queue until accepted by the server. 12 Intro to Network Programming Server Routines ● The server calls the accept() routine to accept a client connection: newfd = accept( int listfd, IPADDR *addr, WORD *port, WORD timeout); accept() blocks until a connection is made. ● A new socket file descriptor is returned. The server uses this file descriptor to communicate with the client. 13 Intro to Network Programming Server Routines ● ● listfd is the file descriptor returned from listen(). addr and port are return arguments that contain the IP address and port of the connecting client. These may be NULL if that information is not needed. timeout is the number of ticks to wait. A value of 0 will wait forever. 14 Intro to Network Programming Utility Routines ● The Netburner API provides the ShowIP() routine to display a binary IP address in dotted-quad notation on the serial port: ShowIP(ipaddr); ● The AsciiToIp() routine converts from dotted-quad notation to a binary IP address: IPADDR ipnb = AsciiToIP(“192.168.0.2”); 15 Intro to Network Programming Network Byte Order ● Note that binary network addresses should be in “network byte order” (big endian order). Intel processors are little endian and the following will NOT work when trying to convert “10.5.50.100” to a network address on an Intel processor: unsigned int addr = (10<<24) + (5<<16) + (50<<8) + 100; 16 Intro to Network Programming Network Byte Order ● ● ● The conversion on the previous slide will work on the Netburner because the Coldfire processor is big endian. To convert 32-bit addresses between host and network order use the HTONL() and NTOHL() routines. These routines do nothing on big endian machines, but should be used for portability. 17 Intro to Network Programming Network Byte Order ● ● Port addresses are 16-bit integers (shorts). Use the HTONS() and NTOHS() routines to convert. Pay careful attention to the documentation to determine if a routine needs (or returns) an address in network or host order. 18