使用Java将ASCII字符发送到服务器

时间:2017-06-06 03:05:11

标签: java sockets

我正在尝试使用Socket将计算机与设备连接。

使用我的代码,我设法使用套接字连接到另一台设备 连接工作正常,但问题是我连接的设备有自己的SDK,而且是ASCII字符。
当我尝试使用String发送命令(来自SDK)时,代码不起作用,请提供建议。

String Address = txtAddress.getText();
    Socket MyClient;
    try {
        MyClient = new Socket(Address,9993);
        os = new DataOutputStream(MyClient.getOutputStream());
        is = new DataInputStream(MyClient.getInputStream());
        this.main.showUserView();
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
         lble.setText("Don't know about host: hostname");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        lble.setText("Couldn't get I/O for the connection to: hostname");
    }

1 个答案:

答案 0 :(得分:0)

也许您可以尝试使用DatagramPacket发送数据?

int PORT = ## (port we are using for communication)

byte[] strBytes = myString.getBytes();

InetAddress address = InetAddress.getByName(ipAddressString);
DatagramPacket packet = new DatagramPacket(strBytes, strBytes.length, address, PORT);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();