Skip to content

Commit 40f432e

Browse files
committedJul 24, 2022
TCP Server and Client example refreshed
1 parent 777b5db commit 40f432e

File tree

14 files changed

+40
-31
lines changed

14 files changed

+40
-31
lines changed
 

‎Extasys for Java Examples/Extasys.Examples.TCPClient/src/Extasys/Examples/TCPClient/TCPClient.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class TCPClient extends Extasys.Network.TCP.Client.ExtasysTCPClient
3434
{
3535

3636
private boolean fKeepSendingMessages = false;
37-
private final String fMessageToExchange = "Test Message";
37+
private final String fMessageToExchange = "128CharMessage111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
3838
private final String fMessageSplitter = "#SPLITTER#";
3939

4040
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)
5757
{
5858
try
5959
{
60-
final String dataReceivedFromServer = new String(data.getBytes(), fCharset);
60+
//final String dataReceivedFromServer = new String(data.getBytes(), fCharset);
6161

6262
// Every time I receive data from the server I send the
6363
// fMessageToExchange string back

‎Extasys for Java Examples/Extasys.Examples.TCPClient/src/Extasys/Examples/TCPClient/frmMain.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class frmMain extends javax.swing.JFrame
3232
private Thread fUpdateStatusThread;
3333
private boolean fUpdateStatusActive = true;
3434

35+
private long fOldBytesIn = 0, fOldBytesOut = 0;
36+
3537
public frmMain()
3638
{
3739
initComponents();
@@ -241,6 +243,7 @@ private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS
241243
fUpdateStatusThread = new Thread(new Runnable()
242244
{
243245

246+
@Override
244247
public void run()
245248
{
246249
try
@@ -249,10 +252,16 @@ public void run()
249252
{
250253
if (fTCPClient != null)
251254
{
252-
jLabelBytesIn.setText(String.valueOf(fTCPClient.getBytesIn()));
253-
jLabelBytesOut.setText(String.valueOf(fTCPClient.getBytesOut()));
255+
Long newBytesIn = fTCPClient.getBytesIn();
256+
Long newBytesOut = fTCPClient.getBytesOut();
257+
258+
jLabelBytesIn.setText(String.valueOf(newBytesIn) + " (" + String.valueOf((newBytesIn - fOldBytesIn) / 1000) + "kb/sec)");
259+
jLabelBytesOut.setText(String.valueOf(newBytesOut) + " (" + String.valueOf((newBytesOut - fOldBytesOut) / 1000) + "kb/sec)");
260+
261+
fOldBytesIn = newBytesIn;
262+
fOldBytesOut = newBytesOut;
254263
}
255-
Thread.sleep(500);
264+
Thread.sleep(1000);
256265
}
257266
}
258267
catch (Exception ex)

‎Extasys for Java Examples/Extasys.Examples.TCPServer/src/Extasys/Examples/TCPServer/TCPServer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void OnDataReceive(TCPClientConnection sender, DataFrame data)
5858
try
5959
{
6060
// I received data from a client
61-
String incomingDataStr = new String(data.getBytes(), fCharset);
61+
final String incomingDataStr = new String(data.getBytes(), fCharset);
6262
//System.out.println("Data received: " + incomingDataStr);
6363

6464
// Send the incoming data back to the client

‎Extasys for Java Examples/Extasys.Examples.TCPServer/src/Extasys/Examples/TCPServer/frmTCPServer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ public void run()
282282
Long newBytesIn = fTCPServer.getBytesIn();
283283
Long newBytesOut = fTCPServer.getBytesOut();
284284

285-
jLabelBytesIn.setText(String.valueOf(newBytesIn) + " (" + String.valueOf(newBytesIn - fOldBytesIn) + "/sec)");
286-
jLabelBytesOut.setText(String.valueOf(newBytesOut) + " (" + String.valueOf(newBytesOut - fOldBytesOut) + "/sec)");
285+
jLabelBytesIn.setText(String.valueOf(newBytesIn) + " (" + String.valueOf((newBytesIn - fOldBytesIn)/1000) + "kb/sec)");
286+
jLabelBytesOut.setText(String.valueOf(newBytesOut) + " (" + String.valueOf((newBytesOut - fOldBytesOut)/1000) + "kb/sec)");
287287
jLabelClientsConnected.setText(String.valueOf(fTCPServer.getCurrentConnectionsNumber()));
288288

289289
fOldBytesIn = newBytesIn;

‎Extasys for Java/nbproject/project.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ annotation.processing.processors.list=
44
annotation.processing.run.all.processors=true
55
application.desc=TCP/UDP Socket for Java
66
application.homepage=
7-
application.title=Extasys 3.2
7+
application.title=Extasys v3.3
88
application.vendor=Nikos Siatras
99
auxiliary.org-netbeans-spi-editor-hints-projects.perProjectHintSettingsFile=nbproject/cfg_hints.xml
1010
build.classes.dir=${build.dir}/classes

‎Extasys for Java/src/Extasys/ByteArrayBuilder.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@ public int IndexOf(byte[] subArray)
5757
{
5858
synchronized (fLock)
5959
{
60-
int i = 0;
6160
int subArrayLength = subArray.length;
6261

6362
// Find subArray in fBytes
64-
for (i = 0; i < fBytes.length; i++)
63+
for (int i = 0; i < fBytes.length; i++)
6564
{
6665
byte[] arrayToCompare = Arrays.copyOfRange(fBytes, i, i + subArrayLength);
6766

‎Extasys for Java/src/Extasys/ExtasysThreadPool.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class ExtasysThreadPool extends ThreadPoolExecutor
3232

3333
public ExtasysThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit)
3434
{
35-
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new ArrayBlockingQueue(500000, true));
35+
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new ArrayBlockingQueue(250000, true));
3636
this.prestartAllCoreThreads();
3737
}
3838

‎Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Packets/IncomingTCPClientPacket.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ of this software and associated documentation files (the "Software"), to deal
3131
public final class IncomingTCPClientPacket implements Runnable
3232
{
3333

34-
public ManualResetEvent fDone = new ManualResetEvent(false);
34+
public final ManualResetEvent fDone = new ManualResetEvent(false);
3535
private final TCPConnector fConnector;
3636
private final DataFrame fData;
3737
private IncomingTCPClientPacket fPreviousPacket;
@@ -130,9 +130,9 @@ public DataFrame getData()
130130
}
131131

132132
/**
133-
* Returns the previous incoming packet received by the client. If the packet
134-
* is null means that the packet has been received and parsed from the
135-
* client.
133+
* Returns the previous incoming packet received by the client. If the
134+
* packet is null means that the packet has been received and parsed from
135+
* the client.
136136
*
137137
* @return the previous incoming packet received by the client.
138138
*/

‎Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Packets/MessageCollectorTCPClientPacket.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal
3030
public final class MessageCollectorTCPClientPacket implements Runnable
3131
{
3232

33-
public ManualResetEvent fDone = new ManualResetEvent(false);
33+
public final ManualResetEvent fDone = new ManualResetEvent(false);
3434
private final TCPConnector fConnector;
3535
private final byte[] fData;
3636
private MessageCollectorTCPClientPacket fPreviousPacket;

‎Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Packets/OutgoingTCPClientPacket.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ of this software and associated documentation files (the "Software"), to deal
3232
public final class OutgoingTCPClientPacket implements Runnable
3333
{
3434

35-
public ManualResetEvent fDone = new ManualResetEvent(false);
35+
public final ManualResetEvent fDone = new ManualResetEvent(false);
3636
private final TCPConnector fConnector;
3737
private final byte[] fBytes;
3838
private final int fOffset;

‎Extasys for Java/src/Extasys/Network/TCP/Client/Connectors/Tools/TCPClientMessageCollector.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal
3030
public class TCPClientMessageCollector
3131
{
3232

33-
private TCPConnector fMyConnector;
33+
private final TCPConnector fMyConnector;
3434
private final byte[] fETXStr;
3535
private final ByteArrayBuilder fIncomingDataBuffer = new ByteArrayBuilder();
3636
private final int fETXLength;
@@ -50,15 +50,15 @@ public TCPClientMessageCollector(TCPConnector connector, String splitter)
5050
fETXLength = fETXStr.length;
5151
}
5252

53-
public void AppendData(byte[] bytes)
53+
public void AppendData(final byte[] bytes)
5454
{
5555
try
5656
{
5757
fIncomingDataBuffer.Append(bytes);
5858
fIndexOfETX = fIncomingDataBuffer.IndexOf(fETXStr);
5959
while (fIndexOfETX > -1)
6060
{
61-
fMyConnector.getMyExtasysTCPClient().OnDataReceive(fMyConnector, new DataFrame(fIncomingDataBuffer.SubList(0, fIndexOfETX)));
61+
fMyConnector.fMyTCPClient.OnDataReceive(fMyConnector, new DataFrame(fIncomingDataBuffer.SubList(0, fIndexOfETX)));
6262
fIncomingDataBuffer.Delete(0, fIndexOfETX + fETXLength);
6363
fIndexOfETX = fIncomingDataBuffer.IndexOf(fETXStr);
6464
}
@@ -73,7 +73,7 @@ public void Dispose()
7373
{
7474
try
7575
{
76-
fMyConnector = null;
76+
//fMyConnector = null;
7777
}
7878
catch (Exception ex)
7979
{

‎Extasys for Java/src/Extasys/Network/TCP/Server/Listener/Packets/MessageCollectorTCPClientConnectionPacket.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public final class MessageCollectorTCPClientConnectionPacket implements Runnable
4848
* @param previousPacket is the previous message collector packet of the
4949
* TCPClientConnection.
5050
*/
51-
public MessageCollectorTCPClientConnectionPacket(TCPClientConnection client, byte[] data, MessageCollectorTCPClientConnectionPacket previousPacket)
51+
public MessageCollectorTCPClientConnectionPacket(final TCPClientConnection client, final byte[] data, final MessageCollectorTCPClientConnectionPacket previousPacket)
5252
{
5353
fClient = client;
5454
fData = data;

‎Extasys for Java/src/Extasys/Network/TCP/Server/Listener/TCPClientConnection.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ of this software and associated documentation files (the "Software"), to deal
4242
*/
4343
public final class TCPClientConnection
4444
{
45+
4546
// Connection properties
4647
protected Socket fConnection;
4748
protected boolean fActive = false;
@@ -135,9 +136,9 @@ private void StartReceivingData()
135136
* @throws
136137
* Extasys.Network.TCP.Server.Listener.Exceptions.OutgoingPacketFailedException
137138
*/
138-
public void SendData(String data) throws ClientIsDisconnectedException, OutgoingPacketFailedException
139+
public void SendData(final String data) throws ClientIsDisconnectedException, OutgoingPacketFailedException
139140
{
140-
byte[] bytes = data.getBytes(fMyListener.getCharset());
141+
final byte[] bytes = data.getBytes(fMyListener.getCharset());
141142
SendData(bytes, 0, bytes.length);
142143
}
143144

@@ -171,9 +172,9 @@ public void SendData(byte[] bytes, int offset, int length) throws ClientIsDiscon
171172
* @param data is the string to send.
172173
* @throws ClientIsDisconnectedException
173174
*/
174-
public void SendDataSynchronous(String data) throws ClientIsDisconnectedException
175+
public void SendDataSynchronous(final String data) throws ClientIsDisconnectedException
175176
{
176-
byte[] bytes = data.getBytes(fMyListener.getCharset());
177+
final byte[] bytes = data.getBytes(fMyListener.getCharset());
177178
SendDataSynchronous(bytes, 0, bytes.length);
178179
}
179180

‎Extasys for Java/src/Extasys/Network/TCP/Server/Listener/Tools/TCPClientConnectionMessageCollector.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal
3030
public class TCPClientConnectionMessageCollector
3131
{
3232

33-
private TCPClientConnection fMyClient;
33+
private final TCPClientConnection fMyClient;
3434
private final byte[] fETXStr;
3535
private final ByteArrayBuilder fIncomingDataBuffer;
3636
private int fIndexOfETX;
@@ -52,15 +52,15 @@ public TCPClientConnectionMessageCollector(TCPClientConnection myClient, String
5252
fIncomingDataBuffer = new ByteArrayBuilder();
5353
}
5454

55-
public void AppendData(byte[] bytes)
55+
public void AppendData(final byte[] bytes)
5656
{
5757
try
5858
{
5959
fIncomingDataBuffer.Append(bytes);
6060
fIndexOfETX = fIncomingDataBuffer.IndexOf(fETXStr);
6161
while (fIndexOfETX > -1)
6262
{
63-
fMyClient.getMyTCPListener().getMyExtasysTCPServer().OnDataReceive(fMyClient, new DataFrame(fIncomingDataBuffer.SubList(0, fIndexOfETX)));
63+
fMyClient.fMyExtasysServer.OnDataReceive(fMyClient, new DataFrame(fIncomingDataBuffer.SubList(0, fIndexOfETX)));
6464
fIncomingDataBuffer.Delete(0, fIndexOfETX + fETXLength);
6565
fIndexOfETX = fIncomingDataBuffer.IndexOf(fETXStr);
6666
}
@@ -75,7 +75,7 @@ public void Dispose()
7575
{
7676
try
7777
{
78-
fMyClient = null;
78+
7979
}
8080
catch (Exception ex)
8181
{

0 commit comments

Comments
 (0)
Please sign in to comment.