slt ;
j'ai 2 bouttons 1 "demarrer"qui permet de demarrer un seveur qui reste en ecoute
sur un port 514 puis affiche les messages recus ainsi lorsque on clique sur le 2eme boutton la socket est fermer or j'ai pas pu resoudre ce proble car mon code qui est en dessous
permet seulement d'analyser un seul msg
j'ai essayes la boucle while mais ca pas marchée:
//*********le code
package syslog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
/**
Implements a basic syslog server receiving data on port 514 and
logging it to a file called syslog.log
*/
public class JavalogServer
{
public DatagramSocket socket;
public String[] start() throws Exception
{
//Create the buffer to store the data as it comes in
byte[] log_buffer = new byte[5000];
//Create the output stream so we can dump the data
try {
//Create a DatagramPacket to receive the incoming log data
DatagramPacket packet =
new DatagramPacket(log_buffer, log_buffer.length);
//Create a socket that listens on the net
DatagramSocket socket = new DatagramSocket(514);
socket.receive(packet);
//Build a string of the packet data
String packet_string =
new String(log_buffer, 0, 0, packet.getLength());
//Put the packet data after a bit of header so we can see where it comes from
String[] tab=new String[2];
tab[0]= packet.getAddress().getHostAddress().toString();
//.getHostName()
tab[1]=packet_string ;
socket.close();
return tab ;
}
catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
//***********************
voici le code sur le boutton demarré
///*********
JavalogServer iii=new JavalogServer ();
int i = statsTableModel.getRowCount();
int j = statsTableModel.getColumnCount();
Date date = new Date();
//-------- date -------------
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat timef = new SimpleDateFormat("HH:mm");
String s = timef.format(new Date(System.currentTimeMillis()));
Object[] data = new Object[j];
data[0] = sdf.format(date).toString();
data[1] = s;
try{
String[] tab=new String[2];
tab=iii.start();
data[3]=tab[0];
data[5]=tab[1];
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "erreur de reception de msg");
}
statsTableModel.addRow(data);
// socket.close();
}
///*/**************