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.....