0

I'm trying to establish and udp connection between a server and an android device. I have written the server and android client part. When I launch the server on my computer localhost, both server and emulator can communicate with each other very well. However, when I launch the server in cloud, emulator can send udp packets to server but can't receive any.

So, first thing come to my mind was that there may be some routing issues that prevents the packets to reach to the emulator. Hence, I have used my mobile phone (samsung galaxy s2, ICS) with a 3g connection. However, same thing occured again. My phone can send udp packets but it can't receive any.

Then, I thought that maybe my machine in cloud can't send packets. SO, I have sniffed the packets with wireshark and seen that it really sends/receives the udp packets.

I have read many questions about this issue in stackoverflow but I couldn't come up with a solution. Maybe you guys help me out. Here is the server and client udp send/receive parts of my work. If you need more information, I'm ready to give.

Thanks in advance.

Server (no need to write listening parts 'cuz no problem with that)

ClientIPAddress = IncomingPacket.getAddress();
ClientPort = IncomingPacket.getPort();
OutToClientSocket = new DatagramSocket();
DatagramPacket PacketToClient = new DatagramPacket(sendBuffer, sendBuffer.length,     ClientIPAddress, ClientPort);
OutToClientSocket.send(PacketToClient);

Client (only listening part)

ServerIP = InetAddress.getByName(MachineName);
DatagramPacket IncomingPacket = new DatagramPacket(buf, buf.length);
MySocket.receive(IncomingPacket);
buf = IncomingPacket.getData();
...
4
  • Why are you creating a second DatagramSocket in the server? What's wrong with the one you received the message on?
    – user207421
    Oct 2, 2012 at 21:08
  • @EJP it runs on a different thread
    – user1078192
    Oct 2, 2012 at 21:13
  • 1
    That's not a sufficient reason. You can still use the same socket, and you should do so.
    – user207421
    Oct 2, 2012 at 21:32
  • @EJP you were exactly right. thank you. It worked when I use 1 socket for sending and receiving.
    – user1078192
    Oct 4, 2012 at 17:40

1 Answer 1

0

I have solved my problem thanks to @EJP . I was using a datagramsocket for listening packets and using another one to send packets. You have to use 1 socket for both operations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.