I'm 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 sending a message from Java to Python the python server receives, but the opposite does not happen. Apparently the server sends the bytes but the client waits infinitely, indicating that nothing has been received.
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();
}
}
}