LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

problem with tcp server sending data to java

Hello,

 

I need to create a server in LabVIEW which will send data to a JAVA client program on the same computer. Eventually I hope to be able to send a stream of data from LabVIEW to JAVA, but for now I'm trying with a string. I followed the Simple Data Server example in Labview to build the server, just changing it so as to send a string (see attached image). I also wrote a client program in JAVA that simply reads in whatever data is written from the server.

 

So I run the server and then the client, and then I write a word in LabVIEW and press enter. That word does not appear in JAVA. Rather what appears are all the words I had entered previously, running in a continuous loop. So if a close the server and client, and then run them again, lo and behold the word I had written before now appears in JAVA, in addition to everything else before. All I want is that the word I enter appear directly in JAVA, and nothing else.

 

I am not an expert in neither LabVIEW, JAVA, or network programming, so I just want to know where the problem is and how I should tackle it. I have no idea what is going wrong. If there is nothing wrong with LabVIEW then at least I will know the problem is in JAVA, although I doubt it since it is very simple code.

 

Any help will be appreciated!!

Hugh

0 Kudos
Message 1 of 8
(4,602 Views)

Hi HughTh,

your control value will be read only ones. If you want to change the string, then you have to put the control inside the while loop. You should also use an event structure to send the data only if you want to send it.

 

Mike

0 Kudos
Message 2 of 8
(4,597 Views)

Hi MikeS81,

 

Thanks for your help. I tried putting the string control inside the while loop, but it is still the same, more or less. Actually, now in JAVA I get an infinite stream of the word I had written before, and the word I write appears when you stop running the server. I really don't undderstand this at all. Could it be that I need to do something such as flush, close or clear the buffer/socket in LabVIEW?

 

I also looked up events in LabVIEW, and I don't think I really understood what you meant. You mean to put a button, eg "send" which, once pressed will send the string? The thing is, in the LabVIEW Simple Server example that comes with LabVIEW, they didn't use an event structure, and it worked, which is why I am confused:S

 

0 Kudos
Message 3 of 8
(4,576 Views)

Without seeing the Java code it is difficult to say why it is reading the data in strange ways, i.e. only reading it when you close the connection at the server. This problem is something in your Java code, not the LabVIEW code. Is your client written in such a way that it will read 4 bytes of data to determine how many bytes it must then read to get the string? If not, this could be the source of your issue on the client side. If it is simply trying to read n number of bytes with no timeout it will not complete its read until the connection is closed.

 

Your server code is sending the value of the control at the moment the client connects repeatedly every 100 ms. It does prepend the length which is a good thing to do.What Mike was suggesting is that you use an event structure to control when data gets sent. As you assumed he meant that nothing will be sent until you hit the "Send" button. You don't need to do a flush or any other type of action. You certainly don't want to close the connect. Although this somewhat depends on how you define your communication between the client and the server. In general though the server will leave the connection open until the cleint closes it.

 

I would recommend you use a network sniffer such as Wireshark to help you understand what is happening. This will allow you to capture and analyze the netowkr traffic between the client and the server. It may help you to see what is happening.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 4 of 8
(4,568 Views)

Hi Mark,

 

Thanks for the info! In Java I'm doing something like this (sorry I know this is not a Java forum but just in case it can clarify matters):

 

BufferedReader in = null;

String fromServer = null;

in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));

while ((in.readLine()) != null) {
            fromServer = in.readLine();
            System.out.println("Server: " + fromServer);
        }
        in.close();

 

So I gather from what you said that I should read the first 4 bytes and these bytes will contain the size of the string I want to read? This might seem like a stupid question, but why 4 bytes?? And after having read them, how do I get the length? I mean, how is the length represented in the bytes?

 

At any rate, having put the string control inside the while loop, can I consider my LabVIEW code to be correct or should I still put an event structure? I'm having some difficulty understanding how to implement that so if it can be done without it I would prefer it!

 

Thanks a lot again,

Hugh

 

 

0 Kudos
Message 5 of 8
(4,555 Views)

Hi Hugh,

you should read the 4 Byte at first because you write it into the front of your string in LabVIEW. You read the size from the string (I32) transfrom it into a string and write before your other data. So in Java you should first read 4 bytes, the length of the following string as I32 and then the rest of your data.

 

Mike

Message Edited by MikeS81 on 03-31-2010 08:09 PM
0 Kudos
Message 6 of 8
(4,552 Views)

Hi Mike,

 

Thanks once again for your help. I tried to read the first four bytes and then convert to int to get the length, but I got a HUGE number. So large in fact, that I got an out of memory error in Java when I tried to initialize a byte array of that size to read the rest.

 

I thought it could be an endian sort of problem, but I tried reversing the four bytes that I read and I got just as large a number.

 

How exactly does LabVIEW send this length? And what exactly is I32? LabVIEW encodes as I32 or I should convert to I32? I'm sorry, I really didn't understand a lot! I know this is not the place to ask this, but if I could just have a clear picture of what it is I am searching for it would help a lot.

 

Thanks again,

Hugh

0 Kudos
Message 7 of 8
(4,529 Views)
LabVIEW will send the data in network byte order. If your data length is 8, then the first four bytes of the data sent would be 0x00, 0x00, 0x00 and 0x08. Can you post an example of what you are sending and what is being read by the Java program.


Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 8 of 8
(4,514 Views)