如何在android应用程序中检查TCP套接字是否已连接
我正在编写一个简短的测试应用程序来练习连接到服务器。该应用程序所做的就是从 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)
I'm writing a short test app to practice connecting to a server. All the app does is take an IP from an editText box, and then connect to the server. It looks like I can connect to the server because I am able to send data to the server and have the server print it.
I wanted to add some error checking to confirm that I am connected before attempting to send anything to the server. However, the problem is, whenever I use the Socket.isConnected() or isBound() methods my app crashes.
So how do I check if I'm connected if these methods doesn't seem to work. As I said, I know I'm connected because I can send stuff to the server.
Below is the code, I connect on the press of a button. What I want to do is confirm that I'm connected, and then kick off a thread that will work in the background sending and receiving data from the server. In the section that say's s.isBound() is where the program crashes. I can even put in s.isConnected() and it will crash also.
Last, what is the difference between isBound, and 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();
}
}
}
}
};
This is what the log outputs.
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我同意您的代码在
TRY/CATCH
循环中放错位置可能是问题的根源。为了回答有关
isBound
和isConnected
的问题,以下是它们的定义。I agree that your code misplaced in the
TRY/CATCH
loop is probably the root of your problem.In answer to your question about
isBound
andisConnected
, here are their definitions.首先,您的
块位于错误的位置。如果任何 catch 块被触发,那么它将执行上面的块。但是,如果引发异常,则意味着
s
可能为 null,因此您会遇到 NullPointerException。 (应用程序崩溃)适当的位置位于try
块内。其次,正如您从logcat日志中看到的,您尝试建立的连接被拒绝。也许 ip/端口或防火墙有问题。
logcat 还会通知您可能有未捕获的异常。
解决该问题,如果问题仍然存在,请再次讨论。
Firstly, your
block is at a wrong place. If any catch block triggered, then it will execute the above block. But if an exception is raised that means that
s
will probably be null, so you have a certain NullPointerException. (crash of the application) The appropriate place is inside thetry
block.Secondly, as you can see from the logcat log, the connection that you are trying to establish refuses. Perhaps there is something wrong with the ip/port or a firewall.
Also logcat informs you that you probably have an uncaught exception.
Fix that problems and if the problem persist come again to discuss it.