gpt4 book ai didi

android - 如何检查TCP套接字是否在Android应用程序中连接

转载 作者:可可西里 更新时间:2023-11-01 02:34:21 25 4
gpt4 key购买 nike

我正在编写一个简短的测试应用程序来练习连接到服务器。该应用程序所做的只是从 editText 框中获取 IP,然后连接到服务器。看起来我可以连接到服务器,因为我可以将数据发送到服务器并让服务器打印它。

我想添加一些错误检查以在尝试向服务器发送任何内容之前确认我已连接。但是,问题是,每当我使用 Socket.isConnected() 或 isBound() 方法时,我的应用程序就会崩溃。

如果这些方法似乎不起作用,我该如何检查我是否已连接。正如我所说,我知道我已连接,因为我可以将内容发送到服务器。

下面是代码,我按下一个按钮就连接了。我想要做的是确认我已连接,然后启动一个线程,该线程将在后台工作,从服务器发送和接收数据。在说 s.isBound() 的部分中,程序崩溃了。我什至可以放入 s.isConnected(),它也会崩溃。

最后,isBound和isConnected有什么区别?

private OnClickListener connectListener = new OnClickListener() {            
@Override
public void onClick(View v) {
if (!connected) {
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals("")) {
try{
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "Trying to Connect");
s = new Socket(serverAddr, SERVERPORT);
Log.d("ClientActivity", "Connected");

output = s.getOutputStream();

input = s.getInputStream();

}
catch (UnknownHostException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}

if(s.isBound()){
connected = true;
cThread = new Thread(new ClientThread());
cThread.setName("Client Connection Thread");
cThread.start();
}

}
}
}
};

这是日志输出的内容。

11-13 17:03:56.718: D/ClientActivity(2039): Trying to Connect
11-13 17:03:56.757: W/System.err(2039): java.net.ConnectException: /192.168.16.1:6340 - Connection refused
11-13 17:03:56.757: W/System.err(2039): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:207)
11-13 17:03:56.757: W/System.err(2039): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
11-13 17:03:56.757: W/System.err(2039): at java.net.Socket.startupSocket(Socket.java:705)
11-13 17:03:56.757: W/System.err(2039): at java.net.Socket.<init>(Socket.java:263)
11-13 17:03:56.757: W/System.err(2039): at com.AUIEE.client_test.Client_TestActivity$1.onClick(Client_TestActivity.java:88)
11-13 17:03:56.757: W/System.err(2039): at android.view.View.performClick(View.java:2485)
11-13 17:03:56.765: W/System.err(2039): at android.view.View$PerformClick.run(View.java:9089)
11-13 17:03:56.765: W/System.err(2039): at android.os.Handler.handleCallback(Handler.java:587)
11-13 17:03:56.765: W/System.err(2039): at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 17:03:56.773: W/System.err(2039): at android.os.Looper.loop(Looper.java:130)
11-13 17:03:56.773: W/System.err(2039): at android.app.ActivityThread.main(ActivityThread.java:3859)
11-13 17:03:56.773: W/System.err(2039): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 17:03:56.773: W/System.err(2039): at java.lang.reflect.Method.invoke(Method.java:507)
11-13 17:03:56.773: W/System.err(2039): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:840)
11-13 17:03:56.773: W/System.err(2039): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
11-13 17:03:56.773: W/System.err(2039): at dalvik.system.NativeStart.main(Native Method)
11-13 17:03:56.781: D/AndroidRuntime(2039): Shutting down VM
11-13 17:03:56.781: W/dalvikvm(2039): threadid=1: thread exiting with uncaught exception (group=0x4001e560)
11-13 17:03:56.789: E/AndroidRuntime(2039): FATAL EXCEPTION: main
11-13 17:03:56.789: E/AndroidRuntime(2039): java.lang.NullPointerException
11-13 17:03:56.789: E/AndroidRuntime(2039): at com.AUIEE.client_test.Client_TestActivity$1.onClick(Client_TestActivity.java:103)
11-13 17:03:56.789: E/AndroidRuntime(2039): at android.view.View.performClick(View.java:2485)
11-13 17:03:56.789: E/AndroidRuntime(2039): at android.view.View$PerformClick.run(View.java:9089)
11-13 17:03:56.789: E/AndroidRuntime(2039): at android.os.Handler.handleCallback(Handler.java:587)
11-13 17:03:56.789: E/AndroidRuntime(2039): at android.os.Handler.dispatchMessage(Handler.java:92)
11-13 17:03:56.789: E/AndroidRuntime(2039): at android.os.Looper.loop(Looper.java:130)
11-13 17:03:56.789: E/AndroidRuntime(2039): at android.app.ActivityThread.main(ActivityThread.java:3859)
11-13 17:03:56.789: E/AndroidRuntime(2039): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 17:03:56.789: E/AndroidRuntime(2039): at java.lang.reflect.Method.invoke(Method.java:507)
11-13 17:03:56.789: E/AndroidRuntime(2039): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:840)
11-13 17:03:56.789: E/AndroidRuntime(2039): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
11-13 17:03:56.789: E/AndroidRuntime(2039): at dalvik.system.NativeStart.main(Native Method)

最佳答案

我同意您在 TRY/CATCH 循环中错放的代码可能是问题的根源。

在回答您有关 isBoundisConnected 的问题时,以下是它们的定义。

public boolean isBound ()

Returns whether this socket is bound to a local address and port.
Returns true if the socket is bound to a local address, false otherwise.


public boolean isConnected ()

Returns whether this socket is connected to a remote host.
Returns true if the socket is connected, false otherwise.

关于android - 如何检查TCP套接字是否在Android应用程序中连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8126083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com