/* 
UDP02.C: Use UDP send message to IP:10000. (A client program for UDP01.EXE)
         COM1 input ESC(27) to quit the program.

Compiler: BC++ 3.1, 
          Turbo C++ 1.01(3.01) (free from http://cc.codegear.com/free/cpp)
          MSC 6.0,
          MSVC 1.52.
      
Compile mode: large

Project: UDP01.c 
         ..\..\lib\7186el.Lib
         ..\..\lib\tcp_dm32.lib 

Details:        
    run: UDP02 [server_ip]
         for example: 
         1. udp02                --> the default server ip=192.168.188.187
         2. udp02 192.168.255.2  --> connect to 192.168.255.2

Hardware: uPAC-7186EX series
    
[Nov 04, 2008] by Liam
*/

#include <stdlib.h>
#include "..\..\lib\7186e.h"
#include "..\..\lib\Tcpip32.h"

uchar InBuf[1500], OutBuf[1500];
int OutIdx=0;
struct ip host_ip;
char *ServerIp="192.168.188.187";

void main(int argc, char *argv[])
{ 
    int skt;
    int err, quit=0;
    int DataBytes;
    
    InitLib();
  
    if(argc>1)
    {
        ServerIp=argv[1];
    }
  
    GetSavedIp(&host_ip);
    lhip("7186EX", &host_ip);
    
    Print("IP=%d.%d.%d.%d Mask=%d.%d.%d.%d\r\n", host_ip.addr.c[0], host_ip.addr.c[1],
                                                 host_ip.addr.c[2], host_ip.addr.c[3],
                                                 host_ip.mask.c[0], host_ip.mask.c[1],
                                                 host_ip.mask.c[2], host_ip.mask.c[3]);
    
    err=Ninit();    // initiate host environment
    if(err<0)
    {
        Print("Ninit() failed!");
        
        return;
    }
    
    err=Portinit("*");  // initiate network device
    if(err<0)
    {
        Print("Portinit() failed!");
        Nterm();
        
        return;
    }

    skt=Nopen(ServerIp, "UDP/IP", 10000, 0, S_NOCON | S_NOWA);
    if(skt<0)
    {
        Print("Cannot open a connection for UDP/IP!");
        Nterm();
        
        return;
    }  
  
    Print("Socket=%d\r\n", skt);
    SOCKET_NOBLOCK(skt);
    
    Print("\r\n[Send]: ");
    while(!quit)
    {
        YIELD();    // must add this line in the while loop
    
        DataBytes=Nread(skt, InBuf, 1500);
        if(DataBytes>0)
        {
            InBuf[DataBytes]=0;
            Print("Receive: "); 
            Print(InBuf);
            Print("\r\n[Send]: ");    
        }

        if(Kbhit())
        {
            int data=Getch(); 
            switch(data)
            {
                case 27:
                    quit=1;
                    break;
                
                case '\r':
                    OutBuf[OutIdx++]=data;
                    Putch('\r');
                    Putch('\n');
                    Nwrite(skt, OutBuf, OutIdx);    
                    OutIdx=0;
                    break;
                
                default:
                    OutBuf[OutIdx++]=data;
                    Putch(data);
                    break; 
            }
        }
    }

    Nclose(skt);
    Nterm();
}