380 likes | 572 Views
Programming Microcontroller ENET (Ethernet) peripheral with MAC (Media Access Control) controller. 64K or 96K Byte SRAM. Enet MAC. ARM966E. CORE. w/DSP. USB 2.0FS. 96 MHz. 256K or 512K Byte Burst Flash. PFQ BC. CAN 2.0B. DMA. INTR Cntl. CLK Cntl. 32K Byte Burst Flash.
E N D
Programming Microcontroller ENET (Ethernet) peripheral with MAC (Media Access Control) controller 64K or 96K Byte SRAM Enet MAC ARM966E CORE w/DSP USB 2.0FS 96 MHz 256K or 512K Byte Burst Flash PFQ BC CAN 2.0B DMA INTR Cntl CLK Cntl 32K Byte Burst Flash GPIO OTP Mem LVD BOD EXT. Bus JTAG ETM9 PLL RTC TIM ADC SPI I2C UART ENET with MAC controller
Standard way of web communication Browser as web client Web server with data base The purpose of web application Represent the data of the server on the client Save the input of the client on the server Example of web applications Query the SBB timetable Buy books Download information about a company The classical web applications
Static web pages are displayed without further processing HTML, XHTML, XML CSS Plug-Ins (Multimedia) Static web pages
Client-side dynamic execution Script languages (JavaScript) Execution of Programs (Applet) Server-side dynamic execution Script languages (Perl & PHP) Execution of Programs (Servlet) Dynamic web pages
Typical applications Context depending menus, input validation & calculation with formula Client-side dynamic execution
Typical applications Creation of individual user web pages Server-side dynamic execution
Script languages Client-side script languages JavaScript, VBSkript & Jscript Server-side script languages Perl & PHP High-level languages C, C++, C#, Visual Basic or Java Programming languages for Internet
Application examples for the embedded word Machines, automation etc. which should be accessible through Internet Purpose of the “Embedded web server” Remote maintenance (diagnostic, setting the parameters) Alarming Configuration Embedded web server
ISO/OSI Model for the Client-Server communication • Transport • Network • Data link • Physical
Socket is an endpoint for communication between two machines IP Address Protocol (TCP or UDP) Port number General procedure for exchanging data Build a connection using two sockets Exchange of data Close the connection Socket (Buchse / prise)
TCP is a connection-oriented transport protocol Transmission Control Protocol Creating the TCP connection First, start the server Next the client must connect himself to the server Finally the data can be exchanged in both directions TCP based application
socket() creates a TCP or UDP socket Server Socket SS on the server Client Socket CS on the client Specification of the used protocol (TCP or UDP) socket()
bind() binds the socket with a local protocol address IP address & port number bind() is only uses by the server bind()
listen() sets the server in a listening mode Maximum number of connections that will be processed simultaneously listen()
accept() enables the Server to accept a connection from a client Full queue (backlog) Next connection request from the queue Empty queue New connection request from a client accept()
connect() enables the client to build a connection with the server Server socket creates a new socket (newSS) Dedicated line to the requested client connect()
The data are exchanged in both direction with recv() & send() recv()andsend()
After successful communication The sockets newSS & CS must be closed close()
UDP is minimal and non connection-oriented transport protocol User Datagram Protocol UDP-based applications
Programming of client and server applications in C Application Programming Interface (API) functions of the TCP/IP- Stacks Socket programming in C
Common data structure struct sockaddr { unsigned short sa_family // Address family (AF_INET) unsigned sa_data[14]; /* Protocol-specific address information */ } Data structure for AF_INET (Address Family Interactive Network System) struct sockaddr_in { unsigend short sin_family // Protocol family (PF_INET) unsigned short sin_port; // Address port (16 bits) struct in_addr sin_addr; // IP address (32 bits) } struct in_addr { NET_IP_ADDR s_addr; // IP address (32 bits) } Data structure for the protocol addresses (1)
Transmission of sockaddr & sockaddr_inin big endian Network byte order Conversion from the “host byte order” into “network byte order” uint32_t htonl (uint32_t hostlong); uint16_t htons (uint32_t hostshort); Help function
Prototype int socket (int domain, int type, int protocol); Argument domain Application domain of the socket (PF_INET) type Behavior of the socket (SOCK_STREAM or SOCK_DGRAM) protocol IPPROTO_TCP or IPPROTO_UDP Result Success: Socket-ID Error: -1 socket()
Prototype int bind (int sock_id, struct sockaddr *addr, int addrlen); Argument sock_id Result of socket() addr Information about Port number & IP address addrlen Size of addr Result Success: 0 Error: -1 bind()
Prototype int listen (int sock_id, int backlog); Argument sock_id Result of socket() backlog Maximum waiting clients Result Success: 0 Error: -1 listen()
Prototype int connect (int sock_id, struct sockaddr *addr, int addrlen); Argument sock_id Result of socket() addr Information about Port number & IP address of the server to connect addrlen Size of addr Result Success: 0 Error: -1 connect()
Prototype int accept(int sock_id, struct sockaddr *addr_remote, int *addrlen_remote); Argument sock_id Result of socket() addr_remote Information about Port number & IP address of the client addrlen_remote Size of addr_remote Result Success: new socket ID Error: -1 accept()
Prototype int recv(int newSock_id, void *buf, int *buf_len, int flags); Argument newSock_id or sock_id Result ofaccept() on server side or socket() on client side buf Buffer for the received data buf_len Length of buf flags Receive option: can be set to zero Result Success: Number of received bytes Error: -1 recv()
Prototype int send(int newSock_id, void *data, int datalen, int flags); Argument newSock_id or sock_id Result ofaccept() on server side or socket() on client side data The data to transmit datalen The length of the data to transmit flags Transmit option: can be set to zero Result Success: Number of transmitted bytes Error: -1 send()
Class Socket Socket (String host, int port); Stream Classes for sending or receiving the data BufferedReader InputStreamReader (ASCII unicode) InputStream (ASCII) PrintStream (unicode ASCII) OutputStream (ASCII) Programming in Java
public void openSocket(){ try{ // replace parameters with real values sock = new Socket(Server_Address, Port); // Send without delay sock.setTcpNoDelay(true); inputStream = sock.getInputStream(); bufferedReader = new BufferedReader new InputStreamReader(inStream)); outputStream = sock.getOutputStream(); printStream = new PrintStream(outStream, true); } catch (SocketException e){ // error handling } catch (IOException e){ // error handling } } Generating a client socket in Java
public void closeSocket(){ try { inputStream.close(); outputStream.close(); sock.close(); } catch (IOException e){ // error handling } } Closing the socket and the data stream
public void sendData(String data){ printStream.print(data); } public void getData(){ String data; try { data = bufferedReader.readLine(); // process data } catch (IOException e){ // error handling } } Transmitting & Receiving the data