package com.fetion;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.Socket;
import java.net.UnknownHostException;
import com.fetion.Utils.FetionException;
import com.fetion.Utils.GenAuthResponse;
import com.fetion.Utils.GenResponse;
public class FetionSocketConnect {
private static String host = "221.130.45.208";
private static int port = 8080;
private static int counter=3;
private static int retryMaxTimes=3;
public static void main(String[] args) throws Exception {
FetionSocketConnect socket=new FetionSocketConnect();
String sid="",psw="";
sid="123453";
socket.login(sid,psw);
}
/**
* @param fetionNum
* @param psw
* @return 200 means ok
* @throws Exception
*/
public int login(String fetionNum,String psw) throws Exception {
int ret=0;
//
if(fetionNum==null || fetionNum.length()==0){
throw new FetionException(10005,"fetionNum is null!");
}
if(psw==null || psw.length()==0){
throw new FetionException(10004,"password is null!");
}
Socket server = new Socket(host, port);
System.out.println("connetioning:" + host + ":" + port);
String body="R fetion.com.cn SIP-C/2.0/r/nF: "+fetionNum+"/r/nI: 1/r/nQ: 1 R/r/nL: 253/r/n/r/n";
String xml="<args><device type=/"PC/" version=/"106/" client-version=/"2.3.0210/" /><caps value=/"simple-im;im-session;temp-group/" /><events value=/"contact;permission;system-message/" /><user-info attributes=/"all/" /><presence><basic value=/"400/" desc=/"/" /></presence></args>";
BufferedReader in = new BufferedReader(new InputStreamReader(server .getInputStream()));
OutputStream os=server.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
// PrintWriter out = new PrintWriter(os);
// System.out.println(xml.length());
System.out.println(body+xml);
dos.write((body+xml).getBytes());
dos.flush();
// ByteArrayOutputStream baOS=new ByteArrayOutputStream();
// StringBuffer sb=new StringBuffer();
String res="";
// String str="";
res=getSipcRes(in);
System.out.println("1:"+res);
ret=getSipcResCode(res);
if(ret!=401){
return -1;
}
String nonce="";
if(res.length()>0){
int pos=res.indexOf("nonce=");
nonce=res.substring(pos+7, pos+7+32); //32位的nonce
System.out.println(nonce);
}
GenAuthResponse genAuthResponse=new GenAuthResponse(fetionNum,psw,"fetion.com.cn", nonce);
String response=genAuthResponse.GenResponse();
String cnonce=genAuthResponse.getCnonce();
body="R fetion.com.cn SIP-C/2.0/r/nF: "+fetionNum+"/r/nI: 1/r/nQ: 2 R/r/nA: Digest response=/""+response+"/",cnonce=/""+cnonce+"/"/r/nL: 253/r/n/r/n";
xml="<args><device type=/"PC/" version=/"106/" client-version=/"2.3.0210/" /><caps value=/"simple-im;im-session;temp-group/" /><events value=/"contact;permission;system-message/" /><user-info attributes=/"all/" /><presence><basic value=/"400/" desc=/"/" /></presence></args>";
System.out.println(body+xml);
dos.write((body+xml).getBytes());
dos.flush();
res=getSipcRes(in);
System.out.println("2:"+res);
ret=getSipcResCode(res);
if(ret!=200){
return -1;
}
Thread.sleep(3000);
System.out.println("connetioning:" + host + ":" + port);
body="R fetion.com.cn SIP-C/2.0/r/nF: "+fetionNum+"/r/nI: 1/r/nQ: "+(++counter)+" R/r/n/r/n";
//
// BufferedReader in = new BufferedReader(new InputStreamReader(server .getInputStream()));
// OutputStream os=server.getOutputStream();
// DataOutputStream dos=new DataOutputStream(os);
// PrintWriter out = new PrintWriter(os);
// System.out.println(xml.length());
System.out.println(body);
dos.write((body).getBytes());
dos.flush();
res=getSipcRes(in);
System.out.println("3:"+res);
ret=getSipcResCode(res);
in.close();
os.close();
dos.close();
server.close();
return ret;
}
private int getSipcResCode(String arg0){
int ret=0;
if(arg0.length()>0){
int i=arg0.indexOf("/r/n");
String temp1=arg0.substring(0, i);
// System.out.println(temp1);
if(temp1.startsWith("")){
String[] sTemp=temp1.split(" ");
if(sTemp.length>=3){
// System.out.println(sTemp[1]);
ret=Integer.parseInt(sTemp[1]);
}
}
}
return ret;
}
private String getSipcRes(BufferedReader in) throws IOException{
String sRet="";
StringBuffer sb=null;
String str="";
ByteArrayOutputStream baOS=null;
if(null!=in){
sb=new StringBuffer();
baOS=new ByteArrayOutputStream();
// dos.flush();
// dos.write("SIPP".getBytes());
int len=0;
while (true) {
// temp = in.read();
str=in.readLine();
// System.out.println(str);
str=str+"/r/n";
// sb.append(str);
if(str.toUpperCase().startsWith("L")){
int i=str.indexOf(":");
if(i>0)
len=Integer.parseInt(str.substring(i+1).trim());
// System.out.println("len:"+len);
}
if(str.startsWith("/r/n")){
// str=in.readLine();
sb.append(str);
// System.out.println("rn"+str);
if(len>0){
for(int i=0;i<len;i++){
int temp;
temp=in.read();
baOS.write(temp);
// System.out.println(temp);
}
// System.out.println(baOS.toString());
sb.append(baOS.toString());
}
break;
}
sb.append(str);
str="";
// res=res+"/r/n"+str;
// if(temp==-1)
// break;
// baOS.write(temp);
}
// System.out.println(sb.toString());
sRet=sb.toString();
}
return sRet;
}
}