将值发送到动态生成的IP地址

时间:2014-11-11 12:23:48

标签: android

我正在开发一个应用程序,其中充当服务器的移动应用程序为动态生成的IP提供值。任何人都可以帮助我实现这一机制。 我用java编写了一个客户端

package Client;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Simpleudpclient { public static void main(String[] args){
    DatagramSocket socket = null;
    DatagramPacket inPacket = null;
    DatagramPacket outPacket = null;
    byte[] inBuf, outBuf;
    final int PORT = 8888;
    String msg = null;

    try {
      InetAddress address = InetAddress.getByName("127.0.0.5");
      socket = new DatagramSocket();

      //Convert string to byte and send to server
      msg = "Hello";
      outBuf = msg.getBytes();
      outPacket = new DatagramPacket(outBuf, 0, outBuf.length,
              address, PORT);
      socket.send(outPacket);

      //Receive reversed message from server
      inBuf = new byte[256];
      inPacket = new DatagramPacket(inBuf, inBuf.length);
      socket.receive(inPacket);

      String data = new String(inPacket.getData(), 0, inPacket.getLength());

      System.out.println("Server : " + data);

    } catch (IOException ioe) {
      System.out.println(ioe);
    }
  }
}

0 个答案:

没有答案