怪异的行为:发送从Android手机图像Java服务器(code工作) [英] Weird behavior : sending image from Android phone to Java server (code working)

查看:90
本文介绍了怪异的行为:发送从Android手机图像Java服务器(code工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Android应用程序发送的图像运行的Java应用程序服务器,它的工作在一个非常奇怪的方式!

下面是我做的。


  1. 编译并运行在桌面上的Java应用程序接收充当服务器的一部分

  2. 编译,部署和运行具有将图像发送到服务器的Andr​​oid部分。 Android应用程序执行完毕,但Java应用程序并不

  3. 运行Android应用程序的活动,有code再次发送图像,这时候,Android应用程序进度对话框卡住,但是,在Java应用程序执行完毕,也图像被成功转移ð..

以下是code代表的Java应用程序的接收部分:

 类的ProjectServer
{
    ServerSocket的serSock;
    插座袜子;    在的BufferedReader;
    PrintWriter的出来;
    公共静态无效的主要(字符串AR [])
    {
        尝试
        {
            为ProjectServer CS =新的ProjectServer();
            cs.startServer();
        }
        赶上(例外五)
        {        }
    }    公共无效startserver的()
    {
        尝试
        {
            serSock =新的ServerSocket(8070);
            的System.out.println(等待客户端...);
            袜子= serSock.accept();            的System.out.println(连接完成);        //接受文件
            的System.out.println(已连接);            //收到code
            INT文件大小= 450660;
            INT读取动作;
            INT电流= 0;
            //接收文件
            字节[] = mybytearray新的字节[文件大小]
            InputStream为= sock.getInputStream();
            FOS的FileOutputStream =新的FileOutputStream(C:\\\\的Project Server \\\\ Capture.png);
            BOS的BufferedOutputStream =新的BufferedOutputStream(FOS);
            读取动作= is.​​read(mybytearray,0,mybytearray.length);
            电流=读取动作;            做{
               读取动作=
                  is.read(mybytearray,电流,(mybytearray.length电流));
               如果(读取动作> = 0)电流+ =读取动作;
            }而(读取动作-1个);            bos.write(mybytearray,0,电流);
            bos.flush();            的System.out.println(结束的开始);
        }
        赶上(例外五)
        {
            的System.out.println(E);
            e.printStackTrace();
        }    }
}

以下是code对Android应用程序SEND部分:

 包com.site.custom;
公共类ACT2扩展活动
{
    私人ProgressDialog PD;
    私人字符串服务器IP =58.146.100.187;
    在私人的BufferedReader;
    私人的PrintWriter出来;
    私人字符串路径;
    私人插座cliSock;    公共无效的onCreate(捆绑onCreateInstance)
    {
        super.onCreate(onCreateInstance);
        的setContentView(R.layout.act2);
        this.setTitle(这已启动);
        PATH = getIntent()getStringExtra(路径)。        //建立连接
        尝试
        {
            cliSock =新的Socket(服务器IP,8070);
            在=新的BufferedReader(新的InputStreamReader(cliSock.getInputStream()));            ((的TextView)findViewById(R.id.tview))的setText(路径)。
        }
        赶上(例外五)
        {
            Log.v(MERA MSG,e.toString());
        }        //发送文件
        ProgressDialog PD = ProgressDialog.show(这一点,发送图像,图像选择:+ path.substring(path.lastIndexOf(//)+ 1),假的,真正的);        尝试
        {
            myfile文件=新的文件(路径);
            的System.out.println((int)的myFile.length());
            字节[] = mybytearray新的字节[450560]。
            FIS的FileInputStream =新的FileInputStream(MYFILE);
            二BufferedInputStream为=新的BufferedInputStream(FIS);
            bis.read(mybytearray,0,mybytearray.length);
            OutputStream的OS = cliSock.getOutputStream();
            的System.out.println(发送...);
            os.write(mybytearray,0,mybytearray.length);
            os.flush();
            的System.out.println(已完成);            pd.dismiss();
            的System.out.println(完成);
        }
        赶上(例外五)
        {
            Log.v(MERA MSG,e.toString());
        }    }
}


解决方案

你不关闭任何连接。您的服务器可能是等待,直到知道数据已经完成发送之前关闭连接。当你的客户端运行的第二次,第一次连接可能是自动由Android关闭,这让你的服务器来处理图像。但是,如果您的服务器不处理图像速度不够快,也许你的客户端获取第二运行'卡住',因为它在等待成功的端口连接到服务器,服务器正忙。

长话短说,你怎么样尝试的close()客户端连接,当你完成它。调用刷新()并没有真正告诉任何服务器说,该数据已完成发送。

如果你想,即使你关闭的OutputStream ,你可以写一个自定义的套接字保持开放插座只是覆盖的close()的方法,这样的事情...

 公共类MySocket扩展了Socket {    公共MySocket(字符串ip地址,端口INT){
        超(ip地址,端口);
    }    公共无效的close(){
        // 没做什么
    }    公共无效reallyClose(){
        super.close();
    }
}

在您的code,变化 cliSock =新的Socket(服务器IP,8070); cliSock =新MySocket(服务器IP,8070) ;

当你调用 OutputStream.close(),它调用的close() MySocket ,但你可以看到它实际上并没有做任何事情。当你已经完全与插座完成,你可以叫 MySocket.reallyClose(); ,它会关闭它关闭正常。

I'm writing an android app that sends an image to a server running a java app, and it's working in a very weird way!

Here is what I do

  1. Compile and run the Java app on the desktop with the receive part that acts as server
  2. Compile,deploy and run the android part that has to send the image to the server. The android app finishes execution but the java app doesn't
  3. Run the android app activity that has the code to send the image AGAIN, and this time, the android app progress dialog gets stuck, BUT, the java app finishes execution and also the image is transferred d successfully...

Following is the code for the RECEIVE part of the Java app:

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();
        }

    }
}

Following is the code for the SEND part on the Android app:

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());
        }

    }
}

解决方案

You're not closing any of the connections. Your Server is probably waiting until the connection is closed before it knows that data has finished being sent. When your Client runs the second time, the first connection is probably closed automatically by Android, this allowing your Server to process the image. However, if your Server doesn't process the image fast enough, maybe your Client gets 'stuck' on the second run because its waiting for the successful port connection to the Server but the Server is busy.

Long story short, how about you try to close() the Client connection when you're finished with it. Calling flush() doesn't really tell anything to the Server to say that the data has finished being sent.

If you want the socket to stay open even though you close the OutputStream, you could write a custom Socket that just overwrites the close() method, something like this...

public class MySocket extends Socket {

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

    public void close(){
        // do nothing
    }

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

In your code, change cliSock=new Socket(serverIP,8070); to cliSock=new MySocket(serverIP,8070);

When you call OutputStream.close(), it calls close() on MySocket, but as you can see it doesn't actually do anything. When you've completely finished with the Socket, you can call MySocket.reallyClose(); and it'll close it off properly.

这篇关于怪异的行为:发送从Android手机图像Java服务器(code工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆