开发者

exception while trying to receive a file from server

i run some code to connect to a server, send it an Object and then receive another Object. while running this on my Android enulator (v2.2) i get - java.net.SocketException: Socket is closed the connection to the server is successful and i'm able to send the object but when i'm trying to do socket.getInputStream() it throws the exception

this is my connector class:

public class ConnectionToServer {
    UserProblemRequest sentProblem;
    Problem responseProblem;
    Socket socket;

    public ConnectionToServer(){
        sentProblem = null;
        responseProblem = null;
        socket = null;
    }

    public void connect(){
        try {
            Log.d(TAG, "Connecting...");
            socket = new Socket(Utils.SERVER_IP, Utils.SERVER_PORT);
            Log.d(TAG, "SUCCESS: Connected!");
        } catch (UnknownHostException e) {
            Log.d(TAG, "ERROR: Falied to connect! (UnknownHostException)");
            e.printStackTrace();
        } catch (IOException e) {
            Log.d(TAG, "ERROR: Falied to connect! (IOException)");
            e.printStackTrace();
        }
    }

    public void setProblemFromByteArray(byte[] data, boolean isFile){
        sentProblem = new UserProblemRequest();
        sentProblem.fileBArray = data.clone();
        if (isFile){
            sentProblem.requestType = Utils.requestType_IMAGE;
        }
        else {
            sentProblem.requestType = Utils.RequestType_STRING;
        }
    }

    public void sendProblem(){

        ObjectOutputStream os;

        try {
            os = new ObjectOutputStream(socket.getOutputStream());
            Log.d(TAG, "Sending file to server...");
            os.writeObject(sentProblem);
            os.flush();
            os.close();
        } catch (IOException e) {
            Log.d(TAG, "ERROR: Falied to send file to server!");
            e.printStackTrace();
        }
        Log.d(TAG, "SUCCESS: File sent to server!");
    }

    public void closeConnection(){
        try {
            socket.close();
        } catch (IOException e) {
            Log.d(TAG, "failed to close socket");
            e.printStackTrace();
        }
    }

    public Problem reciveResponseFromServer(){

        ObjectInputStream ois;

        try {
            ois = new ObjectInputStream(socket.getInputStream());
            responseProblem = (Problem) ois.readObject();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        retur开发者_运维知识库n responseProblem;
    }

}

and i use this code to run it:

ConnectionToServer serverConnection = new ConnectionToServer();
serverConnection.connect();
serverConnection.setProblemFromByteArray(temp_data, true);
serverConnection.sendProblem();
Problem responseProblem = serverConnection.reciveResponseFromServer();
serverConnection.closeConnection();

any ideas?


Does closing the ObjectOutputStream() in sendProblem() wind up closing the underlying socket so that when you getInputStream() in reciveResponseFromServer() the socket is already closed?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜