1

我正在尝试通过 wifi 连接两个 android 设备并发送一些数据。在客户端,套接字显示已连接,但服务器并未提前接受。我什至不确定这怎么可能。下面是我的服务器和客户端代码。我已经使用在其中一台设备上运行的 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

您自己的评论说代码在接受后到达行,所以我不知道您在问什么。但是,您的复制循环应如下所示:

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

在写入任何输出之前,没有理由通过将所有输入读入一个巨大的缓冲区或 ByteArrayInputStream 来引入延迟和浪费内存。“缓冲区”可以是任何合理的大小,例如 2k。

于 2013-04-21T23:13:49.330 回答
0

我设法解决了这个问题..没有服务器IP地址开始..由于某种原因android 4.2没有将套接字绑定到设备IP地址...当我尝试在姜饼上运行相同的代码时它工作了..我得出的结论是果冻豆不再支持套接字编程..可能是谷歌强迫我们使用 wifidirect api 来实现临时网络而不是服务器客户端编程..

于 2013-04-25T19:55:52.543 回答