0

我正在创建一个应用程序,用于连接到连接到 mysql 数据库的 java 实现的服务器,从服务器端一切正常,在 android 上每次我连接到服务器时我都可以发送消息但是当读取响应套接字时关闭,我想问题来自杀死线程的VM,我google'it并尝试将套接字类作为活动运行,然后作为线程我什至尝试优先考虑线程,但我总是得到相同的错误:

12-28 18:28:24.769: W/System.err(1342): java.net.SocketException: Socket closed
12-28 18:28:24.769: W/System.err(1342):     at org.apache.harmony.luni.platform.OSNetworkSystem.read(Native Method)
12-28 18:28:24.769: W/System.err(1342):     at dalvik.system.BlockGuard$WrappedNetworkSystem.read(BlockGuard.java:273)
12-28 18:28:24.769: W/System.err(1342):     at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:458)
12-28 18:28:24.769: W/System.err(1342):     at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:59)
12-28 18:28:24.769: W/System.err(1342):     at java.io.DataInputStream.readLine(DataInputStream.java:310)
12-28 18:28:24.769: W/System.err(1342):     at com.example.lightreading.ConnectionDB$clientCon.run(ConnectionDB.java:83)
12-28 18:28:24.769: W/System.err(1342):     at java.lang.Thread.run(Thread.java:1019)

这是我使用线程的代码:

public class ConnectionDB{

public static String ip;
public static int port;
private static ConnectionDB instance = null;

private StringBuilder sb;
private String response = "";
private String data;


public static ConnectionDB getInstance() {
      if(instance == null) {
         instance = new ConnectionDB();
      }
      return instance;
}

public String connect(String s_send) throws InterruptedException{
    data = s_send;
    Thread cThread = new Thread(new clientCon());
    cThread.setPriority(Thread.MAX_PRIORITY);
    cThread.start();

    return response.toString();
}


class clientCon implements Runnable {

    private Socket socket = null;
    private DataOutputStream dataOutputStream = null;
    private DataInputStream dataInputStream = null;
    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
              socket = new Socket(ip,port);
              dataOutputStream = new DataOutputStream(socket.getOutputStream());
              dataInputStream = new DataInputStream(socket.getInputStream());
              Log.d("sending msg: ",data);
              dataOutputStream.writeBytes(data);
              dataOutputStream.flush();
              dataOutputStream.close();
              Log.d("msg sent: ", data);

              while ((data = dataInputStream.readLine()) != null) {
                    Log.d("data from server: ", data);
                    sb.append(data);
              }
              Log.d("msg received: ", sb.toString());
             } catch (UnknownHostException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             }
        if (sb != null){
            response = sb.toString();
            Log.d("Response from server: ", response);
        }else{
            Log.d("Response from server is null", "");
        }

    }

}

}

4

1 回答 1

0

我有同样的问题,你必须 socket.shutdownOutput(); 为了让套接字准备好传输数据

于 2013-08-01T18:42:38.763 回答