inserimento app chifu
This commit is contained in:
BIN
chifu/src/.DS_Store
vendored
Normal file
BIN
chifu/src/.DS_Store
vendored
Normal file
Binary file not shown.
10
chifu/src/chifu/ChiFu.java
Normal file
10
chifu/src/chifu/ChiFu.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package chifu;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class ChiFu {
|
||||
public static void main(String[] args) {
|
||||
JFrame gui = new Finestra();
|
||||
gui.setVisible(true);
|
||||
}
|
||||
}
|
||||
82
chifu/src/chifu/Finestra.form
Normal file
82
chifu/src/chifu/Finestra.form
Normal file
@@ -0,0 +1,82 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
|
||||
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
|
||||
<Properties>
|
||||
<Property name="defaultCloseOperation" type="int" value="3"/>
|
||||
</Properties>
|
||||
<SyntheticProperties>
|
||||
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
|
||||
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
|
||||
</SyntheticProperties>
|
||||
<AuxValues>
|
||||
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
|
||||
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
|
||||
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
|
||||
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
|
||||
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
|
||||
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
|
||||
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
|
||||
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
|
||||
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
|
||||
</AuxValues>
|
||||
|
||||
<Layout>
|
||||
<DimensionLayout dim="0">
|
||||
<Group type="103" groupAlignment="0" attributes="0">
|
||||
<Group type="102" attributes="0">
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
<Group type="103" groupAlignment="0" attributes="0">
|
||||
<Component id="jScrollPane1" max="32767" attributes="0"/>
|
||||
<Group type="102" attributes="0">
|
||||
<Component id="jTextField1" min="-2" pref="281" max="-2" attributes="0"/>
|
||||
<EmptySpace type="separate" max="-2" attributes="0"/>
|
||||
<Component id="jButton1" pref="89" max="32767" attributes="0"/>
|
||||
</Group>
|
||||
</Group>
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
</Group>
|
||||
</Group>
|
||||
</DimensionLayout>
|
||||
<DimensionLayout dim="1">
|
||||
<Group type="103" groupAlignment="0" attributes="0">
|
||||
<Group type="102" alignment="0" attributes="0">
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
<Component id="jScrollPane1" min="-2" pref="241" max="-2" attributes="0"/>
|
||||
<EmptySpace pref="24" max="32767" attributes="0"/>
|
||||
<Group type="103" groupAlignment="3" attributes="0">
|
||||
<Component id="jTextField1" alignment="3" min="-2" max="-2" attributes="0"/>
|
||||
<Component id="jButton1" alignment="3" min="-2" max="-2" attributes="0"/>
|
||||
</Group>
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
</Group>
|
||||
</Group>
|
||||
</DimensionLayout>
|
||||
</Layout>
|
||||
<SubComponents>
|
||||
<Container class="javax.swing.JScrollPane" name="jScrollPane1">
|
||||
<AuxValues>
|
||||
<AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
|
||||
</AuxValues>
|
||||
|
||||
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
|
||||
<SubComponents>
|
||||
<Component class="javax.swing.JTextArea" name="jTextArea1">
|
||||
<Properties>
|
||||
<Property name="columns" type="int" value="20"/>
|
||||
<Property name="rows" type="int" value="5"/>
|
||||
</Properties>
|
||||
</Component>
|
||||
</SubComponents>
|
||||
</Container>
|
||||
<Component class="javax.swing.JTextField" name="jTextField1">
|
||||
</Component>
|
||||
<Component class="javax.swing.JButton" name="jButton1">
|
||||
<Properties>
|
||||
<Property name="text" type="java.lang.String" value="Invio"/>
|
||||
</Properties>
|
||||
<Events>
|
||||
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButton1ActionPerformed"/>
|
||||
</Events>
|
||||
</Component>
|
||||
</SubComponents>
|
||||
</Form>
|
||||
112
chifu/src/chifu/Finestra.java
Normal file
112
chifu/src/chifu/Finestra.java
Normal file
@@ -0,0 +1,112 @@
|
||||
|
||||
package chifu;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
public class Finestra extends javax.swing.JFrame {
|
||||
|
||||
private boolean server_on;
|
||||
|
||||
public Finestra() {
|
||||
initComponents();
|
||||
server_on = false;
|
||||
avviaLoop();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called from within the constructor to initialize the form.
|
||||
* WARNING: Do NOT modify this code. The content of this method is always
|
||||
* regenerated by the Form Editor.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
|
||||
private void initComponents() {
|
||||
|
||||
jScrollPane1 = new javax.swing.JScrollPane();
|
||||
jTextArea1 = new javax.swing.JTextArea();
|
||||
jTextField1 = new javax.swing.JTextField();
|
||||
jButton1 = new javax.swing.JButton();
|
||||
|
||||
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
|
||||
|
||||
jTextArea1.setColumns(20);
|
||||
jTextArea1.setRows(5);
|
||||
jScrollPane1.setViewportView(jTextArea1);
|
||||
|
||||
jButton1.setText("Invio");
|
||||
jButton1.addActionListener(new java.awt.event.ActionListener() {
|
||||
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
||||
jButton1ActionPerformed(evt);
|
||||
}
|
||||
});
|
||||
|
||||
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
|
||||
getContentPane().setLayout(layout);
|
||||
layout.setHorizontalGroup(
|
||||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addContainerGap()
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addComponent(jScrollPane1)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addGap(18, 18, 18)
|
||||
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)))
|
||||
.addContainerGap())
|
||||
);
|
||||
layout.setVerticalGroup(
|
||||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
||||
.addGroup(layout.createSequentialGroup()
|
||||
.addContainerGap()
|
||||
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
||||
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addComponent(jButton1))
|
||||
.addContainerGap())
|
||||
);
|
||||
|
||||
pack();
|
||||
}// </editor-fold>//GEN-END:initComponents
|
||||
|
||||
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
|
||||
try {
|
||||
// TODO add your handling code here:
|
||||
GestoreServer.scrivi(jTextField1.getText());
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(null, ex.toString());
|
||||
}
|
||||
jTextField1.setText("");
|
||||
}//GEN-LAST:event_jButton1ActionPerformed
|
||||
|
||||
private void avviaLoop() {
|
||||
new Thread(() -> {
|
||||
while(true) {
|
||||
if(!server_on) {
|
||||
System.out.println("Avvio il server");
|
||||
|
||||
server_on = true;
|
||||
try
|
||||
{
|
||||
GestoreServer.messaggio = GestoreServer.ascolta();
|
||||
server_on = false;
|
||||
System.out.println("ho ricevuto un qualcosa e dovrei avere un messaggio");
|
||||
jTextArea1.append(GestoreServer.messaggio.messaggio + "\n");
|
||||
GestoreServer.messaggio = null;
|
||||
repaint();
|
||||
System.out.println("fine metodo");
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(null, ex.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
// Variables declaration - do not modify//GEN-BEGIN:variables
|
||||
private javax.swing.JButton jButton1;
|
||||
private javax.swing.JScrollPane jScrollPane1;
|
||||
private javax.swing.JTextArea jTextArea1;
|
||||
private javax.swing.JTextField jTextField1;
|
||||
// End of variables declaration//GEN-END:variables
|
||||
}
|
||||
54
chifu/src/chifu/GestoreServer.java
Normal file
54
chifu/src/chifu/GestoreServer.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package chifu;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class GestoreServer {
|
||||
private static int port = 50000;
|
||||
private static int dim_buffer = 512;
|
||||
private static String host = "172.16.127.255";
|
||||
public static Messaggio messaggio = null;
|
||||
|
||||
public static Messaggio ascolta() throws SocketException, IOException, ClassNotFoundException {
|
||||
Messaggio messaggio = null;
|
||||
DatagramSocket server = new DatagramSocket(port);
|
||||
byte[] buffer = new byte[dim_buffer];
|
||||
DatagramPacket pacchetto = new DatagramPacket(buffer, dim_buffer);
|
||||
System.out.println("Datagram Server in ascolto");
|
||||
server.receive(pacchetto);
|
||||
System.out.println("Ho ricevuto un messaggio da: " +
|
||||
pacchetto.getAddress().getHostAddress());
|
||||
|
||||
messaggio = (Messaggio) new ObjectInputStream(new ByteArrayInputStream(pacchetto.getData())).readObject();
|
||||
|
||||
server.close();
|
||||
System.out.println("Server chiuso");
|
||||
|
||||
return messaggio;
|
||||
}
|
||||
|
||||
public static void scrivi(String testo) throws UnknownHostException, SocketException, IOException {
|
||||
|
||||
Messaggio messaggio = new Messaggio(host, testo);
|
||||
|
||||
InetAddress destinatario = InetAddress.getByName(host);
|
||||
DatagramSocket client = new DatagramSocket();
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
new ObjectOutputStream(out).writeObject(messaggio);
|
||||
byte[] flusso_messaggio = out.toByteArray();
|
||||
|
||||
DatagramPacket pacchetto = new DatagramPacket(flusso_messaggio,
|
||||
flusso_messaggio.length, destinatario, port);
|
||||
client.send(pacchetto);
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
15
chifu/src/chifu/Messaggio.java
Normal file
15
chifu/src/chifu/Messaggio.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package chifu;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Messaggio implements Serializable {
|
||||
public String nome;
|
||||
public String ip;
|
||||
public String messaggio;
|
||||
|
||||
public Messaggio(String ip, String messaggio) {
|
||||
this.ip = ip;
|
||||
this.nome = ip;
|
||||
this.messaggio = messaggio;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user