Dear TI team,
I have a question related to the socket programming of the tiva tm4c1294ncpdt board.
I am trying to run simple code obtained from internet based of socket based UDP programming.
The code is :
/* * udpclient.c - A simple UDP client * usage: udpclient <host> <port> */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define BUFSIZE 1024 /* * error - wrapper for perror */ void error(char *msg) { perror(msg); exit(0); } Void tcpHandler(UArg arg0, UArg arg1) { int sockfd, portno, n; int serverlen; struct sockaddr_in serveraddr; struct hostent *server; char *hostname; char buf[BUFSIZE]; /* check command line arguments */ if (argc != 3) { fprintf(stderr,"usage: %s <hostname> <port>\n", argv[0]); exit(0); } hostname = argv[1]; portno = atoi(argv[2]); /* socket: create the socket */ sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) error("ERROR opening socket"); /* gethostbyname: get the server's DNS entry */ server = gethostbyname(hostname); if (server == NULL) { fprintf(stderr,"ERROR, no such host as %s\n", hostname); exit(0); } /* build the server's Internet address */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serveraddr.sin_addr.s_addr, server->h_length); serveraddr.sin_port = htons(portno); /* get a message from the user */ bzero(buf, BUFSIZE); printf("Please enter msg: "); fgets(buf, BUFSIZE, stdin); /* send the message to the server */ serverlen = sizeof(serveraddr); n = sendto(sockfd, buf, strlen(buf), 0, &serveraddr, serverlen); if (n < 0) error("ERROR in sendto"); /* print the server's reply */ n = recvfrom(sockfd, buf, strlen(buf), 0, &serveraddr, &serverlen); if (n < 0) error("ERROR in recvfrom"); printf("Echo from server: %s", buf); return 0; }
I obtained it from the following site :
https://www.cs.cmu.edu/afs/cs/academic/class/15213-f99/www/class26/udpclient.c
Now when I am going to run this code as it is than I am facing lot of error which is first of all function related to function gethostbyname. which say that it is not defined , so I want to know that how can I access this function in my code as I think since the BSD style network socket is supported in the TI-NDK so I would be able to run this code using the gethostbyname.
Now I did following changes to the code in order to build it successfully:
Void tcpHandler(UArg arg0, UArg arg1) { int sockfd, portno, n; int serverlen; struct sockaddr_in serveraddr; struct hostent *server; char *hostname; char buf[BUFSIZE]; /* check command line arguments */ hostname = "192.168.80.171"; portno = 8888; /* socket: create the socket */ sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) error("ERROR opening socket"); /* gethostbyname: get the server's DNS entry */ /*server = gethostbyname(hostname); if (server == NULL) { fprintf(stderr,"ERROR, no such host as %s\n", hostname); exit(0); }*/ /* build the server's Internet address */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; /*bcopy((char *)server->h_addr, (char *)&serveraddr.sin_addr.s_addr, server->h_length);*/ serveraddr.sin_addr.s_addr = inet_addr(hostname); serveraddr.sin_port = htons(portno); /* get a message from the user */ bzero(buf, BUFSIZE); printf("Please enter msg: "); fgets(buf, BUFSIZE, stdin); /* send the message to the server */ serverlen = sizeof(serveraddr); n = sendto(sockfd, buf, strlen(buf), 0, &serveraddr, serverlen); if (n < 0) error("ERROR in sendto"); /* print the server's reply */ n = recvfrom(sockfd, buf, strlen(buf), 0, &serveraddr, &serverlen); if (n < 0) error("ERROR in recvfrom"); printf("Echo from server: %s", buf); return 0; }
But this code crash showing this:
ss in flash Starting the TCP Echo example System provider is set to SysMin. Halt the target to view any SysMin contents in ROV. Network Added: If-1:192.168.80.154 os.knl.Task: line 383: E_spOutOfBounds: Task 0x20000d08 stack error, SP = 0x3e8. xdc.runtime.Error.raise: terminating execution
I have used the tcpecho code and removed all the contents in the tcpHandler function and put the content of the code in the main function from the site, as you can see in the code.
So please tell me what is this all about , if anybody wants to emulate the condition than all have to do is replace the content of the tcpHandler with the above code or just copy the above code as it is.
Please tell me why these problems are arising , through my lot of debugging one thing which I come to know is that inspite of BSD socket support by the NDK we can't use the example codes running on linux as it is.
regards