Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Socket programming : peroblem while receiving response from device (http://www.go4expert.com/forums/socket-programming-peroblem-receiving-t26557/)

ajkush 23Aug2011 15:55

Socket programming : peroblem while receiving response from device
 
i am sending command to the socket to get status from the device the command is "bc "

my program is:

Code:

public final class Connection {

    private static  StringBuilder response = new StringBuilder();
    final static String charset = "ISO-8859-1";
    /**
    *
    * @description : Constructor
    */
    private Connection(){

    }

    /**
    *
    * @method sendRequest
    * @description  : Method to create connection with socket using IP address and port No.
    * @param : host
    * @param : port
    * @param : command
    * @param : timeoutInMillis
    * @return void
    * @throws NetworkErrorException
    */
    public static String sendRequest(final String host,final int  port,
            final String command,
            final int timeoutInMillis) throws UnknownHostException,NetworkSettingException

            {
        if (host == null)
        {
            throw new NullPointerException("host is null");  //NOPMD
        }
        Socket clientSocket=null;
        try {

            /**
            * Creating socket connection with IP address and port number to send Command
            */
            try{
                clientSocket = new Socket(host, port);

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

                throw new NetworkSettingException(e.getMessage());
            }

            clientSocket.setSoTimeout(timeoutInMillis);

            /**
            * Print formatted representations of objects to a text-output stream.
            */

            final PrintWriter outPutStream = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream(), charset));
            try
            {
                outPutStream.print(command);
                Log.d("Outgoing data : ", command);
                outPutStream.flush();
                /**
                *  responseString object contain the response after executing the command on socket
                */
                BufferedReader responseString = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), charset));

                try
                {

                    //response.append("s12s22s32s42");
                    try{
                        Log.d("Shivam","Come in while");
                        int c = 0;
                        while (((c = responseString.read()) != '\n') && (c != -1)) {
                           
                            response.append((char)c);
                            System.out.println(response.toString());
                        }
                    }catch (Exception e) {
                        System.out.println(e);
                    e.printStackTrace();
                    }


                }

                finally
                {
                    responseString.close();
                }


            }
            finally
            {
                outPutStream.close();
            }

        }

        catch(IOException ex){
            Log.d("ConnectionRefused IOException: ",ex.getMessage());
        }
        catch(NullPointerException ex){  //NOPMD
            Log.d("ConnectionRefused NullPointerException : ",ex.getMessage());
        }

        finally
        {
            try {
                clientSocket.close();
            } catch (NullPointerException ex) { //NOPMD
                Log.d("NullPointerException", "No Socket to close - " +ex.toString());
            } catch (IOException ex) {
                Log.d("IOException", "Unable to close Socket - " +ex.toString());
            }
        }
        Log.d("FRinal REsponse",response.toString());
        return response.toString();

            }
}

Code:

08-24 00:34:30.728: DEBUG/Parameter sent to Socket :(1257): ip_address :192.168.1.106 port :  8002 response_time : 1000 command : bc
08-24 00:34:30.870: DEBUG/Outgoing data :(1257): bc
08-24 00:34:30.883: DEBUG/Shivam(1257): Come in while
08-24 00:34:31.598: DEBUG/dalvikvm(722): GC_EXPLICIT freed <1K, 18% free 12923K/15751K, paused 8ms+14ms
08-24 00:34:31.887: INFO/System.out(1257): java.net.SocketTimeoutException
08-24 00:34:31.887: WARN/System.err(1257): java.net.SocketTimeoutException
08-24 00:34:32.108: WARN/System.err(1257):    at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:451)
08-24 00:34:32.108: WARN/System.err(1257):    at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:75)
08-24 00:34:32.108: WARN/System.err(1257):    at java.io.InputStreamReader.read(InputStreamReader.java:248)
08-24 00:34:32.108: WARN/System.err(1257):    at java.io.BufferedReader.fillBuf(BufferedReader.java:130)
08-24 00:34:32.118: WARN/System.err(1257):    at java.io.BufferedReader.readLine(BufferedReader.java:357)
08-24 00:34:32.130: WARN/System.err(1257):    at com.shivam.connection.Connection.sendRequest(Connection.java:113)
08-24 00:34:32.130: WARN/System.err(1257):    at com.shivam.device.AbstractDevice.sendCommandToSocket(AbstractDevice.java:90)
08-24 00:34:32.200: WARN/System.err(1257):    at com.shivam.device.Hdmi.sendDataToSocket(Hdmi.java:91)
08-24 00:34:32.200: WARN/System.err(1257):    at com.shivam.device.Hdmi.selectedCombinations(Hdmi.java:75)
08-24 00:34:32.200: WARN/System.err(1257):    at com.shivam.serviceImpl.DeviceServiceImpl.selectedCombinations(DeviceServiceImpl.java:76)
08-24 00:34:32.200: WARN/System.err(1257):    at com.shivam.user.activity.CommandActivity.deviceCurrentStatus(CommandActivity.java:100)
08-24 00:34:32.200: WARN/System.err(1257):    at com.shivam.user.activity.CommandActivity.onCreate(CommandActivity.java:78)
08-24 00:34:32.200: WARN/System.err(1257):    at android.app.Activity.performCreate(Activity.java:4397)
08-24 00:34:32.200: WARN/System.err(1257):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-24 00:34:32.334: WARN/System.err(1257):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
08-24 00:34:32.334: WARN/System.err(1257):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1656)
08-24 00:34:32.334: WARN/System.err(1257):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
08-24 00:34:32.334: WARN/System.err(1257):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
08-24 00:34:32.334: WARN/System.err(1257):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:676)
08-24 00:34:32.334: WARN/System.err(1257):    at android.widget.TabHost.setCurrentTab(TabHost.java:345)
08-24 00:34:32.334: WARN/System.err(1257):    at android.widget.TabHost.addTab(TabHost.java:235)
08-24 00:34:32.334: WARN/System.err(1257):    at com.shivam.user.activity.TabScreen.onCreate(TabScreen.java:81)
08-24 00:34:32.382: WARN/System.err(1257):    at android.app.Activity.performCreate(Activity.java:4397)
08-24 00:34:32.382: WARN/System.err(1257):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-24 00:34:32.382: WARN/System.err(1257):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
08-24 00:34:32.699: WARN/System.err(1257):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
08-24 00:34:32.754: WARN/System.err(1257):    at android.app.ActivityThread.access$500(ActivityThread.java:122)
08-24 00:34:32.754: WARN/System.err(1257):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
08-24 00:34:32.754: WARN/System.err(1257):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 00:34:32.754: WARN/System.err(1257):    at android.os.Looper.loop(Looper.java:132)
08-24 00:34:32.754: WARN/System.err(1257):    at android.app.ActivityThread.main(ActivityThread.java:4123)
08-24 00:34:32.944: WARN/System.err(1257):    at java.lang.reflect.Method.invokeNative(Native Method)
08-24 00:34:32.944: WARN/System.err(1257):    at java.lang.reflect.Method.invoke(Method.java:491)
08-24 00:34:32.944: WARN/System.err(1257):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-24 00:34:32.974: WARN/System.err(1257):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-24 00:34:32.974: WARN/System.err(1257):    at dalvik.system.NativeStart.main(Native Method)
08-24 00:34:33.054: DEBUG/Length(1257): 1
08-24 00:34:33.194: DEBUG/array value(1257):  c
08-24 00:34:37.171: INFO/ActivityManager(672): Displayed com.shivam.user.activity/.TabScreen: +21s564ms
08-24 00:34:37.428: DEBUG/TabletStatusBar(848): lights on
08-24 00:34:39.878: DEBUG/dalvikvm(914): GC_EXPLICIT freed 18K, 5% free 6378K/6663K, paused 105ms+19ms
08-24 00:34:41.703: DEBUG/dalvikvm(722): GC_CONCURRENT freed 1365K, 15% free 13522K/15751K, paused 9ms+9ms
08-24 00:34:45.088: DEBUG/dalvikvm(792): GC_EXPLICIT freed 154K, 4% free 6833K/7111K, paused 8ms+9ms
08-24 00:34:50.068: DEBUG/dalvikvm(722): GC_EXPLICIT freed 433K, 17% free 13102K/15751K, paused 19ms+13ms
08-24 00:37:59.651: DEBUG/SntpClient(672): request time failed: java.net.SocketException: Address family not supported by protocol
08-24 00:42:59.959: DEBUG/SntpClient(672): request time failed: java.net.SocketException: Address family not supported by protocol
08-24 00:45:00.978: DEBUG/dalvikvm(848): GC_CONCURRENT freed 1240K, 12% free 10295K/11655K, paused 382ms+135ms
08-24 00:47:59.974: DEBUG/SntpClient(672): request time failed: java.net.SocketException: Address family not supported by protocol
08-24 00:48:10.288: DEBUG/dalvikvm(742): GC_CONCURRENT freed 404K, 8% free 6715K/7239K, paused 472ms+27ms
08-24 00:53:00.037: DEBUG/SntpClient(672): request time failed: java.net.SocketException: Address family not supported by protocol
08-24 00:58:00.041: DEBUG/SntpClient(672): request time failed: java.net.SocketException: Address family not supported by protocol
08-24 01:03:00.047: DEBUG/SntpClient(672): request time failed: java.net.SocketException: Address family not supported by protocol
08-24 01:04:57.088: DEBUG/TabletStatusBar(848): lights on
08-24 01:04:59.457: DEBUG/AndroidRuntime(1268): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
08-24 01:04:59.457: DEBUG/AndroidRuntime(1268): CheckJNI is ON
08-24 01:05:00.612: WARN/WindowManager(672): Failure taking screenshot for (230x135) to layer 21010
08-24 01:05:01.772: DEBUG/dalvikvm(1257): GC_CONCURRENT freed 906K, 38% free 9894K/15751K, paused 26ms+427ms
08-24 01:05:02.098: DEBUG/dalvikvm(722): GC_EXPLICIT freed <1K, 17% free 13101K/15751K, paused 1700ms+35ms
08-24 01:05:02.698: WARN/InputManagerService(672): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@407e4c70 (uid=10036 pid=1257)
08-24 01:05:06.178: DEBUG/AndroidRuntime(1268): Calling main entry com.android.commands.pm.Pm
08-24 01:05:06.748: DEBUG/dalvikvm(722): GC_CONCURRENT freed 2048K, 18% free 13067K/15751K, paused 9ms+8ms
08-24 01:05:07.888: DEBUG/dalvikvm(722): GC_CONCURRENT freed 1070K, 12% free 13975K/15751K, paused 9ms+7ms
08-24 01:05:08.237: DEBUG/dalvikvm(1068): GC_EXPLICIT freed 12K, 5% free 6302K/6595K, paused 567ms+13ms


i want to know why this time out exception and how can i handle the response ... commands are executing well.


All times are GMT +5.5. The time now is 07:50.