Android 套接字代码
为什么不能使用此代码打开套接字:
try {
Connect=(Button)findViewById(R.id.connect);
InetSocketAddress address = new InetSocketAddress("192.168.1.2",54628);
DatagramPacket request = new DatagramPacket(cmd.getBytes(), cmd.length(), address);
DatagramSocket socket = new DatagramSocket();
socket.send(request);
} catch (SocketException e) {
System.out.print("SSSS");
}
但当我这样做时它确实有效:
clientSocket= new Socket("192.168.1.2",54635);
Text.append( "\n created a socket");
错误日志:
05-24 23:04:58.497: ERROR/AudioPolicyManagerBase(95): unknown stream type
05-24 23:05:10.787: ERROR/AudioPolicyManagerBase(95): unknown stream type
05-24 23:05:10.787: ERROR/AudioPolicyManagerBase(95): unknown stream type
05-24 23:05:10.787: ERROR/AudioPolicyManagerBase(95): unknown stream type
05-24 23:05:10.787: ERROR/AudioPolicyManagerBase(95): unknown stream type
Why can't a socket be open with this code:
try {
Connect=(Button)findViewById(R.id.connect);
InetSocketAddress address = new InetSocketAddress("192.168.1.2",54628);
DatagramPacket request = new DatagramPacket(cmd.getBytes(), cmd.length(), address);
DatagramSocket socket = new DatagramSocket();
socket.send(request);
} catch (SocketException e) {
System.out.print("SSSS");
}
but it does work when I do:
clientSocket= new Socket("192.168.1.2",54635);
Text.append( "\n created a socket");
Error logs:
05-24 23:04:58.497: ERROR/AudioPolicyManagerBase(95): unknown stream type
05-24 23:05:10.787: ERROR/AudioPolicyManagerBase(95): unknown stream type
05-24 23:05:10.787: ERROR/AudioPolicyManagerBase(95): unknown stream type
05-24 23:05:10.787: ERROR/AudioPolicyManagerBase(95): unknown stream type
05-24 23:05:10.787: ERROR/AudioPolicyManagerBase(95): unknown stream type
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确实应该考虑回顾其他问题并接受答案(如果它们对您有用)。接受率为 0% 时,您获得帮助的可能性较小。
将 System.out.print() 语句更改为 e.printStackTrace();然后运行应用程序并复制 Logcat 的输出并将其粘贴到您的问题中。这将帮助我们了解到底出了什么问题。
You should really consider going back over your other questions and accepting answers if they worked for you. You are less likely to get help with 0% accept rate.
Change your System.out.print() statement to e.printStackTrace(); Then run the app and copy the output from your Logcat and paste it in your question. That will help us understand what is actually going wrong.