Android 套接字代码

发布于 2024-11-09 15:44:50 字数 1106 浏览 0 评论 0原文

为什么不能使用此代码打开套接字:

                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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖 2024-11-16 15:44:51

您确实应该考虑回顾其他问题并接受答案(如果它们对您有用)。接受率为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文