我使用套接字编程尝试了这个tutorial。但是,在使用工具&amp ;;进行套接字编程时,它无法向服务器发送消息。接口。你认为我可以使用工具和放大器进行套接字编程吗?接口??还没有“hello”调试消息。
private class OnReadyListener implements MyCustomDialog.ReadyListener
{
@Override
public void ready(String name)
{
try
{
DatagramSocket clientSocket = new DatagramSocket();
String serverHostname = new String("192.168.1.12");
InetAddress IPAddress = InetAddress.getByName(serverHostname);
byte[] sendData = new byte[1024];
String sentence = "hello";
Log.d(TAG, "OnReadyListener ready" + " " + sentence );
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
clientSocket.close();
}
catch (UnknownHostException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
答案 0 :(得分:0)
这里是以不同于你的形式实现的代码,但它是纯粹的套接字编程并且运行良好。 http://thinkandroid.wordpress.com/2010/03/27/incorporating-socket-programming-into-your-applications/