如何在Android中读取套接字输入流
can anybody tell me how to read socket input stream. Here is my code.
if (!serverIpAddress.equals(""))
{
try
{
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.i("ClientActivity", "Connecting...");
Socket socket = new Socket(serverAddr, 5777);
try {
Log.i("ClientActivity", "Sending command.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
// where you issue the commands
out.println("<maplist />");
Log.i("ClientActivity", "Sent.");
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
buffer = new StringBuilder();
new Thread(new Runnable() {
@Override
public void run() {
try
{
buffer.append(input.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
this.WriteXMLResponse(buffer.toString());
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
在我的代码和我正在读取输入流的 run 方法中,发生了异常。上面写着 java.net.socketException: 错误的文件号。
如果我使用 while 循环,我的应用程序就会冻结。 任何建议将不胜感激。 谢谢
can anybody tell me how to read socket input stream. Here is my code.
if (!serverIpAddress.equals(""))
{
try
{
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.i("ClientActivity", "Connecting...");
Socket socket = new Socket(serverAddr, 5777);
try {
Log.i("ClientActivity", "Sending command.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
// where you issue the commands
out.println("<maplist />");
Log.i("ClientActivity", "Sent.");
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
buffer = new StringBuilder();
new Thread(new Runnable() {
@Override
public void run() {
try
{
buffer.append(input.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
this.WriteXMLResponse(buffer.toString());
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
In my code and in run method where i am reading the input Stream a exception occurs. which says java.net.socketException: bad file number.
and if i use while loop my app freezes.
any suggestion will be appretiative.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有看到更多代码,很难判断发生了什么,但这是我对这里发生的事情的推论(我在 Android 应用程序中完成了大量套接字编程):
您正在创建 Socket 并包装应用程序主线程上 BufferedReader 中的输入流,然后构造一个新线程来等待,直到在套接字上获取输入并随后处理该输入。但是,当您创建新线程时,该代码将在后台执行,同时所有这一切发生的方法(您声明 Socket 和 BufferedReader 的方法)正在完成!当该方法完成时,不再有对 Socket 的引用,因此它会被运行时关闭并进行垃圾收集。不幸的是,您的后台线程不知道它的套接字同时已关闭,因此 readLine() 最终抛出一个异常,指出套接字(fileno,unix-wise)现在“坏”,因为它被关闭了运行时。
您最初假设 while 循环可能会修复它,并且它会修复它,因为它不允许该方法完成,从而保持 Socket 打开,但问题是在您的应用程序等待时整个 UI 冻结用于来自套接字的输入。
真正的解决方案是在与应用程序 UI 不同的线程上运行的服务中执行所有套接字通信。
我尽力解释这一点,但如果有任何部分不清楚,请发表评论,我会尝试进一步解释或为您提供更具体的帮助。
It's a little bit hard to tell what's going on without seeing more of your code, but here's my deduction of what's going on here (I've done quite a bit of socket programming in android apps):
You're creating the Socket and wrapping the input stream in a BufferedReader on the application's main thread and then constructing a new thread to wait until you get input on the socket and subsequently process that input. However, when you create the new thread, that code goes to execute in the background, and in the meantime the method that all this is happening in (the method where you're declaring the Socket and BufferedReader) is finishing! When that method finishes, there is no more reference to the Socket, so it's closed and garbage-collected by the runtime. Unfortunately, your background thread has no idea that it's socket has been closed in the meantime, so it's readLine() ends up throwing an exception stating that the socket (fileno, unix-wise) is now "bad" since it was closed by the runtime.
You were somewhat right in initially assuming that a while loop might fix it, and it would, because it wouldn't allow the method to finish, thereby keeping the Socket open, but the problem is that the entire UI freezes while your app is waiting for input from the socket.
The true solution to this is to do all your socket communication in a Service running on a separate thread from the UI of the application.
I did my best to explain this, but if any parts aren't clear, just comment, and I'll try and explain further or give you more specific help.