[En-Nut-Discussion] UDP Server Problem ( NutUdpReceiveFrom() )

MUHAMMAD AZEEM AZAM muhammadazeemazam786 at yahoo.com
Tue Feb 20 14:51:47 CET 2007


Hi 
  I have the following code of UDP Server
  I am still receiving data on my client side (Client is in Visual C++ 6.0)
  I didn't even mentioned the Remote host -- > PORT and IP Address.
                                        OR
  This function automatically reterives the PORT and IP Address of Client (like in MSDN Windows Sockets , the "recfrom() function")
  ---------------------------------------------------
  #define PORT 23
#define IP_ADDR "192.168.05.20"
#define IP_MASK "255.255.255.0"
   
  #define BUFFSIZE 128 
static char buff[BUFFSIZE];
   
  UDPSOCKET *sock;
   
  u_char   ShiftLEDOut         =0x55;
u_char   ShiftLEDIn          =0;
u_char   LCDText[50]         ="\x0";
   
  char text_msg1_str[] = "";
unsigned int packet_number = 1;
   
  void ProcessRequests(void);
void main(void)
  {
 
 static char txt_msg0_str[]  = "UDP Server \x0";
   static char txt_msg1_str[]  = "Ethernut Project\x0";
 
 u_char mac[6] = { 0x00, 0x0A, 0x59, 0x03, 0x00, 0x5F };
  
/***************************************************
       LED & LCD initialization
  *****************************************************/
  
  
 DevBoardShiftLedOut(ShiftLEDOut);
    strcpy(LCDText,txt_msg0_str);
 strcat(LCDText,txt_msg1_str);
 
 LCD_Init();
   LCD_Cursor_YX( 0, 0 );
 LCD_Puts(txt_msg0_str);
  LCD_Cursor_YX( 1, 0 );
   LCD_Puts(txt_msg1_str);
   LCD_SendCmd(0x0C);   /* display on, cursor off */
  /**********************************************************
       Register all devices used in our application.
     Go to Architectures->AVR->DeviceDriver->Ethernet
  *************************************************************/
   
 NutRegisterDevice(&devEth0, 0x8300, 5); //#include <dev/board.h>  [&devEth0 = &DEV_ETHER]
  
/***********************************************************************************
  *Configure a network interface. 
  ************************************************************************************/
 
  u_long ip_addr = inet_addr(IP_ADDR);
     u_long ip_mask = inet_addr(IP_MASK); //#include <arpa/inet.h> for inet_addr()
       NutNetIfConfig("eth0", mac, ip_addr, ip_mask); //#include <dev/board.h>
 
/******************************************************
       Now loop endless for connections.
  *****************************************************/
  
for(;;)
  {
 
 if ((sock = NutUdpCreateSocket(PORT)) != 0) 
  
  {
     ProcessRequests();
  }
 
 NutUdpDestroySocket(sock);
 
NutSleep(125); 
  }
  }
   
   
  void ProcessRequests(void)
{
 for (;;) 
 {
  
  int got_data;
  int B_Rec;
  char *cp;
  
  u_long ip_addr_d;// = inet_addr(IP_ADDR_D);
  u_short port_d;// = PORT_D;
    
  B_Rec = NutUdpReceiveFrom (sock,&ip_addr_d,&port_d,buff,sizeof(buff),1000);
  if (B_Rec <= 0)
            continue;
  /*
      * Chop off EOL.
      */
        
  if ((cp = strchr(buff, '\r')) != 0)
         *cp = 0;       //put zero at the above address that is obtained.
     if ((cp = strchr(buff, '\n')) != 0)
      *cp = 0;
    got_data = strlen(buff);
        
  strcpy(text_msg1_str,buff);
 
  LCD_Init();
    LCD_Cursor_YX( 0, 0 );
    LCD_Puts(text_msg1_str);
    LCD_Cursor_YX( 1, 0 );
    LCD_Puts("Command");
    LCD_SendCmd(0x0C);   /* display on, cursor off */
    if (strncmp(buff, "Memory", got_data) == 0) //If Match 
  {
           NutUdpSendTo(sock,ip_addr_d,port_d,"azam",4);
     
   continue;
     }
    if (strncmp(buff, "Packet", got_data) == 0) //If Match 
  {
  
   int p_size = 128;
     char p_array[p_size];
     int index = 0;
  
 
     p_array[0] = (packet_number & 0xff00) >> 8;   //Mask Upper 8-Bits
    p_array[1] =  packet_number & 0x00ff;   // Mask Lower 8-Bits
   
    for(index=2;index<p_size;index++)
     {
       p_array[index] = rand()%26+ 'a';
      }
  
     NutUdpSendTo(sock,ip_addr_d,port_d,p_array,sizeof(p_array));
    
     packet_number ++ ;
       continue;
  }
  }
   
  -----------------------
  How to Break out of the "Process Request Function" and closing the Server from client side?
   
  Thanks for help in Advance
   
   

 
---------------------------------
Any questions?  Get answers on any topic at Yahoo! Answers. Try it now.


More information about the En-Nut-Discussion mailing list