客户端服务器套接字连接无法正常工作

时间:2014-11-14 16:03:00

标签: android sockets socket.io client-server wifip2p

我已经制作了一个用于通过wifip2p连接两个设备的android应用程序,现在通过wifip2p连接设备工作正常但是在创建套接字连接时客户端没有得到另一个设备中运行的服务器。

客户代码

if(resultCode == RESULT_OK)
    {
        path = data.getStringExtra("file");
        serverStatus.setText(path);

        new Thread()
        {
            public void run()
            {
                Socket socket = new Socket();
                DataInputStream dis = null;
                FileInputStream fis = null;
                InputStream is = null;
                OutputStream stream = null;
                int port = 8000;

                try 
                {
                    //Log.d(WiFiDirectActivity.TAG, "Opening client socket - ");
                    socket.bind(null);
                    socket.connect((new InetSocketAddress(groupOwnerAddress, port)),SOCKET_TIMEOUT);


                    //Log.d(WiFiDirectActivity.TAG, "Client socket - " + socket.isConnected());
                    stream = socket.getOutputStream();
                    ContentResolver cr = getApplicationContext().getContentResolver();

                    try

                    {
                        fis = new FileInputStream(path);
                        //is = cr.openInputStream(Uri.parse(path));
                        byte[] buffer = new byte[4096];
                        int bytesRead;
                        //FileInputStream fis = new FileInputStream(path);
                        //InputStream isf = new FileInputStream(path);
                        //dis = new DataInputStream(fis);

                        while((bytesRead = fis.read(buffer)) != 1)
                        {
                            stream.write(buffer, 0, bytesRead);
                            stream.flush();
                        }


                    } 
                    catch (FileNotFoundException e) 
                    {


                    }
                    //DeviceDetailFragment.copyFile(is, stream);
                    //Log.d(WiFiDirectActivity.TAG, "Client: Data written");
                } 
                catch (IOException e) 
                {
                    //Log.e(WiFiDirectActivity.TAG, e.getMessage());

                } 
                finally 
                {
                    try
                    {
                        if(fis != null)
                        {
                            fis.close();
                        }

                        if(stream != null)
                        {
                            stream.close();
                        }

                        if(socket != null)
                            socket.close();


                    }
                    catch(Exception e)
                    {

                    }

                   /* if(dis != null)
                    {
                        dis = null;
                    }*/




                }
            }
        }.start();

        Toast.makeText(this, "FileSend Succesfuly",Toast.LENGTH_LONG).show();
    }

服务器代码

protected void onHandleIntent(Intent intent)
{



//  port = ((Integer) intent.getExtras().get("port")).intValue();
    port = 8200;
    //saveLocation = (File) intent.getExtras().get("saveLocation");
    serverResult = (ResultReceiver) intent.getExtras().get("serverResult"); 


    //signalActivity("Starting to download");


    String fileName = "";
    FileOutputStream fos = null ;
    InputStream is = null;
    ServerSocket welcomeSocket = null;
    Socket socket = null;

    try {



            welcomeSocket = new ServerSocket(port);

            while(true && serviceEnabled)
            {

            //Listen for incoming connections on specified port
            //Block thread until someone connects 
            signalActivity("Waiting For CLient");
            socket = welcomeSocket.accept();



            final File f = new File(Environment.getExternalStorageDirectory() + "/"
                    + getApplicationContext().getPackageName() + "/wifip2pshared-" + System.currentTimeMillis()
                    + ".mp3");

            File dirs = new File(f.getParent());
            if (!dirs.exists())
                dirs.mkdirs();
            //File f = new File("/wifip2p.jpg");
            f.createNewFile();



            is = socket.getInputStream();

            byte[] buffer = new byte[4096];
            int bytesRead;

            fos = new FileOutputStream(f);

            while((bytesRead = is.read(buffer)) != -1)
            {
                fos.write(buffer, 0, bytesRead);
                signalActivity("File Transfer is Going on");
            }



        }


    } catch (IOException e) {
        signalActivity(e.getMessage());



    }
    catch(Exception e)
    {
        signalActivity(e.getMessage());

    }
    finally
    {

        try
        {
            //closing socket
            if(socket != null)
            {
                socket.close();
            }

            //closing ServerSocket
            if(welcomeSocket != null)
            {
                welcomeSocket.close();
            }

            if(is != null)
                is.close();

            if(fos != null)
                fos.close();

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    //Signal that operation is complete
    serverResult.send(port, null);





}

0 个答案:

没有答案