UDP将数据从一个设备发送到另一个设备

时间:2013-03-24 01:23:00

标签: java android networking udp

我试图通过一个小设备从一个设备发送到另一个设备, 我的设备IP是192.168.1.59,这是主机, 这是我的服务器代码

public class gameServer extends Thread{

/**
 * Sets up a server for Android applciation
 */
private static final String TAG = "GameServer";
private DatagramSocket socket;
private int port = 50000;
private InetAddress local = null;

public gameServer(  ) throws IOException
{
    socket = new DatagramSocket( port );
    Log.d(TAG, "Server was setup");
}

private String getLocalIPAddress()
{
    try
    {
        for (Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); nis.hasMoreElements();)
        {
            NetworkInterface ni = nis.nextElement();
            Log.v(TAG, "NetworkInterface = " + ni.getDisplayName());
            for (Enumeration<InetAddress> ips = ni.getInetAddresses(); ips.hasMoreElements();)
            {
                InetAddress ip = ips.nextElement();
                String s = ip.getHostAddress();
                Log.v(TAG, "InetAddress = " + s);
                if (!ip.isLoopbackAddress())
                {
                            if(InetAddressUtils.isIPv4Address(s)) return s;
                }
            }
        }
    }
    catch (SocketException e)
    {
            Log.e(TAG,"getLocalIPAddress()", e);
    }
    return null;
}

public void passClient( gameObject clientTemp )
{

}

@Override
public void run()
{
    Log.d(TAG, "Ip address used:" + getLocalIPAddress() );
    while( true )
    {
        //Send some data
        try 
        {
            local = InetAddress.getByName("127.0.0.1");
        } 
        catch (UnknownHostException e2) {
            e2.printStackTrace();
        }
        String msg = "hello there";
        int msgLength = msg.length();
        byte[] message = msg.getBytes();
        DatagramPacket p = new DatagramPacket( message, msgLength, local, port );
        try 
        {
            socket.send( p );
        } 
        catch (IOException e2) 
        {
            Log.d(TAG, "Error with sending");
            e2.printStackTrace();
        }

    }
}
}

这是我的客户

public class gameClient extends Thread {

private static final String TAG = "gameClient";
private gameServer server;
private boolean rdyForPlay = false;
private ArrayList<gameObject> assets = new ArrayList();
private int ID = 0;
private maths cal = new maths();

public gameClient( boolean serverTag )
{
    if( serverTag == true)
    {
        try 
        {
            server = new gameServer();
            server.run();
        } 
        catch (IOException e) 
        {
            Log.d(TAG, "Could not start server");
            e.printStackTrace();
        }
    }
    else
    {
        //DELETE!!!
        //ONLY FOR TESTING
        DatagramSocket socket = null;;
        try 
        {
            socket = new DatagramSocket( 50000 );
        } 
        catch (SocketException e1) 
        {
            e1.printStackTrace();
        }

        byte[] buf = new byte[256];
        DatagramPacket packet = new DatagramPacket( buf, buf.length );
        try 
        {
            socket.receive( packet );
        } 
        catch (IOException e) 
        {
            Log.d(TAG, "Error with receiving data");
            e.printStackTrace();
        }

        String data = new String( buf, 0, packet.getLength() );

        Log.d(TAG, "Data received was :" + data);


    }
}

public void sendTouchEvent(float xSet, float ySet)
{

}

@Override
public void run()
{

}

private void updateAssets()
{

}
}

当代码尝试接收数据包时,它会在socjet.receive(数据包)上崩溃; 任何人都可以看到任何理由吗?

帆布

1 个答案:

答案 0 :(得分:1)

您的问题是您有两个try块。如果第一个抓住了某个套接字null。所以做这样的事情:

 DatagramSocket socket = null;
    try 
    {
        socket = new DatagramSocket( 50000 );
        byte[] buf = new byte[256];
        DatagramPacket packet = new DatagramPacket( buf, buf.length );
        socket.receive( packet );
    } 
    catch (SocketException e1) 
    {
        e1.printStackTrace();
    }
    catch (IOException e) 
    {
        Log.d(TAG, "Error with receiving data");
        e.printStackTrace();
    }

确保close()您的活动的套接字onDestroy!

还要考虑自动发现而不是静态IP:AutoDiscovery