1. satyarajasekhar's Avatar
    Hi,
    I am trying to write a simple socket program in which the server socket as well as client socket also remain on device. I am starting the server socket and then starting the client socket which writes the image to the outputstream and the listening server socket will fetch the byte data from the inputstream and display it.
    I am in confusion whether to use outputstream or dataoutputstream to write the data. Somebody in forums told that outputstream is not good for writing the image data.

    Please provide me the sample code in which i had to write and read the image data from the blackberry device.
    Client Side Code
    SocketConnection sock = (SocketConnection)Connector.open("socket://sample-PC:5680;deviceSide=false");

    InputStream is = sock.openInputStream();
    OutputStream os =sock.openOutputStream();
    Bitmap bmp = Bitmap.getBitmapResource("raj-logo-hai.png");
    int height=bmp.getHeight();
    int width=bmp.getWidth();
    int[] rgbdata = new int[width*height];
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);

    Graphics g = new Graphics(bmp);
    bmp.getARGB(rgbdata,0,width,0,0,width,height);
    for (int i = 0; i < rgbdata.length ; i++) {
    if (rgbdata[i] != -1) {
    dos.writeInt(i);
    dos.flush();
    }
    }
    bos.flush();
    bos.close();
    sock.close();

    server side code
    ServerSocketConnection ss =(ServerSocketConnection)Connector.open("socket://:"+5680+";deviceSide=true");

    str=new StringBuffer();
    while(true){
    SocketConnection incoming = (SocketConnection)ss.acceptAndOpen();

    InputStream is =incoming.openDataInputStream();
    OutputStream os = incoming.openOutputStream();
    byte[] responseData = new byte[82000];
    int length1 = 0;
    StringBuffer rawResponse = new StringBuffer();

    while (-1 != (length1 = is.read(responseData)))
    {
    rawResponse.append(new String(responseData, 0, length1));
    }
    final String result = rawResponse.toString();
    byte[] dataArray = result.getBytes();
    bitmap = EncodedImage.createEncodedImage(dataArray, 0,
    dataArray.length);
    BitmapField picture = new BitmapField(bitmap.getBitmap());

    add(picture);
    updateDisplay();
    08-10-09 10:58 PM
  2. ydaraishy's Avatar
    At a glance, I'm not sure you're using the deviceSide option correctly.
    08-11-09 06:24 PM
LINK TO POST COPIED TO CLIPBOARD