別の問題で再び戻ってきました。質問/回答を検索しようとしましたが、これまでのところどれもうまくいかなかったため、別の問題を投稿することにしました。Java/Tomcat で小さな REST JAXRS WebService を実行しており、JSP ページからコマンドを送信してソケット クライアントを開始および停止したいと考えています。これまでのところ、ソケットの開始を達成しましたが、停止できません。何が間違っていますか?
編集 1: 同じソケット関数 (同じクラスの PLC を使用) を JFrame インターフェイスでテストしました。1 つのボタンは REST 関数を表し、1 つの接続と他の切断は 2 つのボタンを備えています。
マイレスト機能
NewTen_PLC m_plc = new NewTen_PLC(0x01, "Central Park", "10.80.4.10", 5001);
Thread m_thread;
@PUT
@Path("/connect")
public void Connect(InputStream is)
{
System.out.println("CONNECT COMMAND RECEIVED");
try
{
System.out.println( "Starting Executor" );
//Using thread manager
//ExecutorService threadExecutor = Executors.newCachedThreadPool();
//Start Thread
//threadExecutor.execute(m_plc);
//Shutdown working threads when task is complete
//threadExecutor.shutdown();
//Using Thread
m_thread = new Thread(m_plc);
m_thread.start();
System.out.println( "Tasks started, main ends.\n" );
}
catch(Exception ex)
{
}
}
@PUT
@Path("/disconnect")
public void Disconnect(InputStream is)
{
System.out.println("DISCONNECT COMMAND RECEIVED");
try
{
//m_plc.closeConnection();
m_plc.m_bConnected = false;
}
catch(Exception ex)
{
System.out.println("Error while trying to close socket");
ex.printStackTrace();
}
}
これは、Socket 関数とスレッド Run メソッドを含む Class PLC です。
//--------------------------------------------------------------------------------------------------------
// Run (Thread)
//--------------------------------------------------------------------------------------------------------
@Override
public void run()
{
try
{
//DEBUG
System.out.printf("\n Thread Started and Running in %s \n", getPLCName());
//Connect to remote site
connectToRemoteServer();
//Set streams
getStreams();
//Process connection
processConnection();
}
catch(EOFException eofException)
{
System.out.println( "\nClient terminated connection" );
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
finally
{
closeConnection(); // close connection
}
}
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
// Connect to Server/Remote Site
//--------------------------------------------------------------------------------------------------------
private void connectToRemoteServer() throws IOException
{
//DEBUG
System.out.println("Attempting connection to site");
//Get IP Address
InetAddress ipAddress = InetAddress.getByName(this.getIPAddress());
//Create and Open Socket to server
m_clientSocket = new Socket(ipAddress, this.getPort(), null, this.getLocalPort());
//Set connected
if(m_clientSocket != null)
m_bConnected = true;
//Set read timeout to infinite
m_clientSocket.setSoTimeout(0);
//DEBUG
System.out.println("Connected to site");
}
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
// Get in and out Streams
//--------------------------------------------------------------------------------------------------------
private void getStreams() throws IOException
{
//DEBUG
System.out.println("Setting Streams");
//Input data stream
m_inStream = new DataInputStream(m_clientSocket.getInputStream());
//Output data stream
m_outStream = new DataOutputStream(m_clientSocket.getOutputStream());
}
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
// Process connection
//--------------------------------------------------------------------------------------------------------
private void processConnection() throws IOException
{
NewTen_Buffer inBuffer = new NewTen_Buffer(8192);
try // read message and display it
{
while(m_inStream.read() != -1 && m_bConnected == true)
{
//Read
inBuffer.m_iSize = m_inStream.read(inBuffer.m_bBuffer);
//DEBUG
System.out.printf("Data read has %d bytes \n", inBuffer.m_iSize);
//Decode
newData(inBuffer);
}
//DEBUG
System.out.println("OUT OF READING LOOP");
}
finally
{
}
}
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
// Close Connection
//--------------------------------------------------------------------------------------------------------
public void closeConnection()
{
System.out.println( "\nClosing connection" );
try
{
System.out.println( "\nIn Try closing" );
m_bConnected = false;
m_clientSocket.close(); // close socket
//System.out.println( "\nClosing Data input streams" );
//m_inStream.close(); // close output stream
//m_outStream.close(); // close input stream
//System.out.println( "\nShutdown ip op" );
//m_clientSocket.shutdownInput();
//m_clientSocket.shutdownOutput();
//System.out.println( "\nClosing socket" );
//m_clientSocket.close(); // close socket
System.out.println( "\nEnd of Try closing" );
}
catch ( IOException ioException )
{
ioException.printStackTrace();
} // end catch
}
//--------------------------------------------------------------------------------------------------------
それを止める方法はありますか?
乾杯、レオ