0

I'm trying to send data from Labview over a TCP socket and receiving the data with Java.

I'm using an example TCP VI from Labview (I cant post pictures).

I realize there's a TCP read, I haven't gotten to that point yet. My problem is dealing with types.

import java.io.*;
import java.net.*;

public class JavaApplication3 {
    public static void main(String[] args) throws IOException {

        String serverHostname = new String ("97.77.53.127");

        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            echoSocket = new Socket(serverHostname, 6340);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                                        echoSocket.getInputStream()));
        } catch (UnknownHostException e) {

            System.exit(1);
        } catch (IOException e) {
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(
                                       new InputStreamReader(System.in));
        String userInput;

        System.out.print ("input: ");
        while ((userInput = stdIn.readLine()) != null) {
            out.println(userInput);
            System.out.println("echo: " + in.readLine());
            System.out.print ("input: ");
        }

        out.close();
        in.close();
        stdIn.close();
        echoSocket.close();
    }
}

My first problem I want to deal with, is whenever I receive a input from the Labview VI, in the java program I get:

input: d
echo: ?��/�?�~gʕ ?�$���;P?��G��j�?��"�?�?��;���h?�
input: input: d
echo: ?��/�?�~gʕ ?�$���;P?��G��j�?��"�?�?��;���h?�
input: 

I'm assuming my problem is with type casting, but I really don't know enough to fix it. Any help would be appreciated.

Pokechu22
  • 4,984
  • 9
  • 37
  • 62
Ryan
  • 1
  • Hi, you could try instead of `in.readLine()`, using `in.read()`. You will need to loop since `read()` read a char value. So if you read and get `-1`, then end of stream has been reached. If this prints out numbers, then they are most likely char values, and you'll have to convert them. Let us know if that's the case. – Alvin Bunk Oct 22 '14 at 00:14
  • Hi, first off, thanks for the help! I switched to in.read() and I did start getting numbers like: 63, 65533, 104, 76 etc. My labview program converts a 1d array of doubles into a string, not sure if that changes anything. – Ryan Oct 23 '14 at 01:32

1 Answers1

1

Ok, then try something like:

int temp = 0;
while( (temp = in.read()) != -1){
   System.out.print( (char)temp );
}

Just put this somewhere in your code. This simply casts the int returned to a char value, which will print out the letters.

Let me know how it goes.

Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45