繁体   English   中英

Labview与Java的TCP连接

[英]Labview TCP connection with Java

我正在尝试通过TCP套接字从Labview发送数据,并使用Java接收数据。

我使用的是Labview的示例TCP VI(我无法发布图片)。

我意识到有一个TCP读取,但我还没到那点。 我的问题是处理类型。

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();
    }
}

我要处理的第一个问题是,当我收到来自Labview VI的输入时,会在Java程序中得到:

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

我以为我的问题出在类型转换上,但我真的不知道如何解决。 任何帮助,将不胜感激。

好的,然后尝试类似:

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

只需将其放在代码中的某个位置即可。 这只是将返回的int强制转换为char值,这将打印出字母。

让我知道事情的后续。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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