简体   繁体   中英

Labview TCP connection with Java

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM