Connection reset by peer: socket write error

qq29391091 2013-04-27 10:17:20
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";
command += "Priority: " + priority + "\r\n\r\n";

try {
os = socket.getOutputStream();
os.write(command.getBytes());
os.flush();
//os.close();
//os = null;
} catch (IOException e) {
// TODO Auto-generated catch block
Log.mylog("AmiAction set Redirect IOException", 2);
Log.mylog(e, 2);
connectTest();
}
}


public void setCallerid(String channel, String callerid)
{

setVar(channel, "CALLERID(num)", callerid);

}

public void setMoh(String channel, String moh)
{

setVar(channel, "CHANNEL(musicclass)", moh);
}

public void setGateway(String channel, String gateway)
{
setVar(channel, "gateway", gateway);
}
public void setIvrname(String channel, String ivrname)
{
setVar(channel, "ivrname", ivrname);
}

public void setHangup(String channel, String cause)
{
String command = "Action: Hangup\r\n";
command += "Channel: " + channel + "\r\n";
command += "cause: " + cause + "\r\n\r\n";

try {
os = socket.getOutputStream();
os.write(command.getBytes());
os.flush();
os = null;
} catch (IOException e) {
// TODO Auto-generated catch block
Log.mylog("AmiAction set Hangup IOException", 2);
Log.mylog(e, 2);
connectTest();
}
}
public void setMonitor(String channel, String uniqueid)
{
String command = "Action: Monitor\r\n";
command += "Channel: " + channel + "\r\n";
command += "File: " + uniqueid + "\r\n";
command += "Mix: True\r\n\r\n";
try {
os = socket.getOutputStream();
os.write(command.getBytes());
os.flush();
os = null;
} catch (IOException e) {
// TODO Auto-generated catch block
Log.mylog("AmiAction set monitor", 2);
Log.mylog(e, 2);
connectTest();
}
}

}
不停的调用setVar向socket服务端发送数据据,每发一段时间就出现这个异常,是代码哪里有问题?
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:124)
at com.ebt.ast.AmiAction.setVar(AmiAction.java:104)
at com.ebt.ast.AmiAction.setCallerid(AmiAction.java:152)
at com.ebt.ast.DialRoute.doRoute(DialRoute.java:65)
at com.ebt.ast.AmiEvent.Newexten(AmiEvent.java:374)
at com.ebt.ast.AMI.EventHandle(AMI.java:100)
at com.ebt.ast.AMI.Match(AMI.java:54)
at com.ebt.ast.AMI.run(AMI.java:11)
at java.lang.Thread.run(Thread.java:662)
...全文
164 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
lcf 2013-04-27
  • 打赏
  • 举报
回复
是服务器主动断开连接,应该是你把服务器搞挂了或者服务器觉得你太暴力了

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧