Android平板电脑和PC之间的通信

时间:2013-12-17 07:49:32

标签: c# android communication

我从Android开始,在我的第一个应用程序中,我需要建立一个与Android桌面之间的通信PC。通信是由直接IP直接进行的,因为我需要的是当我有几台PC和平板电脑时,每个表只与它的PC通信。

从平板电脑到Pc的通讯已经有效,但从PC到桌面我都无法获得数据传输

Android方

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是表IP,SERVERPORT2是4445 当我启动应用程序时,它在显示“Server:Receiving”但

之后仍在等待数据

C#代码

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();
}

我想,任何愚蠢的想法都会让我忘记但是在阅读了很多论坛和书籍之后我就停止了这一点

欢迎任何建议

1 个答案:

答案 0 :(得分:0)

您使用UDP连接并且socket.receive(数据包)不等待数据包。如果缓冲区中没有数据包,则此操作会抛出异常。 尝试将您的代码更改为:

@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;
            }
        }
    }