1

Androidフォンでマルチキャストソケットが動作しています.1つのデバイスを別のデバイスに接続しようとしています.1つのデバイスはwifiホットポットとしてホストされており、もう1つのクライアントは接続するだけです.クライアントが必要とするポートを見つけるにはどうすればよいですか.聞く、パケットを受信する?

これが私のサーバーコードです

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
        {

        }
    }

}

}
4

0 に答える 0