Strange behavior: sending an image from an Android phone to a Java server (working with code)

I am writing an Android application that sends an image to a server with a java application, and it works very weird!

That's what I'm doing

  • Compile and run the Java application on the desktop with the reception part that acts as a server
  • Compile, deploy and run the part of Android that should send the image to the server. Android application completes execution but java application does not execute
  • Run the action of the Android application, which has the code for sending the AGAIN image, and this time the Android application progress dialog box closes, BUT, the Java application terminates the execution, and the image is successfully transmitted ...

The following is the code for the RECEIVE part of the Java application:

class ProjectServer
{
    ServerSocket serSock;
    Socket  sock;

    BufferedReader in;
    PrintWriter out;
    public static void main(String ar[])
    {
        try
        {
            ProjectServer cs=new ProjectServer();
            cs.startServer();
        }
        catch(Exception e)
        {

        }
    }

    public void startServer()
    {
        try
        {
            serSock=new ServerSocket(8070);
            System.out.println("Waiting for client...");
            sock=serSock.accept();

            System.out.println("Connections done");

        //Accept File
            System.out.println("Connected");

            //receive code
            int filesize=450660;
            int bytesRead;
            int current=0;
            // receive file
            byte [] mybytearray  = new byte [filesize];
            InputStream is = sock.getInputStream();
            FileOutputStream fos = new FileOutputStream("C:\\Project Server\\Capture.png");
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            bytesRead = is.read(mybytearray,0,mybytearray.length);
            current = bytesRead;

            do {
               bytesRead =
                  is.read(mybytearray, current, (mybytearray.length-current));
               if(bytesRead >= 0) current += bytesRead;
            } while(bytesRead > -1);

            bos.write(mybytearray, 0 , current);
            bos.flush();

            System.out.println("end-start");    
        }
        catch(Exception e)
        {
            System.out.println(e);
            e.printStackTrace();
        }

    }
}

SEND Android:

package com.site.custom;
public class Act2 extends Activity
{
    private ProgressDialog pd;
    private String serverIP="58.146.100.187";
    private BufferedReader in;
    private PrintWriter out;
    private String path;
    private Socket cliSock;

    public void onCreate(Bundle onCreateInstance)
    {
        super.onCreate(onCreateInstance);
        setContentView(R.layout.act2);
        this.setTitle("This has started");
        path=getIntent().getStringExtra("path");

        //Establish Connection
        try
        {
            cliSock=new Socket(serverIP,8070);
            in=new BufferedReader(new InputStreamReader(cliSock.getInputStream()));

            ((TextView)findViewById(R.id.tview)).setText(path);
        }
        catch(Exception e)
        {
            Log.v("MERA MSG",e.toString());
        }

        //Send file
        ProgressDialog pd=ProgressDialog.show(this, "Sending image", "Image chosen:"+path.substring(path.lastIndexOf("//")+1),false,true);

        try
        {
            File myFile = new File (path);
            System.out.println((int)myFile.length());
            byte[] mybytearray  = new byte[450560];
            FileInputStream fis = new FileInputStream(myFile);
            BufferedInputStream bis = new BufferedInputStream(fis);
            bis.read(mybytearray,0,mybytearray.length);
            OutputStream os = cliSock.getOutputStream();
            System.out.println("Sending...");
            os.write(mybytearray,0,mybytearray.length);
            os.flush();
            System.out.println("Completed");

            pd.dismiss();
            System.out.println("Done");
        }
        catch(Exception e)
        {
            Log.v("MERA MSG",e.toString());
        }

    }
}
+3
1

- . , , , , . , , , Android, . , , , "" , , .

, , close() , . flush() , , .

, , OutputStream, Socket, close(), - ...

public class MySocket extends Socket {

    public MySocket(String ipAddress, int port){
        super(ipAddress,port);
    }

    public void close(){
        // do nothing
    }

    public void reallyClose(){
        super.close();
    }
}

cliSock=new Socket(serverIP,8070); cliSock=new MySocket(serverIP,8070);

OutputStream.close(), close() MySocket, , , . Socket, MySocket.reallyClose();, .

+1

All Articles