07-18-2017 11:50 AM - edited 07-18-2017 11:51 AM
Overview:
I am trying to create TCP IPv6 communication for my sbRIO9607, running Linux RT, in LabVIEW. I am programming in the C language in the Eclipse IDE for my tests.
Complete:
I got some C code to compile and run on my sbRIO using Eclipse. The sever code and client code both run and exchange data through the localhost. I've attached my programs to this thread.
Code:
For the first part of the server code (see complete file in attached zip), I have:
// START C CODE
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
// Main
int main(void) {
// INPUTS
int portno = 3344;
// Declare Vars
char buffer[256]; // Can recieve 256 characters of information
int sockfd, newsockfd, tempVar;
socklen_t clilen;
struct sockaddr_in6 serv_addr, cli_addr;
int n;
char client_addr_ipv6[100];
// Start Msg
printf("\nIPv6 TCP Server Started...\n");
// Sockets Layer Call: socket()
sockfd = socket(AF_INET6, SOCK_STREAM, 0); // Returns int sockfd on success; Returns int -1 on error;
// printf("\nAF_INET6 = %i\n\nSOCK_STREAM = %i\n",AF_INET6, SOCK_STREAM); // int AF_INET6 = 10; int SOCK_STREAM = 1;
// socket() Error Catch
if (sockfd < 0) {
perror("\nServer: Socket Open Error\n");
exit(1);
} else {
printf("\nServer Socket Open Success --- Sockfd = %i\n", sockfd);
}
// Parse Addr
bzero((char *) &serv_addr, sizeof(serv_addr)); // Sets aside memory for serv_addr structure (sizeof() returns uint16 number of bytes) (8 bit = 1 byte)
serv_addr.sin6_flowinfo = 0;
serv_addr.sin6_family = AF_INET6; // AF_INET6 = 10;
serv_addr.sin6_addr = in6addr_any;
serv_addr.sin6_port = htons(portno); // Convert the port number to network byte order
// Sockets Layer Call: bind()
tempVar = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
// END C CODE
Question:
1. How do I wrap the code in bold so that I can call it from LabVIEW to get a pointer to the structure into LabVIEW that I can pass to the bind() and the sizeof() functions? If possible, can I create the .so from Eclipse or do I need another software/IDE?
Thanks.
07-19-2017 04:19 AM
Well you would best just pass in a byte array of at least 32 bytes, treat that inside the C function as your struct sockaddr_in6 pointer and fill in the right parameters and then pass it to the bind function.
Unlike what your comment in your code says, bzero() does not "set aside" any memory but only clears the memory area that you allocated implicitedly on the global heap when declaring the variable. To do what you asked you would need to create a function with a pointer sized integer passed by reference, but returning pointers to global heap memory is pure madness. Not that it couldn't work but it goes against any and every proper coding convention to create maintainable code that can be called safely from a multithreading environment like LabVIEW!
07-19-2017 05:07 AM
Thanks, but I'm a bit confused by your answer. Are you saying that it is possible to do what I am asking, but it is bad practice? If so, what would you do in this situation?
Also, if I am going to create my own .so file in Eclipse to get the structure, can I follow this tutorial? http://zone.ni.com/reference/en-XX/help/371361B-01/lvhowto/completing_c_file/
In this tutorial, can I add as many includes as I need so I can call the bind function and get the prototypes for my structure, like in my .o code?
Thanks.
07-19-2017 12:08 PM
Well you still should first do at least a semester course in C programming to understand the basics of C, before trying to attempt this adventure. Most of what you ask is basic C programming knowledge and has in fact very little to do with LabVIEW. There is also no way I could teach you the necessary basics in a few short posts here.
As to the link you show, it is in priciple right but at the same time specifically for development of shared libraries under Windows and/or the Pharlap RT system and is going to at least confuse you a bit since certain things are different due to the fact that you create an ELF shared library (no need to link with the LabVIEW.lib import library) and the use of the gcc compiler instead of Visual C.