Realizzare una chat con flash e java (parte 2 il server)
Posted on 30. Apr, 2006 by Administrator in flash, java, tutorials
In questo articolo tratteremo come realizzare il server per la chat in java.Questo server riceve qualsiasi tipo di messaggi da un client e lo manda a tutti
i clients conessi ad esso.
Lo useremo in futuro per altre applicazioni.
Il server si avvia con il comando "java server 1111" dove 1111 è la porta da
utilizzare

Il codice del nostro server è:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.Vector;
public class server extends JFrame
{
static public JLabel jLabel2;
static public JLabel jLabel3;
static public JLabel jLabel4;
static public JTextField portText;
static public JTextArea ServerMessage;
static public JScrollPane jScrollPane1;
static public JPanel contentPane;
static Vector out = new Vector();
static server mioserver;
public server()
{
super();
initializeComponent();
this.setVisible(true);
}
public void initializeComponent()
{
jLabel2 = new JLabel();
jLabel3 = new JLabel();
jLabel4 = new JLabel();
portText = new JTextField();
ServerMessage = new JTextArea();
jScrollPane1 = new JScrollPane();
contentPane = (JPanel)this.getContentPane();
jLabel2.setText("porta");
jLabel3.setText("Java server creato da");
jLabel4.setText("www.sastgroup.com");
jScrollPane1.setViewportView(ServerMessage);
contentPane.setLayout(null);
addComponent(contentPane, jLabel2, 265,282,34,18);
addComponent(contentPane, jLabel3, 122,242,143,18);
addComponent(contentPane, jLabel4, 123,269,148,18);
addComponent(contentPane, portText, 306,278,71,22);
addComponent(contentPane, jScrollPane1, 4,6,373,227);
this.setTitle("Server");
this.setLocation(new Point(48, 52));
this.setSize(new Dimension(390, 336));
}
public void addComponent(Container container,Component c,int x,int y,int
width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}
public void scrivimessaggio(String str){
ServerMessage.append("Messaggio"+str+"\n");
ServerMessage.updateUI(); //aggiorna
}
public static void main(String[] args)
{
mioserver=new server();
portText.setText(""+Integer.parseInt(args[0]));
ServerMessage.append( "Porta:"+portText.getText()+"\n" );
try
{
ServerSocket s = new ServerSocket(Integer.parseInt(portText.getText()));
ServerMessage.append( "Server in attesa ...\n" );
boolean done = false;
while (!done)
{
try
{
Socket incoming = s.accept();
DataInputStream chatIn = new DataInputStream(incoming.getInputStream());
DataOutputStream chatOut = new DataOutputStream(incoming.getOutputStream());
out.addElement(chatOut);
String str = chatIn.readLine();
if (str == null)
done = false;
else {
ServerMessage.append( "E' entrato:"+str+"\n" );
(new ThreadServer(chatIn,chatOut,mioserver)).start();
}
}
catch (Exception es){ System.out.println(es);}
}
s.close();
}
catch (Exception ex) { System.out.println(ex); }
}
}
class ThreadServer extends Thread
{
DataInputStream chatIn;
DataOutputStream chatOut;
server mioserver;
ThreadServer(DataInputStream chatIn, DataOutputStream chatOut,server mioserver)
{
this.chatIn = chatIn;
this.chatOut = chatOut;
this.mioserver = mioserver;
}
public void run()
{
try
{
while (true)
{
String str = chatIn.readLine();
if (str == null) break;
//scrive il messaggio ricevuto a tutti i client
for (int i = 0 ; i < server.out.size() ; i++)
{
((DataOutputStream)(server.out.elementAt(i))).writeBytes(str); //server è il
nome di questo file
mioserver.scrivimessaggio(str);
}
}
} catch (IOException ioe) {System.out.println("errore "+ioe);}
}
}







Leave a reply