2013年9月15日星期日

Android UDP broadcasts can not send data

Debug debugging socket.send (packet); wrong solution . . . .
class ClientClass extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
try {
InetAddress serverAddress=InetAddress.getByName("255.255.255.255");
int messageCount=0;  
DatagramSocket socket=new DatagramSocket();
byte[] buffer=localIpAddress.getBytes();
DatagramPacket packet=new DatagramPacket(buffer,buffer.length,serverAddress,4567);
while (true) {
try {
socket.send(packet);
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("Send Data:" +  new String(packet.getData()));  
Toast.makeText(MainActivity.this, "数据已发送!", Toast.LENGTH_SHORT).show();
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
messageCount++;  
buffer = (localIpAddress + messageCount).getBytes();  
                    packet.setData(buffer,0,buffer.length);  
}

} catch (Exception e) {
// TODO: handle exception
Toast.makeText(MainActivity.this, "数据未能发送!", Toast.LENGTH_SHORT).show();
}
super.run();
}
}

------ Solution ------------------------------------- -------
code did not see the problem for more than try to change the port ip
------ Solution ------- -------------------------------------
255.255.255.255 server address you this right ? How this address is
------ Solution ---------------------------------- ----------

when Android uses socket UDP broadcasting, is the need to use the IP address.  
Does must 255.255.255.255, have this IP address ???? client how the packets and sends it to the server ?
suggest you replace server 's ip
------ Solution ----------------------------- ---------------
seemingly address can not drop , are generally the last field is 255 , the same as the front with the LAN address . Advice network debugging assistant , first under test , look at the code is not the problem
------ eference ---------------------- -----------------

when Android uses socket UDP broadcasting, is the need to use the IP address.
------ eference --------------------------------------- < br>
when I use the Debug debugging displayed in DatagramPacket packet = new DatagramPacket (buffer, buffer.length, serverAddress, 4567); this is a code error , tried a lot of programs , always an error in this line out . . .
------ eference --------------------------------------- < br>
when Android uses socket UDP broadcasting, is the need to use the IP address.          
Does must 255.255.255.255, have this IP address ???? client how the packets and sends it to the server ?   
suggest you replace server ip  
send data using UDP , just hair, regardless of the other side has not received , so the principle is that no matter how ip should be sent out , I also tried the other ip but the same error .
------ eference --------------------------------------- < br> is solved, the error lies solely with the use of a toast , android provisions can not operate directly in other threads UI controls , so when I use the UI controls is toast will complain , but there is another thing is for the network operation , android provisions must open up a new thread , or certainly will complain anyway thank a few . Points to you.

没有评论:

发表评论