From 40f432e6cdb222a055240dbabb971dcdc8abf68f Mon Sep 17 00:00:00 2001 From: Nikos Siatras Date: Sun, 24 Jul 2022 13:40:16 +0300 Subject: [PATCH] TCP Server and Client example refreshed --- .../src/Extasys/Examples/TCPClient/TCPClient.java | 4 ++-- .../src/Extasys/Examples/TCPClient/frmMain.java | 15 ++++++++++++--- .../src/Extasys/Examples/TCPServer/TCPServer.java | 2 +- .../Extasys/Examples/TCPServer/frmTCPServer.java | 4 ++-- Extasys for Java/nbproject/project.properties | 2 +- .../src/Extasys/ByteArrayBuilder.java | 3 +-- .../src/Extasys/ExtasysThreadPool.java | 2 +- .../Packets/IncomingTCPClientPacket.java | 8 ++++---- .../Packets/MessageCollectorTCPClientPacket.java | 2 +- .../Packets/OutgoingTCPClientPacket.java | 2 +- .../Tools/TCPClientMessageCollector.java | 8 ++++---- ...MessageCollectorTCPClientConnectionPacket.java | 2 +- .../TCP/Server/Listener/TCPClientConnection.java | 9 +++++---- .../TCPClientConnectionMessageCollector.java | 8 ++++---- 14 files changed, 40 insertions(+), 31 deletions(-) diff --git a/Extasys for Java Examples/Extasys.Examples.TCPClient/src/Extasys/Examples/TCPClient/TCPClient.java b/Extasys for Java Examples/Extasys.Examples.TCPClient/src/Extasys/Examples/TCPClient/TCPClient.java index fba1d3e..1ce7c22 100644 --- a/Extasys for Java Examples/Extasys.Examples.TCPClient/src/Extasys/Examples/TCPClient/TCPClient.java +++ b/Extasys for Java Examples/Extasys.Examples.TCPClient/src/Extasys/Examples/TCPClient/TCPClient.java @@ -34,7 +34,7 @@ public class TCPClient extends Extasys.Network.TCP.Client.ExtasysTCPClient { private boolean fKeepSendingMessages = false; - private final String fMessageToExchange = "Test Message"; + private final String fMessageToExchange = "128CharMessage111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"; private final String fMessageSplitter = "#SPLITTER#"; private final Charset fCharset = Charset.forName("UTF-8"); // This is the default character set to be used for all TCPConnectors @@ -57,7 +57,7 @@ public void OnDataReceive(TCPConnector connector, DataFrame data) { try { - final String dataReceivedFromServer = new String(data.getBytes(), fCharset); + //final String dataReceivedFromServer = new String(data.getBytes(), fCharset); // Every time I receive data from the server I send the // fMessageToExchange string back diff --git a/Extasys for Java Examples/Extasys.Examples.TCPClient/src/Extasys/Examples/TCPClient/frmMain.java b/Extasys for Java Examples/Extasys.Examples.TCPClient/src/Extasys/Examples/TCPClient/frmMain.java index e7eda61..5167f8d 100644 --- a/Extasys for Java Examples/Extasys.Examples.TCPClient/src/Extasys/Examples/TCPClient/frmMain.java +++ b/Extasys for Java Examples/Extasys.Examples.TCPClient/src/Extasys/Examples/TCPClient/frmMain.java @@ -32,6 +32,8 @@ public class frmMain extends javax.swing.JFrame private Thread fUpdateStatusThread; private boolean fUpdateStatusActive = true; + private long fOldBytesIn = 0, fOldBytesOut = 0; + public frmMain() { initComponents(); @@ -241,6 +243,7 @@ private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS fUpdateStatusThread = new Thread(new Runnable() { + @Override public void run() { try @@ -249,10 +252,16 @@ public void run() { if (fTCPClient != null) { - jLabelBytesIn.setText(String.valueOf(fTCPClient.getBytesIn())); - jLabelBytesOut.setText(String.valueOf(fTCPClient.getBytesOut())); + Long newBytesIn = fTCPClient.getBytesIn(); + Long newBytesOut = fTCPClient.getBytesOut(); + + jLabelBytesIn.setText(String.valueOf(newBytesIn) + " (" + String.valueOf((newBytesIn - fOldBytesIn) / 1000) + "kb/sec)"); + jLabelBytesOut.setText(String.valueOf(newBytesOut) + " (" + String.valueOf((newBytesOut - fOldBytesOut) / 1000) + "kb/sec)"); + + fOldBytesIn = newBytesIn; + fOldBytesOut = newBytesOut; } - Thread.sleep(500); + Thread.sleep(1000); } } catch (Exception ex) diff --git a/Extasys for Java Examples/Extasys.Examples.TCPServer/src/Extasys/Examples/TCPServer/TCPServer.java b/Extasys for Java Examples/Extasys.Examples.TCPServer/src/Extasys/Examples/TCPServer/TCPServer.java index 99e046c..e35b3fc 100644 --- a/Extasys for Java Examples/Extasys.Examples.TCPServer/src/Extasys/Examples/TCPServer/TCPServer.java +++ b/Extasys for Java Examples/Extasys.Examples.TCPServer/src/Extasys/Examples/TCPServer/TCPServer.java @@ -58,7 +58,7 @@ public void OnDataReceive(TCPClientConnection sender, DataFrame data) try { // I received data from a client - String incomingDataStr = new String(data.getBytes(), fCharset); + final String incomingDataStr = new String(data.getBytes(), fCharset); //System.out.println("Data received: " + incomingDataStr); // Send the incoming data back to the client diff --git a/Extasys for Java Examples/Extasys.Examples.TCPServer/src/Extasys/Examples/TCPServer/frmTCPServer.java b/Extasys for Java Examples/Extasys.Examples.TCPServer/src/Extasys/Examples/TCPServer/frmTCPServer.java index 6f440b5..68f42ae 100644 --- a/Extasys for Java Examples/Extasys.Examples.TCPServer/src/Extasys/Examples/TCPServer/frmTCPServer.java +++ b/Extasys for Java Examples/Extasys.Examples.TCPServer/src/Extasys/Examples/TCPServer/frmTCPServer.java @@ -282,8 +282,8 @@ public void run() Long newBytesIn = fTCPServer.getBytesIn(); Long newBytesOut = fTCPServer.getBytesOut(); - jLabelBytesIn.setText(String.valueOf(newBytesIn) + " (" + String.valueOf(newBytesIn - fOldBytesIn) + "/sec)"); - jLabelBytesOut.setText(String.valueOf(newBytesOut) + " (" + String.valueOf(newBytesOut - fOldBytesOut) + "/sec)"); + jLabelBytesIn.setText(String.valueOf(newBytesIn) + " (" + String.valueOf((newBytesIn - fOldBytesIn)/1000) + "kb/sec)"); + jLabelBytesOut.setText(String.valueOf(newBytesOut) + " (" + String.valueOf((newBytesOut - fOldBytesOut)/1000) + "kb/sec)"); jLabelClientsConnected.setText(String.valueOf(fTCPServer.getCurrentConnectionsNumber())); fOldBytesIn = newBytesIn; diff --git a/Extasys for Java/nbproject/project.properties b/Extasys for Java/nbproject/project.properties index a3215ae..2de83d1 100644 --- a/Extasys for Java/nbproject/project.properties +++ b/Extasys for Java/nbproject/project.properties @@ -4,7 +4,7 @@ annotation.processing.processors.list= annotation.processing.run.all.processors=true application.desc=TCP/UDP Socket for Java application.homepage= -application.title=Extasys 3.2 +application.title=Extasys v3.3 application.vendor=Nikos Siatras auxiliary.org-netbeans-spi-editor-hints-projects.perProjectHintSettingsFile=nbproject/cfg_hints.xml build.classes.dir=${build.dir}/classes diff --git a/Extasys for Java/src/Extasys/ByteArrayBuilder.java b/Extasys for Java/src/Extasys/ByteArrayBuilder.java index 0e63aec..c378ff9 100644 --- a/Extasys for Java/src/Extasys/ByteArrayBuilder.java +++ b/Extasys for Java/src/Extasys/ByteArrayBuilder.java @@ -57,11 +57,10 @@ public int IndexOf(byte[] subArray) { synchronized (fLock) { - int i = 0; int subArrayLength = subArray.length; // Find subArray in fBytes - for (i = 0; i < fBytes.length; i++) + for (int i = 0; i < fBytes.length; i++) { byte[] arrayToCompare = Arrays.copyOfRange(fBytes, i, i + subArrayLength); diff --git a/Extasys for Java/src/Extasys/ExtasysThreadPool.java b/Extasys for Java/src/Extasys/ExtasysThreadPool.java index 895e258..eab7d25 100644 --- a/Extasys for Java/src/Extasys/ExtasysThreadPool.java +++ b/Extasys for Java/src/Extasys/ExtasysThreadPool.java @@ -32,7 +32,7 @@ public class ExtasysThreadPool extends ThreadPoolExecutor public ExtasysThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit) { - super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new ArrayBlockingQueue(500000, true)); + super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new ArrayBlockingQueue(250000, true)); this.prestartAllCoreThreads(); } diff --git a/Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Packets/IncomingTCPClientPacket.java b/Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Packets/IncomingTCPClientPacket.java index 3086a45..26b41c8 100644 --- a/Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Packets/IncomingTCPClientPacket.java +++ b/Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Packets/IncomingTCPClientPacket.java @@ -31,7 +31,7 @@ of this software and associated documentation files (the "Software"), to deal public final class IncomingTCPClientPacket implements Runnable { - public ManualResetEvent fDone = new ManualResetEvent(false); + public final ManualResetEvent fDone = new ManualResetEvent(false); private final TCPConnector fConnector; private final DataFrame fData; private IncomingTCPClientPacket fPreviousPacket; @@ -130,9 +130,9 @@ public DataFrame getData() } /** - * Returns the previous incoming packet received by the client. If the packet - * is null means that the packet has been received and parsed from the - * client. + * Returns the previous incoming packet received by the client. If the + * packet is null means that the packet has been received and parsed from + * the client. * * @return the previous incoming packet received by the client. */ diff --git a/Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Packets/MessageCollectorTCPClientPacket.java b/Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Packets/MessageCollectorTCPClientPacket.java index 3d3852d..a16a9f7 100644 --- a/Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Packets/MessageCollectorTCPClientPacket.java +++ b/Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Packets/MessageCollectorTCPClientPacket.java @@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal public final class MessageCollectorTCPClientPacket implements Runnable { - public ManualResetEvent fDone = new ManualResetEvent(false); + public final ManualResetEvent fDone = new ManualResetEvent(false); private final TCPConnector fConnector; private final byte[] fData; private MessageCollectorTCPClientPacket fPreviousPacket; diff --git a/Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Packets/OutgoingTCPClientPacket.java b/Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Packets/OutgoingTCPClientPacket.java index 5dea41c..863a07e 100644 --- a/Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Packets/OutgoingTCPClientPacket.java +++ b/Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Packets/OutgoingTCPClientPacket.java @@ -32,7 +32,7 @@ of this software and associated documentation files (the "Software"), to deal public final class OutgoingTCPClientPacket implements Runnable { - public ManualResetEvent fDone = new ManualResetEvent(false); + public final ManualResetEvent fDone = new ManualResetEvent(false); private final TCPConnector fConnector; private final byte[] fBytes; private final int fOffset; diff --git a/Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Tools/TCPClientMessageCollector.java b/Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Tools/TCPClientMessageCollector.java index 82a37ed..e16c691 100644 --- a/Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Tools/TCPClientMessageCollector.java +++ b/Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Tools/TCPClientMessageCollector.java @@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal public class TCPClientMessageCollector { - private TCPConnector fMyConnector; + private final TCPConnector fMyConnector; private final byte[] fETXStr; private final ByteArrayBuilder fIncomingDataBuffer = new ByteArrayBuilder(); private final int fETXLength; @@ -50,7 +50,7 @@ public TCPClientMessageCollector(TCPConnector connector, String splitter) fETXLength = fETXStr.length; } - public void AppendData(byte[] bytes) + public void AppendData(final byte[] bytes) { try { @@ -58,7 +58,7 @@ public void AppendData(byte[] bytes) fIndexOfETX = fIncomingDataBuffer.IndexOf(fETXStr); while (fIndexOfETX > -1) { - fMyConnector.getMyExtasysTCPClient().OnDataReceive(fMyConnector, new DataFrame(fIncomingDataBuffer.SubList(0, fIndexOfETX))); + fMyConnector.fMyTCPClient.OnDataReceive(fMyConnector, new DataFrame(fIncomingDataBuffer.SubList(0, fIndexOfETX))); fIncomingDataBuffer.Delete(0, fIndexOfETX + fETXLength); fIndexOfETX = fIncomingDataBuffer.IndexOf(fETXStr); } @@ -73,7 +73,7 @@ public void Dispose() { try { - fMyConnector = null; + //fMyConnector = null; } catch (Exception ex) { diff --git a/Extasys for Java/src/Extasys/Network/TCP/Server/Listener/Packets/MessageCollectorTCPClientConnectionPacket.java b/Extasys for Java/src/Extasys/Network/TCP/Server/Listener/Packets/MessageCollectorTCPClientConnectionPacket.java index 92c2648..d8e58c4 100644 --- a/Extasys for Java/src/Extasys/Network/TCP/Server/Listener/Packets/MessageCollectorTCPClientConnectionPacket.java +++ b/Extasys for Java/src/Extasys/Network/TCP/Server/Listener/Packets/MessageCollectorTCPClientConnectionPacket.java @@ -48,7 +48,7 @@ public final class MessageCollectorTCPClientConnectionPacket implements Runnable * @param previousPacket is the previous message collector packet of the * TCPClientConnection. */ - public MessageCollectorTCPClientConnectionPacket(TCPClientConnection client, byte[] data, MessageCollectorTCPClientConnectionPacket previousPacket) + public MessageCollectorTCPClientConnectionPacket(final TCPClientConnection client, final byte[] data, final MessageCollectorTCPClientConnectionPacket previousPacket) { fClient = client; fData = data; diff --git a/Extasys for Java/src/Extasys/Network/TCP/Server/Listener/TCPClientConnection.java b/Extasys for Java/src/Extasys/Network/TCP/Server/Listener/TCPClientConnection.java index 3103aec..e6f079d 100644 --- a/Extasys for Java/src/Extasys/Network/TCP/Server/Listener/TCPClientConnection.java +++ b/Extasys for Java/src/Extasys/Network/TCP/Server/Listener/TCPClientConnection.java @@ -42,6 +42,7 @@ of this software and associated documentation files (the "Software"), to deal */ public final class TCPClientConnection { + // Connection properties protected Socket fConnection; protected boolean fActive = false; @@ -135,9 +136,9 @@ private void StartReceivingData() * @throws * Extasys.Network.TCP.Server.Listener.Exceptions.OutgoingPacketFailedException */ - public void SendData(String data) throws ClientIsDisconnectedException, OutgoingPacketFailedException + public void SendData(final String data) throws ClientIsDisconnectedException, OutgoingPacketFailedException { - byte[] bytes = data.getBytes(fMyListener.getCharset()); + final byte[] bytes = data.getBytes(fMyListener.getCharset()); SendData(bytes, 0, bytes.length); } @@ -171,9 +172,9 @@ public void SendData(byte[] bytes, int offset, int length) throws ClientIsDiscon * @param data is the string to send. * @throws ClientIsDisconnectedException */ - public void SendDataSynchronous(String data) throws ClientIsDisconnectedException + public void SendDataSynchronous(final String data) throws ClientIsDisconnectedException { - byte[] bytes = data.getBytes(fMyListener.getCharset()); + final byte[] bytes = data.getBytes(fMyListener.getCharset()); SendDataSynchronous(bytes, 0, bytes.length); } diff --git a/Extasys for Java/src/Extasys/Network/TCP/Server/Listener/Tools/TCPClientConnectionMessageCollector.java b/Extasys for Java/src/Extasys/Network/TCP/Server/Listener/Tools/TCPClientConnectionMessageCollector.java index 8bb257e..78218d9 100644 --- a/Extasys for Java/src/Extasys/Network/TCP/Server/Listener/Tools/TCPClientConnectionMessageCollector.java +++ b/Extasys for Java/src/Extasys/Network/TCP/Server/Listener/Tools/TCPClientConnectionMessageCollector.java @@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal public class TCPClientConnectionMessageCollector { - private TCPClientConnection fMyClient; + private final TCPClientConnection fMyClient; private final byte[] fETXStr; private final ByteArrayBuilder fIncomingDataBuffer; private int fIndexOfETX; @@ -52,7 +52,7 @@ public TCPClientConnectionMessageCollector(TCPClientConnection myClient, String fIncomingDataBuffer = new ByteArrayBuilder(); } - public void AppendData(byte[] bytes) + public void AppendData(final byte[] bytes) { try { @@ -60,7 +60,7 @@ public void AppendData(byte[] bytes) fIndexOfETX = fIncomingDataBuffer.IndexOf(fETXStr); while (fIndexOfETX > -1) { - fMyClient.getMyTCPListener().getMyExtasysTCPServer().OnDataReceive(fMyClient, new DataFrame(fIncomingDataBuffer.SubList(0, fIndexOfETX))); + fMyClient.fMyExtasysServer.OnDataReceive(fMyClient, new DataFrame(fIncomingDataBuffer.SubList(0, fIndexOfETX))); fIncomingDataBuffer.Delete(0, fIndexOfETX + fETXLength); fIndexOfETX = fIncomingDataBuffer.IndexOf(fETXStr); } @@ -75,7 +75,7 @@ public void Dispose() { try { - fMyClient = null; + } catch (Exception ex) {