Hello, I’m working on application that uses a socket connection. I’ve implemented 2 different Threads for the read() and the write() methods so they are not being blocked by the read. Everything is working well on the WiFi connection but when I switch to GSM connection the connection becomes unstable without any exception being thrown. It means that there are freezes – the write and read seems to work but no data is actually being piped. Here is what I’m doing:
private Socket socketConn = null; private DataInputStream dis = null; private DataOutputStream dos = null; //Read buffer private byte[] buffer; ByteArrayOutputStream readBuffer; private String serverAddress; private int serverPort; public void openConnection(int timeout) throws Exception { if(socketConn != null){ return; } //if we need to resolve the host - if succeed we replace the default otherwise we use the default. //the check is done only once. if(checkHostResolved){ checkHostResolved = false; try{ InetAddress ia = InetAddress.getByName(serverHostAddress); serverAddress = ia.getHostAddress(); } catch(UnknownHostException e2){ MyLog.printException(this, e2, "run() InetAddress.getByName"); } } socketConn = new Socket(); InetSocketAddress isa = new InetSocketAddress(serverAddress, serverPort); socketConn.connect(isa, timeout); socketConn.setKeepAlive(true); socketConn.setTcpNoDelay(true); socketConn.setSoLinger(true, 1000); dis = new DataInputStream(socketConn.getInputStream()); dos = new DataOutputStream(socketConn.getOutputStream()); } public void closeConnection() { if(socketConn != null){ try{ if(dis != null){ socketConn.shutdownInput(); dis.close(); } if(dos != null){ socketConn.shutdownOutput(); dos.close(); } if(socketConn != null){ socketConn.close(); } if(readBuffer != null){ readBuffer.reset(); } } catch(IOException ioe){ MyLog.printException(this, ioe, "Problem Closing connection"); } finally{ dis = null; dos = null; socketConn = null; } } MyLog.printLog(this, "closeConnection end"); } private int trySend(String message) { try{ byte[] data = message.getBytes(); dos.write(data); dos.flush(); return 0; } catch(SocketException se){ MyLog.printException(this, se, "Problem with trySend"); } catch(Exception e){ MyLog.printException(this, e, "trySend()"); } MyLog.printLog(this, "trySend() Problem with trySend!!!!"); return ERROR_CODE_CONNECTION; } private boolean tryRead() { try{ int b = 0; while((b = dis.read(buffer)) > 0){ readBuffer.write(buffer, 0, b); //if the last read byte is '\0' then we have complete reading at least 1 packet of data if(buffer[b - 1] == '\0'){ byte[] data = readBuffer.toByteArray(); MyLog.printLog(this, "read data.length=" + data.length); readBuffer.reset(); String text = new String(data, "utf-8"); return true; } MyLog.printLog(this, "out while read b=" + b); //if the b<0 then EOF has reached if(b < 0){ throw new Exception("EOF has reached"); } } catch(Exception e){ } return false; } Does anyone encounter connection stability problems? Do I need to open the connection differently? How about using SocketChannel is it more reliable? Does Socket connection is a good approach when talking about mobile network connection? Thanks ayanir -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en