Connessione tramite socket

Listato 20. Connessione tramite socket

public void run()
{
    DataInputStream is = null;
    DataOutputStream os = null;
    StreamConnection sc = null;
    Response response;
    try
    {
        sc = (StreamConnection)Connector.open("socket://80.20.12.112:9012", Connector.READ_WRITE, true);
        is = sc.openDataInputStream();
        os = sc.openDataOutputStream();
        
        os.write("OK\r\n".getBytes());
        os.flush();

        // Analizzare risposta con InputStream is

        ......
    }
    catch (Exception e1)
    {
        e1.printStackTrace();
    }
    try
    {
        if (is!=null)
         is.close();
        if (os!=null)
         os.close();
        if (sc!=null)
         sc.close();
        is = null;
        os = null;
        sc = null;
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}