[英]how i can send a receive a data from second time using the same open socket in mule java?
在这个论坛上,大家好,在这种情况下,我需要社区的帮助,因为我不知道如何保持打开套接字来从设备发送和接收数据。 例如,当我发送第一个命令以接收答案时,我成功地收到了答案,但是在尝试发送回第二个命令时,在第一次发送/接收之后,我没有响应,有时会抛出错误。
这是我在Mule 3.5 CE Java 1.7中使用的类
public class TCPMuleOutBasic extends TcpMessageReceiver {
public TCPMuleOutBasic(Connector connector, FlowConstruct flowConstruct,
InboundEndpoint endpoint) throws CreateException {
super(connector, flowConstruct, endpoint);
}
protected Work createWork(Socket socket) throws IOException {
return new MyTcpWorker(socket, this);
}
protected class MyTcpWorker extends TcpMessageReceiver.TcpWorker {
public MyTcpWorker(Socket socket, AbstractMessageReceiver receiver)
throws IOException {
super(socket, receiver);
// TODO Auto-generated constructor stub
}
@Override
protected Object getNextMessage(Object resource) throws Exception {
ArrayList<String> testarr2 = new ArrayList<>();
BufferedReader entrada;
//Socket clientSocket = null;
try{
ArrayList<String> testarr = new ArrayList<>();
//this.socket.
//clientSocket = this.socket;
//OPEN IN BUFFER, DE ENTRADA
entrada = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
//OPEN OUT BUFFER DE SALIDA ENVIA COMANDO
PrintStream pingStream = new PrintStream(this.socket.getOutputStream());
//PrintStream pingStream = new DataOutputStream(clientSocket.getOutputStream());
System.out.println("STATUS SOCKET VAL 1.2: "+this.socket.isClosed()+" CON "+isConnected());
testarr = this.enviarComando("#SER",1,this.socket,entrada,pingStream);
System.out.println("ARRAY VALOR 1: " + testarr.get(0));
//OPEN A SECOND TIME, THIS IS MY PROBLEM DONT WORK
//ABRO BUFFER DE ENTRADA
entrada = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
//ABRO BUFFER DE SALIDA ENVIA COMANDO
pingStream = new PrintStream(this.socket.getOutputStream());
System.out.println("STATUS SOCKET VAL 2: "+this.socket.isClosed()+" CON "+isConnected());
testarr2 = this.enviarComando("#NLO",2,this.socket,entrada,pingStream);
/*System.out.println("ARREGLO VALOR 2: " + testarr2.get(0));*/
dataIn.close();
return testarr;
}catch(IOException e)
{
System.out.println("ERROR ARREGLO:"+e.getMessage());
return null;
}
}//FIN NEXTMESSANGE
//METODO PARA HACER PETICION
//FUNCTION TO SEND A RECIVE DATA
protected ArrayList<String> enviarComando (String comando,int req,Socket openSocket,BufferedReader entrada,PrintStream pingStream) throws Exception {
boolean InitConnection = false;
String lineaComando;
String mensajeServidor;
Socket clientSocket = null;
ArrayList<String> retorno = new ArrayList<String>();
try{
if (InitConnection == false) {
lineaComando = comando;
System.out.println("Mandando Comandos:"+lineaComando);
clientSocket = openSocket;
String direccion = clientSocket.getInetAddress().getHostAddress();
System.out.println("Direccion:"+direccion);
System.out.println("Cliente en línea ="+req);
System.out.println("Comando enviado:" + lineaComando);
lineaComando = lineaComando + CRC.getCrc16Ccitt(lineaComando) + "\r";
pingStream.print(lineaComando);
//entrada = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//entrada.ready();
System.out.println("STATUS FLUJO DE ENTRADA ="+req+entrada.ready());
int contador = 0;
System.out.println("ENTRADA: "+entrada.toString());
while((mensajeServidor = entrada.readLine()) != null) //Mientras haya mensajes desde el cliente
{
contador++;
//Se muestra por pantalla el mensaje recibido
retorno.add(mensajeServidor);
System.out.println("mensaje dispositivo "+contador+ ": "+ mensajeServidor);
}
/*
lineaComando = "#NLO";
System.out.println("Mandando 2do Comandos:"+lineaComando);
lineaComando = lineaComando + CRC.getCrc16Ccitt(lineaComando) + "\r";
pingStream.print(lineaComando);
while((mensajeServidor = entrada.readLine()) != null) //Mientras haya mensajes desde el cliente
{
contador++;
//Se muestra por pantalla el mensaje recibido
retorno.add(mensajeServidor);
System.out.println("mensaje dispositivo Segundo"+contador+ ": "+ mensajeServidor);
}*/
InitConnection = true;
}
//dataIn.close();
InitConnection = false;
logger.debug("Socket timeout");
System.out.println("CIERRO SOCKET");
return retorno;
}catch (SocketException e )
{
System.out.println("ERROR F:"+e.getMessage()+" "+comando+" Request: "+req);
return null;
}
}
}
}
连接器和流程。
<tcp:connector name="TCP" doc:name="TCP connector"
clientSoTimeout="100000" receiveBacklog="0" receiveBufferSize="0"
sendBufferSize="0" serverSoTimeout="100000" socketSoLinger="0"
validateConnections="true" keepSendSocketOpen="true" keepAlive="true">
<receiver-threading-profile
maxThreadsActive="5" maxThreadsIdle="5" />
<reconnect-forever />
<!-- <service-overrides messageReceiver="TCPMuleOut" />-->
<service-overrides messageReceiver="ve.com.comware.socket.TCPMuleOutBasic" />
<tcp:direct-protocol payloadOnly="true" />
</tcp:connector>
<flow name="socketcomware0.1Flow1" doc:name="socketcomware0.1Flow1">
<tcp:inbound-endpoint host="107.170.3.231" port="2020" doc:name="TCP" responseTimeout="100000" exchange-pattern="request-response"/>
<logger message="HOLA1" level="INFO" doc:name="Logger"/>
<logger message="test ----------------- #[message.payload[0]] -------------------#[message.payload[1]] " level="INFO" doc:name="Logger"/>
</flow>
错误:
org.mule.exception.DefaultSystemExceptionStrategy: Caught exception in Exception Strategy: Index: 0, Size: 0
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at ve.com.comware.socket.TCPMuleOutBasic$MyTcpWorker.getNextMessage(TCPMuleOutBasic.java:65)
请尝试此操作,并让我知道,以便我了解您的用例-
<tcp:connector name="TCP1" validateConnections="true" sendBufferSize="0" receiveBufferSize="0" receiveBacklog="0" clientSoTimeout="10000" serverSoTimeout="10000" socketSoLinger="0" doc:name="TCP">
<tcp:direct-protocol payloadOnly="false"/>
</tcp:connector>
<flow name="tcp-testFlow">
<tcp:inbound-endpoint exchange-pattern="request-response" host="${host}" port="${port}" responseTimeout="10000" connector-ref="TCP1" doc:name="TCP"/>
<logger message="This is what your client is sending you - #[payload]" level="INFO" doc:name="Logger"/>
<set-payload value="Hello from server - #[payload]" doc:name="Set Payload"/>
</flow>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.