try {
ServerSocket serverSocket=null;
serverSocket=new ServerSocket(1989);
Socket socket=serverSocket.accept();
serverSocket.setSoTimeout(2000);
int temp=0;
String chan_name;
long chan_id;
int chan_lock;
int chan_skip;
long chan_group;
byte[] buf = new byte[1024*4];
while(true){
// socket.setSoTimeout(1000);
try {
InputStream ipInputStream=socket.getInputStream();
while((temp=ipInputStream.read(buf))!=-1){
try {
JSONObject prodate;
String recStr = new String(buf, 0,
temp);
prodate = new JSONObject(recStr);
chan_name = prodate.getString("mservice_name");
chan_id = prodate.getLong("prog_id");
chan_lock=prodate.getInt("mlock_flag");
chan_skip=prodate.getInt("mskip_flag");
chan_group=prodate.getLong("mfav_group");
if(chan_name.contains(".")){
StringBuffer tmp2 = new StringBuffer(chan_name);
Bundle bundle = new Bundle();
// bundle.putString("chan_index",
// tmp2.substring( tmp2.indexOf(".")-1, tmp2.indexOf(".")));
//Log.d(TAG, "chan_index: " + tmp2.substring(0, tmp2.indexOf(".")));
bundle.putString("chan_name",tmp2.substring(tmp2.indexOf(".")+1));
bundle.putLong("chan_id", chan_id);
bundle.putInt("chan_lock", chan_lock);
bundle.putInt("chan_skip", chan_skip);
bundle.putLong("chan_group", chan_group);
Log.d(TAG, "chan_name: " + tmp2.substring(tmp2.indexOf(".")+1));
msg = chanHandler.obtainMessage();
msg.setData(bundle);
msg.what = 1;
chanHandler.sendMessage(msg);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SocketTimeoutException e) {
// TODO: handle exception
Log.d(TAG, "scan server timeout... ");
msg = chanHandler.obtainMessage();
msg.what = 0;
chanHandler.sendMessage(msg);
}
catch(SocketException e){
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage());
}