Conect Java client with Python server

妖精的绣舞 提交于 2019-12-25 02:57:22

问题


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

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

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

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;

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


回答1:


What you need is to assign data to a string read from the stream:

data=ims.readUTF();

the toString() method in the stream just returns a representation of the object id, it's meaningless.

See http://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html#readUTF()




回答2:


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



来源:https://stackoverflow.com/questions/29829036/conect-java-client-with-python-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!