This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C1294NCPDT: Other microcontrollers forum

Part Number: TM4C1294NCPDT

We have been working on a project with UDP communication.  The requirement is to send data periodically over ethernet to UDP client. With much difficulty we modified the UDP example code. But does not seem to be working. Tried debugging the code, the "sendto" function was returning -1 value.The code snippet below. Could you guide me how to do this. Will the old NDK version support UDP communication? Thank you in advance.

Void udp_func(int argc, char **argv)
{

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;
serveraddr.sin_len = sizeof(serveraddr);
serveraddr.sin_addr.s_addr = inet_addr("192.168.0.184");
serveraddr.sin_port = htons(2000);

strcpy(buf,"test_data");

/* 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;

}

  • RAJ BS said:
    I modified the UDP example code... Could you guide me how to do this.

    You've 'very well' described your issue - yet your (vital) Subject Line (pardon) conveys 'Little of Value/Interest.'   (to 'draw in' (proper) support)

    You CAN 'edit' that subject line ... and "Difficulty w/modified UDP example code" - far better 'highlights/presents' your requirement - & thus is expected to prove a superior 'lure.'

  • Hi Raj,

    First, can you confirm that you got the out of the box TI-RTOS UDP Echo example to work properly. 

    Also, what version of TI-RTOS are you using?

    Did you use WireShark to see if anything was sent out of the device over Ethernet?

    Todd

  • Yes, Thank you for your assistance, I have done it.

  • Yes, we tried UDP Echo example, it was working fine.What we are finding difficulty is sending data to the UDP client. we are using RTOS version 6.0.1.
    When we tried using the wireshark, we were getting data at the server side but not at the client side. Thank you.

  • Hi Raj,

    Can you right-click on your project in CCS and open Properties, then click the CCS General tab, then Products and show me the products your project is using so that I make sure I look at the correct code:

    Regards,

    Brandon

  • Hello,

    Thank you for replying, Here is the screenshot of the same.

  • Hi Raj,

    Thanks for providing that screenshot. I'm not seeing anything immediately wrong with your udp_func and the -1 return code doesn't tell us very much. Are you able to step up and into the function call for sendto() with the debugger? Maybe you could narrow down where the function is running into trouble. It could be at SockConnect(), SockSend(), etc. It would be very helpful to find the origin of the error, if possible.

    You mentioned that you are getting data at the server side, but not the client side. Just to clarify, the code above is from your server, correct? And the sendto() call above is meant for your client, which fails. But the recvfrom call just after it is successful? And it's just one client and one server in your setup? Is that all right?

    Regards,

    Brandon

  • Hi Brandon,

    There was a minor mistake in which i mentioned. The data was NOT getting received at the UDP server side, while configuring as UDP client. The above mentioned code portion mentions the same. Regret the inconvenience. Thank you

  • Hi Raj,

    My mistake for not noticing the comment. Have you been able to step into the sendto() call with the debugger and observed a reason for the error?

    Regards,

    Brandon

  • Hi Brandon,

    Thank you for writing for us.I regret the delay. we were able to step into the "Sendto" function, step by step ,went deep into the libraries and at some point, nothing further. I assume we are going wrong somewhere at "11enter" function. Please let us know if something strikes. The screenshot has been given below. 

  • Hi Raj,

    Hmm. Also, how are you starting this udp_func? The function signature doesn't match the signature of a Task. I wonder if something in your setup is incorrect? Can you expand on what steps you took to go from the udpEcho example to your current project?

    Regards,

    Brandon

  • Hello Brandon,

    From the existing udp_func(), the only changes we made in this project are we changed the "serveraddr.sin_addr.s_addr = inet_addr("192.168.0.184")" and 
    "serveraddr.sin_port = htons(2000)" , which is our system IP and port number.

  • Hello Brandon,

    The problem is resolved. We were trying to bind and listen(using "sendto" function) to the same IP and Port, which we didn't notice was creating the problem. When we gave separate IP and port for listening, it started working fine. We appreciate your help, expecting more interaction in future.