我将数据从 PC 发送到 Android。但我无法在 Android 中使用 UDP 接收数据。如何在 Android 中使用 UDP 接收数据?如何在 android-display 上查看接收到的数据?谁能帮我?
代码在这里:
public class MainActivity extends Activity {
EditText textOut;
EditText port;
int UDP_SERVER_PORT = 8255;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
try {
int port = UDP_SERVER_PORT;
Log.d("UDP", "Connecting..");
DatagramSocket clientsocket = new DatagramSocket(port);
byte[] receivebuf = new byte[1024];
DatagramPacket receivepacket =
new DatagramPacket(receivebuf,receivebuf.length);
clientsocket.receive(receivepacket);
String modifiedSentence = new String(receivepacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientsocket.close();
} catch (Exception e) {
Log.e("UDP", "C: Error", e);
}
};
}