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, "the data has been sent!", 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, "data can not be sent!", Toast.LENGTH_SHORT) show ();. } super.run (); } }
Reply: Code did not see the problem more than try to change ip change the port Reply: The server address 255.255.255.255 you right? How is this address Reply: When using a socket UDP broadcasts Android is the need to use this IP address. Reply: Show When I use the Debug debugging in DatagramPacket packet = new DatagramPacket (buffer, buffer.length, serverAddress, 4567); This piece of code error, tried a lot of programs out of this line is always wrong. . . Reply:
Is it necessary to use 255.255.255.255, have this IP address???? Client how to send packets to the server side of it? I suggest you replace the server's ip Reply:
The server address 255.255.255.255 you right? How is this address
When using a socket UDP broadcasts Android is the need to use this IP address.
Is it necessary to use 255.255.255.255, have this IP address???? Client how to send packets to the server side of it? I suggest you replace the server's ip
Send data using UDP, just hair, regardless of the other party did not receive, so in principle, no matter how ip should be sent out, I have tried other ip but the same error. Reply: Address can not be so seemingly drops, usually the last field is 255, the same as the front with the LAN address. Recommend next network debugging assistant, first under test, look at the code in question is not Reply: Solved, in fact, the error lies solely with the use of a toast, android provisions can not directly manipulate the UI controls in the other thread, so when I use the UI controls are toast will complain, but there is another point for network operations, android provisions must open a new thread, or certainly will complain anyway thank a few. Give you. Reply: Yes. . If you want to update ui main thread, with Handler,,,
No comments:
Post a Comment