2013-04-21 99 views
1

我想通过wifi连接两个android设备并发送一些数据。在客户端,套接字显示已连接,但服务器未超出接受范围。我甚至不确定这是可能的。以下是我的服务器和客户端代码。我已连接使用设备上运行的WiFi热点的两个设备。Android客户端套接字正在连接,但服务器套接字不接受?

服务器端:

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

回答

0

你自己的评论说,代码到达后接受行,所以我不知道知道你在问什么。但是您的副本循环都应该是这样的:

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

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

+0

嗨,谢谢你的回复。 .that是一个打字错误..服务器不接受连接出于某种原因,我无法弄清楚为什么。我还想补充说,当相同的代码作为java代码运行在pc上时,它会工作..在android中,我添加了所有权限,并且代码在一个活动中运行..没有任何线程。 – nitinsh99 2013-04-22 00:49:18

+0

@ nitinsh99如果它'不接受连接',它在accept()调用后如何到达该行? – EJP 2013-04-24 10:36:23

0

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

相关问题