Hi everyone,

I am using SocketConnection to send bytes of data from one phone to another. The sending part is ok. The receiving end is not. I am using 1 thread to handle the sending and 1 thread to handle the receiving.

The main problem lies in that the InputStream object call to its read() method NEVER returns with a -1 when the end of stream has reached.

Has anyone come across this problem and managed to find a workaround?

The code for receiving bytes is as follows:

Code:
public void run() {
    	try {
            stringitem.setText("Waiting for connection");
            scn = (ServerSocketConnection) Connector.open("socket://:5000");
            
            // Wait for a connection.
            sc = (SocketConnection) scn.acceptAndOpen();
            stringitem.setText("Connection accepted");
            is = sc.openInputStream();
            os = sc.openOutputStream();
            
            int numofbytes = 0;
            while (true) {
                int c = 0;

                f.append("START!\n");
                while (c != -1) {
                	f.append("|A");
                	c = is.read();	// ############# Code hangs here after last byte was read ########
                	numofbytes++;
                	f.append("["+numofbytes+"]");
                }
                f.append("\nSTOP!\n");
                

                if (c == -1) {
                	f.append("@@@@@ c == -1");
                    break;
                }
                
                if (c == '\n') {
                	f.append("@@@@@ c == newline");
                    break;
                }
                
                f.append("Num of bytes received (I) - " + numofbytes+"\n");
            }
            f.append("Num of bytes received (II) - " + numofbytes+"\n");
            
            
            stop(); //Close all open streams
            stringitem.setText("Connection is closed");
        } catch (IOException ioe) {
            if (ioe.getMessage().equals("ServerSocket Open")) {
                Alert a = new Alert("", "Port 5000 is already taken.",
                        null, AlertType.ERROR);
                a.setTimeout(Alert.FOREVER);
                a.setCommandListener(this);
                display.setCurrent(a);
            } else {
            	f.append("IOException: "+ioe.toString()+"\n");
                if (!stop) {
                    ioe.printStackTrace();
                }
            }
        } catch (Exception e) {
        	f.append("Other Exception: "+e.toString());
        }
    }

Thanks in advance!