[En-Nut-Discussion] UDP Server Problem ( NutUdpReceiveFrom() )
Ernst Stippl
ernst at stippl.org
Fri Feb 23 08:31:53 CET 2007
Hi!
processrequests waits until it receives an UDP packet, does its work and
sends back a UDP packet, all this in an endless for (;;) loop.
You will need to define a criteria when to break out of the for loop. But
remember: there is no "session" which can be closed. The sender will never
know if the packet arrives UNLESS there is some handshake on the application
layer. You could send back a packet with a special content signalling the
sender to logically ending the communication.
regard
ernst
-----Ursprüngliche Nachricht-----
Von: en-nut-discussion-bounces at egnite.de
[mailto:en-nut-discussion-bounces at egnite.de] Im Auftrag von MUHAMMAD AZEEM
AZAM
Gesendet: Dienstag, 20. Februar 2007 14:52
An: en-nut-discussion at egnite.de
Betreff: [En-Nut-Discussion] UDP Server Problem ( NutUdpReceiveFrom() )
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.
_______________________________________________
http://lists.egnite.de/mailman/listinfo/en-nut-discussion
--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.441 / Virus Database: 268.18.3/693 - Release Date: 19.02.2007
17:01
More information about the En-Nut-Discussion
mailing list