Home TCP Socket communication Java - Python
Reply: 1

TCP Socket communication Java - Python

newUser.java
1#
newUser.java Published in 2017-12-07 22:50:14Z

I want to transmit an integer number throught a socket. I have a python script that open a socket and wait for requests, in the other side there is a Java servlet that send a request at this socket to obtain a value.

All the communication works well the problem is that the value obtained from java cause a EOFexception. I've tried some convertion and formats in the python script (I'm not expert of this language) but nothing works.

Instead, if I use in python a echo socket, picking all that socket receive and send back to java, all working fine.

But I need to send a simple value.

The following is the portion of code that I use, I've tried with float, this is with a String value:

[Python socket related istructions]

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(server_address)
sock.listen(1)
connection = sock.accept()

[Python echo socket working]

data = connection.recv(16)
if data:
    connection.sendall(data)

instead I want to do:

[Python return a example value]

 connection.sendall('55')

That working if I send request from a client like Hercules but cause a EOFexception in Java

[Java Servlet - send request]

socket = new Socket(address, port);
dataInputStream = new DataInputStream(socket.getInputStream());
String content = dataInputStream.readUTF(); //EOFexception
Destination.value = content;
newUser.java
2#
newUser.java Reply to 2017-12-08 00:02:53Z

Solved the problem by using InputStream instead of DataInputStream, write the python socket to send string as byte and changed the java receiver to read byte stream and convert in to string (good format for my purpose).

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.394343 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO