Communication between Python and Java

Asked

Viewed 582 times

1

I am trying to create a client server application where the server will be written in python and the client in Java. The problem is that when you send a message from Java to Python the python server gets it, but otherwise it doesn’t. Apparently the server sends the bytes but the client is waiting endlessly, indicating that it received nothing.

Python code

import socket

HOST = ''
PORT = 5000

udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
origem = (HOST,PORT)
udp.bind(origem)
msg, client = udp.recvfrom(1000)
print(client," - ",msg)
msg = "recebido"
dest = ('192.168.1.4',5000)
echo = "resposta"
udp.sendto(echo.encode('ascii'),dest)
udp.close()

Java code

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {

    public static void main(String[] args) {

        String msg = "conectado";
        byte[] buffer = msg.getBytes();

        try {
            DatagramSocket socket = new DatagramSocket();
            //Enviando pacote
            DatagramPacket pacote = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("192.168.1.4"), 5000);
            socket.send(pacote);

            //Receebendo pacote
            buffer = new byte[1000];
            pacote.setData(buffer);
            pacote.setLength(1000);
            socket.receive(pacote);

            //Convertendo arraybyte para String
            String conteudo = new String(buffer);
            System.out.println(conteudo);

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
  • UDP does not guarantee the delivery of the package, maybe it arrives or not, understood?

  • I tested with TCP as well and the result is the same. The test is being done with the server and the client running on the same machine (localhost). I also did the test using Matlab and it didn’t work either. Matlab can send but does not receive. Any idea of what might be?

  • I’m sure it could be the firewall.

  • Your firewall is blocking the incoming stream(Inbound).

  • I’ve disabled the firewall and problem persists. I think it is data type incompatibility, that is, the python byte array is not the same thing as the java byte array. I don’t know how to check this kind of problem.

  • 1

    I tested it here and it worked normal. Byte is the same thing in any language, the only thing that changes is the order of bytes, but that’s only if you run from machines that have different architectures.

  • Does Voce really need for your application to implement socket-level communication? There is a reason why they have created protocols that abstract the socket, such as http, ftp, websocket, jsonrpc, etc...

  • You’re using the same port for both codes, maybe that’s the problem, try to change the doors to 5000 and 5001

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.