1

Wi-Fi経由で2台のAndroidデバイスを接続し、データを送信しようとしています。クライアント側のソケットは接続済みと表示されていますが、サーバーは受け入れ前に移動していません。それがどのように可能かさえわかりません。以下は私のサーバーとクライアントのコードです。デバイスの1つで実行されているwifiホットスポットを使用して両方のデバイスを接続しました。

サーバ側:

    try
    {
          ServerSocket servsock = new ServerSocket(4149);

              Log.d("VR","Waiting");
                text.setText("waiting"); //code reaching upto here



              Socket sock = servsock.accept();
              //System.out.println("Accepted connection11 : " + sock);
              text.setText("this is acc"+servsock); 
              //Log.d("VR","Accepted");
                text.setText("accepted");


           // receive file
                byte [] mybytearray  = new byte [filesize];
                InputStream is = sock.getInputStream();
                FileOutputStream fos = new FileOutputStream("/sdcard/WebOffice.jpg"); // destination path and name of file
                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();

                long end = System.currentTimeMillis();
                System.out.println(end-start);
                bos.close();



              sock.close();


              Log.d("VR","Socket closed");

                  Log.d("VR","image should be loaded by now");
                    text.setText("image should be loaded by now");


                Intent i  = new Intent(this,ImageDisplay.class);
                startActivity(i);








    }
    catch (Exception e) {
        // TODO: handle exception
    }

クライアント側:

試す {

                    ThreadPolicy tp = ThreadPolicy.LAX;
                    StrictMode.setThreadPolicy(tp);
                    ip = et.getText().toString();
                    sock = new Socket("192.168.43.182",4149); 

                    status.setText("connecting");
                    status.setText("connected"+sock);

                    System.out.println("Connecting...");

                     // sendfile
                          File myFile = new File (selectedImagePath); 
                          byte [] mybytearray  = new byte [(int)myFile.length()];
                          FileInputStream fis = new FileInputStream(myFile);
                          BufferedInputStream bis = new BufferedInputStream(fis);
                          bis.read(mybytearray,0,mybytearray.length);
                          OutputStream os = sock.getOutputStream();
                          System.out.println("Sending...");
                          os.write(mybytearray,0,mybytearray.length);
                          os.flush();

                        sock.close();

                        //Intent i  = new Intent(Sender.this,ImageReceive.class);
                        //startActivity(i);
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
4

2 に答える 2

0

私は問題を解決することができました..そもそもサーバーのIPアドレスがありませんでした..何らかの理由で、Android 4.2はソケットをデバイスのIPアドレスにバインドしていません...ジンジャーブレッドで同じコードを実行しようとすると、機能しました..私ジェリービーンはもうソケットプログラミングをサポートしていないという結論に達しました..おそらくGoogleは、サーバークライアントプログラミングではなく、アドホックネットワークを実現するためにwifidirect APIを使用することを強制しています..

于 2013-04-25T19:55:52.543 に答える
0

あなた自身のコメントは、コードがacceptの後の行に到達すると言っているので、あなたが何を求めているのかわかりません。ただし、コピー ループはすべて次のようになります。

while ((count = in.read(buffer)) >= 0)
{
  out.write(buffer, 0, count);
}

出力を書き込む前に、すべての入力を巨大なバッファーまたは ByteArrayInputStream に読み込むことによって、遅延を導入し、メモリを浪費する理由はありません。'buffer' は、2k などの適切なサイズにすることができます。

于 2013-04-21T23:13:49.330 に答える