将 Java 客户端与 Python 服务器连接起来 [英] Conect Java client with Python server

查看:40
本文介绍了将 Java 客户端与 Python 服务器连接起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下一个代码制作了一个服务器 python(我认为其余的代码不是必需的):

I made a server python with the next code (i think the rest of the code isn't necessary):

while 1:
    data = conn.recv(BUFFER_SIZE)
    if not data: break
    print "received data:", data
    conn.send(data+'\r\n')

conn.close()

当我尝试使用 Java 客户端接收回声时,在控制台上打印时收到一条奇怪的消息:

and when i try to received the echo with a Java client, i get a strange message when printing on console:

Mensaje recibido = java.io.DataInputStream@1cf2a0ef

在服务器点,我收到了很好的消息:Hola Mundo.

At server point, I'm receiving good the message: Hola Mundo.

Java 中的代码是:

The code in Java is:

    DataInputStream ims;
    String data;

    s = new Socket(HOST,PORT);

    oms = new DataOutputStream(s.getOutputStream());
    ims = new DataInputStream(s.getInputStream());

    oms.writeUTF(ms);
    ims.read();
    data = ims.toString();

    oms.close();
    ims.close();
    s.close();

    return data;

我认为 ims.toString() 可能是错误的.

I think that ims.toString() is probably wrong.

推荐答案

您是对的,ims.toString 是错误的.如果 python 发送 UTF-8 字符串,您可以使用 ims.readUTF().

You are correct that ims.toString is wrong. If python is sending a UTF-8 string you could use ims.readUTF().

这篇关于将 Java 客户端与 Python 服务器连接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆