我试过这个tutorial使用套接字编程。但是,使用实现和接口(interface)进行套接字编程时,无法向服务器发送消息。你认为我可以用实现和接口(interface)进行套接字编程吗?也没有“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();
}
}
}
最佳答案
这里的代码实现形式与您的不同,但它是纯套接字编程并且运行良好。 http://thinkandroid.wordpress.com/2010/03/27/incorporating-socket-programming-into-your-applications/
关于java - 套接字编程的实现和接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7550473/