如何将带缓冲区的byte []发送到服务器?

时间:2018-05-25 12:48:11

标签: android arrays sockets buffer

我尝试将一个带有ssl套接字和缓冲区的byte []发送到我的服务器,但是没有成功缓冲区。 我搜索了很多,看到一些主题看起来像我的,但我尝试了他们的代码(适应我的情况),但从未成功。你的帮助会很棒,谢谢。

编辑客户端(android):

byte[] bytes = null;

    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.test);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    bytes = stream.toByteArray();
    File file = new File(String.valueOf(bytes));
    ImageView mImageView = findViewById(R.id.imageView2);

    Bitmap bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(),
            R.drawable.test);
    mImageView.setImageBitmap(bitmap);

    if (socketConnect()) {
        try {
            Log.d("MainActivity", "Before OutputStream");
            OutputStream os = sock.getOutputStream();
            os.write(bytes);

            os.flush();
            os.close();
            //in.close();
            sock.close();
            Log.d("MainActivity", "End");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    else {
        Log.e("MainActivity", "Erreur");
    }

服务器端(python):

import socket
import sys
import ssl


sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("",9999))

sock.listen(10)

i=1

while True:
    (clientsocket, (ip, port)) = sock.accept()
    connstream = ssl.wrap_socket(clientsocket,
                                 server_side = True,
                                 certfile = "server.crt",
                                 keyfile = "server.key")
    f = open('file_'+str(i)+".png",'wb') #open in binary
    i=i+1
    print(i)
    l = 1
    l = connstream.read(1024)
    while (l):
        print("toto")
        f.write(l)
        l = connstream.read(1024)
    f.close()

    connstream.close()

sock.close()

当我运行我的代码时,似乎我的FileInputStream不起作用

感谢您的帮助

编辑:

Logcat:

05-25 15:41:45.993 16257-16257/? I/zygote: Not late-enabling -Xcheck:jni (already on)
05-25 15:41:46.020 16257-16257/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
05-25 15:41:46.477 16257-16257/com.youstiti.simon.testenvoiwithbuffer 
I/InstantRun: starting instant run server: is main process
05-25 15:41:46.952 16257-16257/com.youstiti.simon.testenvoiwithbuffer 
E/MainActivity: debut connexion socket
Choix bks version
05-25 15:41:46.953 16257-16257/com.youstiti.simon.testenvoiwithbuffer 
E/MainActivity: Bks recup
05-25 15:41:46.978 16257-16257/com.youstiti.simon.testenvoiwithbuffer 
E/MainActivity: Connexion socket
05-25 15:41:46.978 16257-16257/com.youstiti.simon.testenvoiwithbuffer 
D/MainActivity: Before OutputStream
05-25 15:41:47.002 16257-16257/com.youstiti.simon.testenvoiwithbuffer 
D/MainActivity: Before FileInputStream
05-25 15:41:47.002 16257-16257/com.youstiti.simon.testenvoiwithbuffer 
W/System.err: java.io.FileNotFoundException: [B@838f46d (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:200)
    at java.io.FileInputStream.<init>(FileInputStream.java:150)
    at com.youstiti.simon.testenvoiwithbuffer.MainActivity.onCreate(MainActivity.java:115)
    at android.app.Activity.performCreate(Activity.java:6975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
05-25 15:41:47.003 16257-16257/com.youstiti.simon.testenvoiwithbuffer 
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
05-25 15:41:47.021 16257-16282/com.youstiti.simon.testenvoiwithbuffer 
D/OpenGLRenderer: HWUI GL Pipeline
05-25 15:41:47.047 16257-16282/com.youstiti.simon.testenvoiwithbuffer 
I/OpenGLRenderer: Initialized EGL, version 1.4
05-25 15:41:47.047 16257-16282/com.youstiti.simon.testenvoiwithbuffer 
D/OpenGLRenderer: Swap behavior 1
05-25 15:41:47.047 16257-16282/com.youstiti.simon.testenvoiwithbuffer 
W/OpenGLRenderer: Failed to choose config with 
EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
05-25 15:41:47.047 16257-16282/com.youstiti.simon.testenvoiwithbuffer 
D/OpenGLRenderer: Swap behavior 0
05-25 15:41:47.052 16257-16282/com.youstiti.simon.testenvoiwithbuffer 
D/EGL_emulation: eglCreateContext: 0xa5a31e60: maj 2 min 0 rcv 2
05-25 15:41:47.061 16257-16282/com.youstiti.simon.testenvoiwithbuffer 
D/EGL_emulation: eglMakeCurrent: 0xa5a31e60: ver 2 0 (tinfo 0x99aa9d20)
05-25 15:41:47.101 16257-16282/com.youstiti.simon.testenvoiwithbuffer 
D/EGL_emulation: eglMakeCurrent: 0xa5a31e60: ver 2 0 (tinfo 0x99aa9d20)

编辑LOGCAT OUTOFMEMORY:

--------- beginning of crash
05-25 16:24:02.845 14571-14571/com.youstiti.simon.youstititest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.youstiti.simon.youstititest, PID: 14571
java.lang.OutOfMemoryError: Failed to allocate a 509239740 byte allocation with 33554432 free bytes and 445MB until OOM
    at java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:83)
    at java.lang.StringBuilder.<init>(StringBuilder.java:67)
    at java.util.Arrays.toString(Arrays.java:2062)
    at com.youstiti.simon.youstititest.activity.GalleryActivity$Upload.doInBackground(GalleryActivity.java:145)
    at com.youstiti.simon.youstititest.activity.GalleryActivity$Upload.<init>(GalleryActivity.java:94)
    at com.youstiti.simon.youstititest.activity.GalleryActivity.onActivityResult(GalleryActivity.java:84)
    at android.app.Activity.dispatchActivityResult(Activity.java:6463)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3738)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3785)
    at android.app.ActivityThread.access$1400(ActivityThread.java:157)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5525)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

1 个答案:

答案 0 :(得分:0)

  

文件file = new File(String.valueOf(bytes));

这是胡说八道。你的内存中有字节。您不需要File类

  

FileInputStream in = new FileInputStream(bytes);

这也是胡说八道。您不需要FileInputStream,其次您不能以这种方式将其用于字节。那会编译吗?真的?

你可以取消所有这些。你唯一需要的是:

os.write(bytes);