ex_tcp_client.c

This example program shows how to use the TCP/IP stack in client mode. It sends a single TCP message after the server's MAC address has been resolved via ARP. The message is sent to socket 10.1.0.101:54124 (IP address 10.1.0.101, port 54124).

00001 
00009 #include "net\stacktsk.h"
00010 #include "net\tick.h"
00011 #include "net\helpers.h"
00012 #include "net\tcp.h"
00013 #include "net\arp.h"
00014 #include "net\arptsk.h"
00015 
00016 //Create a TCP socket for receiving and sending data
00017 static TCP_SOCKET tcpSocketUser = INVALID_SOCKET;
00018 
00019 //TCP State machine
00020 #define SM_TCP_SEND_ARP     0
00021 #define SM_TCP_WAIT_RESOLVE 1
00022 #define SM_TCP_RESOLVED     2
00023 #define SM_TCP_SENT         3
00024 static BYTE smTcp = SM_TCP_SEND_ARP;
00025 
00026 //Timers
00027 TICK8  tsecArpRequested = 0;   //Time last ARP request was sent
00028 TICK16 tsecMsgSent = 0;        //Time last message was sent
00029 
00030 void tcpExample(void)
00031 {
00032     BYTE c;
00033     NODE_INFO tcpServerNode;
00034 
00035     //Initialize remote IP and address with 10.1.0.101. The MAC address is
00036     //is not intialized yet, but after we receive an ARP responce.
00037     tcpServerNode.IPAddr.v[0] = 10;
00038     tcpServerNode.IPAddr.v[1] = 1;
00039     tcpServerNode.IPAddr.v[2] = 0;
00040     tcpServerNode.IPAddr.v[3] = 101;
00041 
00042     tcpSocketUser = INVALID_SOCKET;
00043 
00044     //Infinite loop. Check if anything is received on TCP port
00045     while(1)
00046     {
00047         switch (smTcp) {
00048         case SM_TCP_SEND_ARP:
00049             if (ARPIsTxReady()) {
00050                 tsecArpRequested = TickGet8bitSec();   //Remember when we sent last request
00051                 
00052                 //Send ARP request for given IP address
00053                 ARPResolve(&tcpServerNode.IPAddr);
00054                 
00055                 smTcp = SM_TCP_WAIT_RESOLVE;
00056             }
00057             break;
00058         case SM_TCP_WAIT_RESOLVE:
00059             //The IP address has been resolved, we now have the MAC address of the
00060             //node at 10.1.0.101
00061             if (ARPIsResolved(&tcpServerNode.IPAddr, &tcpServerNode.MACAddr)) {
00062                 smTcp = SM_TCP_RESOLVED;
00063 
00064                 //We can now connect, seeing that we have the remote server node
00065                 //info (MAC and IP address)
00066                 tcpSocketUser = TCPConnect(&tcpServerNode, 54124);
00067     
00068                 //An error occurred during the TCPListen() function
00069                 if (tcpSocketUser == INVALID_SOCKET) {
00070                     //Add user code here to take action if required!
00071                 }
00072             }
00073             //If not resolved after 2 seconds, send next request
00074             else {
00075                 if (TickGetDiff8bitSec(tsecArpRequested) >= 2) {
00076                     smTcp = SM_TCP_SEND_ARP;
00077                 }
00078             }
00079             break;
00080         case SM_TCP_RESOLVED:
00081             //Checks if there is a transmit buffer ready for accepting data
00082             if (TCPIsPutReady(tcpSocketUser)) {
00083                 //Send a TCP Message with "Hello"
00084                 TCPPut(tcpSocketUser, 'H');
00085                 TCPPut(tcpSocketUser, 'e');
00086                 TCPPut(tcpSocketUser, 'l');
00087                 TCPPut(tcpSocketUser, 'l');
00088                 TCPPut(tcpSocketUser, 'o');
00089 
00090                 //Send contents of transmit buffer, and free buffer
00091                 TCPFlush(tcpSocketUser);
00092                 smTcp = SM_TCP_SENT;    //Set to "message sent" state
00093             }
00094             break;
00095         case SM_TCP_SENT:
00096             //Message sent state. If the user wants to resend the message, smTcp must be reset
00097             //to SM_TCP_RESOLVED
00098             break;
00099         }
00100 
00101         //This task performs normal stack task including checking for incoming
00102         //packet, type of packet and calling appropriate stack entity to
00103         //process it.
00104         StackTask();
00105     }
00106 }

Generated on Wed Feb 22 12:39:24 2006 for SBC68EC Web Server by  doxygen 1.4.5