Skip to content

Commit

Permalink
TCP Server and Client example refreshed
Browse files Browse the repository at this point in the history
  • Loading branch information
nsiatras committed Jul 24, 2022
1 parent 777b5db commit 40f432e
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -241,6 +243,7 @@ private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS
fUpdateStatusThread = new Thread(new Runnable()
{

@Override
public void run()
{
try
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Extasys for Java/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions Extasys for Java/src/Extasys/ByteArrayBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion Extasys for Java/src/Extasys/ExtasysThreadPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,15 +50,15 @@ public TCPClientMessageCollector(TCPConnector connector, String splitter)
fETXLength = fETXStr.length;
}

public void AppendData(byte[] bytes)
public void AppendData(final byte[] bytes)
{
try
{
fIncomingDataBuffer.Append(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);
}
Expand All @@ -73,7 +73,7 @@ public void Dispose()
{
try
{
fMyConnector = null;
//fMyConnector = null;
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,15 +52,15 @@ public TCPClientConnectionMessageCollector(TCPClientConnection myClient, String
fIncomingDataBuffer = new ByteArrayBuilder();
}

public void AppendData(byte[] bytes)
public void AppendData(final byte[] bytes)
{
try
{
fIncomingDataBuffer.Append(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);
}
Expand All @@ -75,7 +75,7 @@ public void Dispose()
{
try
{
fMyClient = null;

}
catch (Exception ex)
{
Expand Down

0 comments on commit 40f432e

Please sign in to comment.