1
여러 가지 방법으로이 작업을 수행했지만 Java UDP 패킷을 처음 사용하는 데는 도움이되지 않았습니다.UDP 보내기 및 받기
Android 용 내 코드는 서비스를 통해 시작되며 새로운 스레드에서 실행됩니다. 대기에 대한
코드 :
try {
int port = 58452;
// Create a socket to listen on the port.
DatagramSocket dsocket = new DatagramSocket(port);
// Create a buffer to read datagrams into. If a
// packet is larger than this buffer, the
// excess will simply be discarded!
byte[] buffer = new byte[2048];
// Create a packet to receive data into the buffer
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
// Now loop forever, waiting to receive packets and printing them.
while (true) {
// Wait to receive a datagram
dsocket.receive(packet);
// Convert the contents to a string, and display them
String msg = new String(buffer, 0, packet.getLength());
System.out.println(packet.getAddress().getHostName() + ": "
+ msg);
// Reset the length of the packet before reusing it.
packet.setLength(buffer.length);
}
} catch (Exception e) {
System.err.println(e);
}
코드 보내는이 :
try {
String host = "MY PHONES IP";
int port = 58452; //Random Port
byte[] message = "LAWL,LAWL,LAWL".getBytes();
// Get the internet address of the specified host
InetAddress address = InetAddress.getByName(host);
// Initialize a datagram packet with data and address
DatagramPacket packet = new DatagramPacket(message, message.length,
address, port);
// Create a datagram socket, send the packet through it, close it.
DatagramSocket dsocket = new DatagramSocket();
dsocket.send(packet);
dsocket.close();
System.out.println("Sent");
} catch (Exception e) {
System.err.println(e);
}
}
이 잘 전송하지만 안드로이드에받을 수 없습니다. 도와주세요!
또한 내 로그 캣 출력 : http://pastebin.com/Rfw5mSKV
감사 -Fusion
동일한 기기로 수신을 시도하셨습니까? 올바른 IP인지 라우터가 포트를 필터링하지 않습니까? 언뜻보기에 코드는 괜찮아 보입니다 ... – Fildor
그래, 10 번 정도 포트를 시도했습니다. 나는 같은 기계에서 시도 할 것이다! – Austin