日期:2014-05-20  浏览次数:20803 次

Connection reset by peer: socket write error
public class AmiAction {
AmiSocket amiSocket = new AmiSocket();
//OutputStream os = null;
Socket socket = null;
OutputStream os = null;
BufferedReader in = null;

public AmiAction(){
connect();
}
int i = 0;
public void connect(){
socket = null;
try {
InetAddress addr = InetAddress.getByName(Config.strAmiServer);
socket = new Socket(addr, Config.intAmiPort);
//socket.setSendBufferSize(2000);
System.out.println(socket.getSendBufferSize());
os = socket.getOutputStream();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
/**
 * 
Action: login
Username: amiuser
Secret: admin@xxmobile
*/
String strLogin = "Action: login\r\n";
strLogin += "Username: " + Config.strAmiUser + "\r\n";
strLogin += "Secret: " + Config.strAmiSecret + "\r\n\r\n";

String strResponse = in.readLine(); //Asterisk Call Manager/1.1
os.write(strLogin.getBytes());
os.flush();

strResponse = in.readLine();
if (strResponse.equals("Response: Success")) {

Log.mylog("AMI login success", 0);
} else {
Log.mylog("AMI login failure", 0);
}
//os.close();
//System.out.println(socket.toString());
} catch (Exception e) {
Log.mylog(e.getMessage(), 2);
Log.mylog(e, 2);
}

}

public boolean connectTest(){
String strLogin = "Action: CoreStatus\r\n\r\n";
boolean flag = true;
try {
os = socket.getOutputStream();
os.write(strLogin.getBytes());
os.flush();
} catch (Exception e) {
Log.mylog("AmiSocket connectTest", 2);
Log.mylog(e, 2);
Log.mylog("Socket connectTest exception.Connect again", 2);
connect();
flag = false;


return flag;

}

public void setVar(String channel, String varname, String varvalue)
{
String command = "Action: Setvar\r\n";
command += "Channel: " + channel + "\r\n";
command += "Variable: " + varname + "\r\n";
command += "Value: " + varvalue + "\r\n\r\n";
System.out.println(i++);
try {
os = socket.getOutputStream();
os.write(command.getBytes());
os.flush();
os = null;
} catch (IOException e) {
// TODO Auto-generated catch block
Log.mylog("AmiAction set var IOException", 2);
Log.mylog(e, 2);
connectTest();
}

}

public void setRedirect(String channel, String exten, String context, int priority)
{
String command = "Action: Redirect\r\n";
command += "Channel: " + channel + "\r\n";
command += "Exten: " + exten + "\r\n";
command += "Context: " + context +"\r\n";