问题
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