Home/Support/Support Forum/udp on wiem 9210 module in netos showing abort exception
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

udp on wiem 9210 module in netos showing abort exception

0 votes
hi folks,

this the sample code working in netos programmer but same way if we try it via thread cant able to establish the udp connection and showing abort exception ...
already dadatos_dad gave me some suggestion but still cant able to establish connection through "sendto" not working.....
/* Sample UDP client */

#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>

int main(int argc, char**argv)
{
int sockfd,n;
struct sockaddr_in servaddr,cliaddr;
char sendline[1000];
char recvline[1000];

if (argc != 2)
{
printf("usage: udpcli <IP address>\n");
exit(1);
}

sockfd=socket(AF_INET,SOCK_DGRAM,0);

bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=inet_addr(argv[1]);
servaddr.sin_port=htons(32000);

while (fgets(sendline, 10000,stdin) != NULL)
{
sendto(sockfd,sendline,strlen(sendline),0,
(struct sockaddr *)&servaddr,sizeof(servaddr));
n=recvfrom(sockfd,recvline,10000,0,NULL,NULL);
recvline[n]=0;
fputs(recvline,stdout);
}
}


in thread code ,


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "trace.h"
#include "tx_api.h"
#include "sockapi.h"
#include "termios.h"
#include "netosIo.h"
#include "appconf_api.h"
#include "appconf.h"

//udp
TX_THREAD Udp_thread= {0,};
UNS_8 Udp_Stack[THREAD_STACK_SIZE];
//udp
struct sockaddr_in si_me, si_other;
int sock, i, slen = sizeof(si_me),recv_len;
char buf[BUFLEN];

#define NA_UDP_PORT 7000
#define BUFLEN 512


/* UDP/FTP Communication Thread Settings */

#define NA_UDP_THREAD_NAME "UDP Communication"
#define THREAD_STACK_SIZE 8192
#define THREAD_PRIORITY_UDP 16
#define THREAD_TIME_SLICE 0
#define THREAD_ENTRY_IP 0
#define MSG_7 7

//function prototype

int naUdpStart(void);
void udp_app(unsigned long unused);
void die(char *s);
/*****************************************************************************************/
/* */
/* int naUdpStart(void) */
/* */
/* Purpose: Entry point for UDP THREAD */
/* */
/* Inputs : None */
/* */
/* Outputs: return NASTATUS_SUCCESS 0 */
/* NASTATUS_RESOURCE_ERROR */
/* Notes : None */
/* History: 21/07/14 VSS - Initial Revision */
/* */
/*****************************************************************************************/

int naUdpStart(void)
{
int rc = NASTATUS_SUCCESS;

int ccode;

ccode = tx_thread_create(&Udp_thread, // thread_ptr
NA_UDP_THREAD_NAME, // name_ptr
udp_app, // entry_function
THREAD_ENTRY_IP, // entry_input
Udp_Stack, // stack_start
THREAD_STACK_SIZE, // stack_size
THREAD_PRIORITY_UDP, // priority
THREAD_PRIORITY_UDP, // preempt_threshold
THREAD_TIME_SLICE , // time_slice
TX_AUTO_START); // auto_start

if (ccode != TX_SUCCESS)
{
DEBUG_PRINTF(TRACE_CRITICAL, "naUdpStart: tx_thread_create fails %d\n", ccode);
rc = NASTATUS_RESOURCE_ERROR;
}

return rc;
}

/*****************************************************************************************/
/* */
/* void die(char *s) */
/* */
/* Purpose: Entry point for SOCKET ERROR */
/* */
/* Inputs : None */
/* */
/* Outputs: None */
/* Notes : None */
/* History: 16/07/14 VSS - Initial Revision */
/* */
/*****************************************************************************************/

void die(char *s)
{
perror(s);
exit(1);
}

/*****************************************************************************************/
/* */
/* void udp_app(NASTATUS unused) */
/* */
/* Purpose: Entry point for SOCKET Connection */
/* */
/* Inputs : None */
/* */
/* Outputs: None */
/* Notes : None */
/* History: 16/07/14 VSS - Initial Revision */
/* */
/*****************************************************************************************/

void udp_app(NASTATUS unused)
{
(void)unused;

//create a UDP socket
if ((sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
die("socket");
}
// zero out the structure
memset((char *) &si_me, 0, sizeof(si_me));

si_me.sin_family = AF_INET;
si_me.sin_port = htons(NA_UDP_PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);

//bind socket to port
if( bind(sock ,(struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
{
die("bind");
}

//keep listening for data
while(1)
{




strcat (buf,"_Append");
//now reply to server socket/the client with the same data
if (sendto(sock,buf,recv_len+MSG_7, 0, (struct sockaddr*) &si_me, slen) == -1)
{
die("sendto()");
}
else
{

printf("Message_Sent :%s\n" , buf);
printf("Transmitted packet to %s Port No:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port)); //print details of the client/peer and the data transmitted
printf("Waiting for data...\n");
fflush(stdout);
memset((char *) &buf, 0, sizeof(buf));
}

printf("
\n");



}

close(sock);

}

as per dadatos_dad comment i changed the "si_other struct "to"si_me struct" and i tried without bind also not able send data through udp.....
asked Jul 27, 2014 in NET+OS by vijayakumar New to the Community (5 points)
edited Jul 26, 2014 by vijayakumar

Please log in or register to answer this question.

2 Answers

0 votes
hi folks,

anybody who able to know how to resolve this issue pls help me...
i am new netos ...
i have tried different way to resolve the issue but still i couldnt able to resolve the issue....
answered Jul 29, 2014 by vijayakumar New to the Community (5 points)
Hello

In place of calling die, use the following relative to a socket-related error:

printf("error return code was %d\n", getErrno());

Let us know what error you get.
0 votes
Hello

After reviewing your code again, here are a couple of things I missed that you might want to review:

You declare two stack buffers as follows:

char sendline[1000];
char recvline[1000];

On top of everything else on your stack do you have enough room for these? You could be blowing your stack. Think about mallocing your buffers instead of declaring them on the stack.

IN the following code segments:

while (fgets(sendline, 10000,stdin) != NULL)

n=recvfrom(sockfd,recvline,10000,0,NULL,NULL);

You are receiving up to 10,000 bytes into buffers that are only 1,000 bytes in size. In the worst case this could cause a major league buffer overflow. Doing so could certainly cause a crash.

Also:
#define THREAD_STACK_SIZE 8192
UNS_8 Udp_Stack[THREAD_STACK_SIZE];

You are placing an 8K buffer (thread stack) on your stack. Generally not a good idea. AS suggested previously, you might want to malloc this thus freeing up 8K from your stack.

In general, when you are getting weird crashed look for:
Places where you are overflowing a buffer
Places where you are overflowing your stack. AS stated, sometimes replacing a stack-place buffer with a malloced one can clean up such issues.
answered Aug 6, 2014 by dakotas_dad Veteran of the Digi Community (694 points)
...