0

我正在尝试将一个小数据包从一个设备发送到另一个设备,我的设备 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(packet); 上崩溃。任何人都可以看到任何原因吗?

帆布

4

1 回答 1

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 !

还要考虑 AutoDiscovery 而不是静态 IP:AutoDiscovery

于 2013-03-24T02:08:26.513 回答