Skip to content

Commit

Permalink
Client and Server projects
Browse files Browse the repository at this point in the history
  • Loading branch information
LuMarans30 authored Sep 21, 2022
1 parent 54ccbaa commit 09e3e03
Show file tree
Hide file tree
Showing 28 changed files with 451 additions and 0 deletions.
12 changes: 12 additions & 0 deletions clientSocket/clientSocket.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="flatlaf-2.4" level="project" />
</component>
</module>
Binary file added clientSocket/lib/flatlaf-2.4.jar
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions clientSocket/out/production/clientSocket/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: maranoClient

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions clientSocket/src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: maranoClient

93 changes: 93 additions & 0 deletions clientSocket/src/maranoClient.form
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="maranoClient">
<grid id="27dc6" binding="jPanel1" layout-manager="GridLayoutManager" row-count="8" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="521" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="5f2b" class="javax.swing.JTextField" binding="txtIpPorta">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value="localhost:5000"/>
</properties>
</component>
<component id="a5782" class="javax.swing.JTextField" binding="txtOp">
<constraints>
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<hspacer id="328b5">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="c3c25">
<constraints>
<grid row="7" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<vspacer id="a909c">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="ddffa" class="javax.swing.JButton" binding="btnInvio">
<constraints>
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Invio"/>
</properties>
</component>
<hspacer id="c1a43">
<constraints>
<grid row="4" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="30b4" class="javax.swing.JTextField" binding="txtRisultato">
<constraints>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<editable value="false"/>
</properties>
</component>
<component id="97cfd" class="javax.swing.JButton" binding="btnCollega">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Collega"/>
</properties>
</component>
<component id="4cba" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="IP e porta: "/>
</properties>
</component>
<component id="dafe9" class="javax.swing.JLabel">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Operazione: (exit per terminare la connessione)"/>
</properties>
</component>
</children>
</grid>
</form>
182 changes: 182 additions & 0 deletions clientSocket/src/maranoClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import com.formdev.flatlaf.FlatDarkLaf;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Locale;

import static java.lang.System.exit;

/**
* La classe maranoClient, sottoclasse di JFrame, è il client GUI che gestisce la connessione da/a un server (di default locale)
* @author Andrea Marano
* @version 1.0
*/
public class maranoClient extends JFrame {

/**
* Contiene l'indirizzo ip e la porta del server
*/
private JTextField txtIpPorta;

/**
* Contiene l'espressione matematica da inviare al server
*/
private JTextField txtOp;

/**
* Invia i dati al server
*/
private JButton btnInvio;

/**
* Il risultato dell'espressione matematica ricevuto dal server
*/
private JTextField txtRisultato;

/**
* Container che contiene tutti i componenti grafici utilizzati
*/
private JPanel jPanel1;

/**
* Esegue la connessione verso il server utilizzando l'ip e porta specificati da txtIpPorta
*/
private JButton btnCollega;

/**
* Flusso di dati in input (dal server al client)
*/
private DataInputStream socketInput;

/**
* Flusso di dati in output (dal client al server)
*/
private DataOutputStream socketOutput;

/**
* Gestisce la connessione client/server
*/
private Socket clientSocket;

/**
* Apertura della connessione verso l'indirizzo IP e porta specificati in input
* @param ip l'ip del server
* @param porta La porta aperta del server
* @throws IOException se la connessione al server fallisce
*/
public void start(String ip, int porta) throws IOException {

clientSocket = new Socket(ip,porta);
}

/**
* Terminazione della connessione, dei relativi stream e chiusura del client
* @throws IOException se la chiusura delle connessioni o stream fallisce
*/
public void stop() throws IOException {

socketInput.close();
socketOutput.close();
clientSocket.close();
exit(0);
}

/**
* Invio dell'operazione da svolgere al server e lettura del risultato
* @param operazione l'espressione matematica che il server deve risolvere
* @throws IOException se la lettura/scrittura dei dati da/a il server fallisce o se il metodo stop() lancia un'eccezzione
*/
public void work(String operazione) throws IOException {

socketInput = new DataInputStream(clientSocket.getInputStream());
socketOutput = new DataOutputStream(clientSocket.getOutputStream());

socketOutput.writeUTF(operazione);
String msg = socketInput.readUTF();
txtRisultato.setText(msg);

if(operazione.toLowerCase(Locale.ROOT).equals("exit"))
{
stop();
}
}


/**
* Costruttore di default
*/
public maranoClient() {

btnInvio.setEnabled(false);

btnInvio.addActionListener(new ActionListener() {
/**
* Invoked when an action occurs.
* Invia l'espressione matematica al server tramite un socket
* @param e the event to be processed
*/
@Override
public void actionPerformed(ActionEvent e) {
try
{
if(!txtOp.getText().isEmpty())
work(txtOp.getText());
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage(), "Errore", JOptionPane.ERROR_MESSAGE);
}
}
});

btnCollega.addActionListener(new ActionListener() {
/**
* Invoked when an action occurs.
* Collegamento al server
* @param e the event to be processed
*/
@Override
public void actionPerformed(ActionEvent e) {
try
{
if(!txtIpPorta.getText().isEmpty()) {
String ip = txtIpPorta.getText().split(":")[0];
int porta = Integer.parseInt(txtIpPorta.getText().split(":")[1]);
start(ip,porta);
}

btnCollega.setEnabled(false);
btnInvio.setEnabled(true);
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage(), "Errore", JOptionPane.ERROR_MESSAGE);
}
}
});
}

/**
* Metodo main del client
* @param args argomenti del metodo main
*/
public static void main(String[] args) {
FlatDarkLaf.setup();
JFrame frame = new maranoClient();
frame.setTitle("Client");
frame.setContentPane(new maranoClient().jPanel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setSize(new Dimension(480,360));
frame.setVisible(true);
frame.setResizable(false);
}


}
Binary file added serverSocket/lib/MathParser.org-mXparser.jar
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions serverSocket/out/production/serverSocket/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: maranoServer

Binary file not shown.
14 changes: 14 additions & 0 deletions serverSocket/serverSocket.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="MathParser.org-mXparser" level="project" />
<orderEntry type="library" name="serverSocket" level="project" />
<orderEntry type="library" name="maranoServerSocket" level="project" />
</component>
</module>
3 changes: 3 additions & 0 deletions serverSocket/src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: maranoServer

Loading

0 comments on commit 09e3e03

Please sign in to comment.