package com.safemobile.lib.sound; import com.safemobile.lib.SM; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class TCPaudioClient implements Runnable{ private boolean alive = true; public String serverHostname = new String ("10.120.1.114");// private int port = 50001; private Thread listenThread; private Socket soc =null; private InputStream recv; private OutputStream writer; private volatile int n=0; public Boolean connOK=false; byte[] buffer = new byte[16384]; private List _listeners = new ArrayList(); public TCPaudioClient(String hostName, int _port) { serverHostname=hostName; this.port = _port; SM.Debug("---TCPhandler construcort--- port:"+_port); listenThread = new Thread(this, "TCPlisten"); listenThread.start(); // (2) Start the thread. } @Override public void run() { try { soc = new Socket(serverHostname, port); SM.Debug("Socket timeout:" + soc.getSoTimeout() ); //soc.setSoTimeout(5000); recv= soc.getInputStream(); writer =soc.getOutputStream() ; if(soc !=null) connOK = true; } catch (UnknownHostException e) { SM.Debug("UnknownHostException", "break:"+e.toString()); } catch (IOException e) { SM.Debug("IOException", "break:"+e.toString()); } while(alive) { try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block SM.Debug(e.toString()); } while(connOK) { try { buffer = new byte[16384]; n = recv.read(buffer); if(n==-1) break; _fireDataArrived(buffer,n); } catch(Exception ex) { SM.Debug("break:"+ex.toString()); connOK = false; } }//while(connOK) try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block SM.Debug(e.toString()); } if(alive)RestartTCP(); }//while(alive) SM.Debug("=================================="); SM.Debug("TCP listenThread stoped!! alive = false"); SM.Debug("=================================="); } public boolean Send(byte[] data,int len) { try { if(writer != null) { writer.write(data,0,len); return true; } else { return false; } } catch (Exception e) { // TODO Auto-generated catch block SM.Debug(e.toString()); } return false; } private void RestartTCP() { try { SM.Debug("Restarting TCP...ip:"+serverHostname); soc = new Socket(serverHostname, port); recv= soc.getInputStream(); writer =soc.getOutputStream(); if(soc !=null) connOK = true; } catch (UnknownHostException e) { SM.Debug("break:"+e.toString()); } catch (IOException e) { SM.Debug("break:"+e.toString()); } } public boolean isAlive() { return alive; } public void setAlive(boolean alive) { this.alive = alive; } public void Stop() { this.alive = false; if(soc !=null) { try { soc.close(); soc = null; } catch (IOException e) { // TODO Auto-generated catch block SM.Debug("break:"+e.toString()); } connOK = false; } // stop thread if(listenThread != null) { Thread moribund = listenThread; listenThread = null; moribund.interrupt(); } } public synchronized void addTCPListener( ITCPaudioLis l ) { _listeners.add( (ITCPaudioLis) l ); } public synchronized void removeTCPListener( ITCPaudioLis l ) { _listeners.remove( l ); } private synchronized void _fireDataArrived(byte[] data, int len) { TCPaudioEvent event = new TCPaudioEvent( this, data, len ); Iterator listeners = _listeners.iterator(); while( listeners.hasNext() ) { ( (ITCPaudioLis) listeners.next() ).dataRecv(event); } } }