Android,如何获取正确的UDP地址

时间:2013-03-19 00:54:25

标签: java android networking wifi

我的Android手机上有多个插件工作,我正在尝试连接另一个,一个设备作为wifi火锅托管,另一个客户端只是连接,我如何找到该端口客户端需要监听,接收数据包吗?

这是我的服务器代码

public class gameServer extends Thread{

/**
 * Sets up a server for Android applciation
 */

//Variables
private static final String TAG = "gameServer";
private int port = 50000;
private MulticastSocket socket = null;
private InetAddress address = null;
private ArrayList<gameObject> assets = new ArrayList();

//Client
private DatagramPacket packet;


public gameServer() throws IOException
{
    //Setup server
    try
    {
        socket = new MulticastSocket( port );
        Log.d(TAG, "Server started");
    }
    catch ( IOException e)
    {
        Log.d(TAG, "Error code 0004");
        e.printStackTrace();
    }
}

public void passClient( gameObject clientTemp )
{
    assets.add( clientTemp );
}

@Override
public void run()
{
    InetAddress temper = socket.getInetAddress();
    Log.d(TAG, "Server thread was started");
    System.out.println(temper);
    try {
        address = InetAddress.getByName("230.0.0.1");
    } catch (UnknownHostException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        socket.joinGroup(address);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    while( true )
    {
    //Send out data
    try {
    //Setup the data to send to clients
    byte[] buf = new byte[256];
    String dataToBeSent = "";

    for(int i = 0;i < assets.size(); i++)
    {
        dataToBeSent += assets.get(i).returnPosX() + ":" + assets.get(i).returnPosY();
    }

    buf = dataToBeSent.getBytes();

    // send it
    InetAddress group = InetAddress.getByName("230.0.0.1");
    DatagramPacket packet = new DatagramPacket(buf, buf.length, group, port);
    socket.send(packet);
    }
    catch (IOException e) 
    {
        e.printStackTrace();
    }

    //Receive data
    byte[] buf = new byte[256];
    //Set the properties of the packet (DatagramPacket)
    packet = new DatagramPacket(buf, buf.length);
    //Receive the data
    try {
        socket.receive(packet);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //Conver the data to a string
    String  received = new String(packet.getData(), 0, packet.getLength());

    //Debug
    //Log.d(TAG, received);

    }
}
}

客户端代码

public class gameClient extends Thread {

private static final String TAG = "gameClient";
private gameObject player;
private Context context;
private int port = 50000;
private MulticastSocket socket;
private InetAddress address = null;
private DatagramPacket packet = null;

public gameClient( gameObject temp )
{
    player = temp;
    try {
        socket = new MulticastSocket( port );
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public gameObject rtnObj()
{
    return player;
}

@Override
public void run()
{
    //Gather ip address
    try {
        address = InetAddress.getByName("192.168.1.1");
    } catch (UnknownHostException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        socket.joinGroup(address);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    while( true )
    {
        //Send out data
        try {
            //Receive data
            byte[] buf = new byte[256];
            //Set the properties of the packet (DatagramPacket)
            packet = new DatagramPacket(buf, buf.length);
            //Receive the data
            try {
                socket.receive(packet);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //Conver the data to a string
            String  received = new String(packet.getData(), 0, packet.getLength());
            String[] posValues = received.split(":");


            player.setPosition(Integer.parseInt(posValues[0]), Integer.parseInt(posValues[1]));

            //Debug
            Log.d(TAG, received);

        }
        finally
        {

        }
    }

}

}

0 个答案:

没有答案
相关问题