Question

I am starting on Android and in my first application I need to establish a communication between and android table a PC. The communication is direct by staqtic IPs as I need that when I have several PCs and tablets each table only communicates with its PC.

The communication from the tablet to the Pc is already working but from the PC to the table I cannot get data transfered

Android side

public class Server implements Runnable 
{
    @Override
    public void run() 
    {
        while (always==true)
        {
            while(start2==false)
            {
            }
            try 
            {
                InetAddress serverAddr = InetAddress.getByName("192.168.173.133");
                updatetrack("\nServer: Start connecting\n");
                //*DatagramSocket socket = new DatagramSocket(SERVERPORT2, serverAddr);/
                DatagramSocket socket = new DatagramSocket(SERVERPORT2);

                byte[] buf = new byte[17];
                DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, SERVERPORT2);
                //*DatagramPacket packet = new DatagramPacket(buf, buf.length);/
                updatetrack("Server: Receiving\n");
                socket.receive(packet);
                updatetrack("Server: Message received: '" + new String(packet.getData()) + "'\n");
                updatetrack("Server: Succeed!\n");
                start2=false;

            } 
            catch (Exception e) 
            {
                updatetrack("Server: Error!\n");
                start2=false;
            }
        }
    }
}    

192.168.173.133 is the table IP and SERVERPORT2 is 4445 When I start the application it remains waiting for data after displaying "Server: Receiving" but

C# code

public static void Main()
{
        IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("192.168.173.133"), 4445);
        string hostname = Dns.GetHostName();
        byte[] data = Encoding.ASCII.GetBytes(hostname);
        sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
        sock.SendTo(data, iep2);
        sock.Close();
}

I suppose that it is any silly think I am forgotten but after reading many forums and books I am stopped on this point

Any advise will be welcome

Was it helpful?

Solution

You use UDP connection and socket.receive(packet) doesn't wait for packet. If there isn't packet in the buffer this operation throw exception. Try to change your code to:

@Override
    public void run() 
    {
        while (always==true)
        {
            while(start2==false)
            {
            }
            try 
            {
                InetAddress serverAddr = InetAddress.getByName("192.168.173.133");
                updatetrack("\nServer: Start connecting\n");
                //*DatagramSocket socket = new DatagramSocket(SERVERPORT2, serverAddr);/
                DatagramSocket socket = new DatagramSocket(SERVERPORT2);

                while (always==true)
                {
                 try{
                byte[] buf = new byte[17];
                DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, SERVERPORT2);
                //*DatagramPacket packet = new DatagramPacket(buf, buf.length);/
                updatetrack("Server: Receiving\n");
                socket.receive(packet);
                updatetrack("Server: Message received: '" + new String(packet.getData()) + "'\n");
                updatetrack("Server: Succeed!\n");
                start2=false;
                }
                catch(Exception ex) {ex.printStackTrace();}
                }


            } 
            catch (Exception e) 
            {
                updatetrack("Server: Error!\n");
                start2=false;
            }
        }
    } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top