Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2007
    Messaggi
    115

    far partire il client di una chat java

    Salve a tutti ragazzi..ho bisogno di un aiutino...in pratica ho scaricato una chat in java composto tra 3 classi: chatServer, chatClient e FinestraChat. Ora il server ha il main e non ho problemi a lanciarlo (lo lancio con Jcreator o da dos). Lanciato il server il mio problema è lanciare il client con l'interfaccia grafica... lanciadolo da telnet con dos va...pero senza interfaccia naturalmente...con Jcreator mi dice invece che manca il main... come si puo fare...ne ho scaricato 2 chat con la stessa strategia...il client è senza main...mi dite una soluzione...inserisco qui il codice: ( non so se e il giusto modo di inserirlo in caso avvisatemi che modifico):

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.net.*;
    import java.io.*;

    public class ChatClient extends Applet {
    boolean isStandalone = false;
    Button bConferma = new Button();
    Label lMessaggio = new Label();
    Label label2 = new Label();
    TextField tUserid = new TextField();
    FinestraChat chat;
    Socket t;
    static DataInputStream is;
    static DataOutputStream os;

    public void init() {
    try {
    jbInit();
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }

    private void jbInit() throws Exception {
    this.setBackground(Color.blue);
    this.setSize(new Dimension(445, 197));
    bConferma.setActionCommand("");
    bConferma.setFont(new Font("TimesRoman", 1, 14));
    bConferma.setLabel("Conferma");
    bConferma.setBounds(new Rectangle(168, 106, 108, 38));
    bConferma.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    bConferma_actionPerformed(e);
    }
    });
    lMessaggio.setBackground(Color.blue);
    lMessaggio.setForeground(Color.red);
    lMessaggio.setFont(new Font("Dialog", 3, 12));
    lMessaggio.setBounds(new Rectangle(6, 165, 428, 29));
    label2.setText("Userid :");
    tUserid.setBackground(Color.white);
    tUserid.setBounds(new Rectangle(100, 34, 302, 28));
    label2.setBounds(new Rectangle(34, 33, 56, 29));
    label2.setFont(new Font("Dialog", 1, 14));
    label2.setForeground(Color.black);
    label2.setBackground(Color.blue);
    this.setLayout(null);
    this.add(bConferma, null);
    this.add(label2, null);
    this.add(lMessaggio, null);
    this.add(tUserid, null);
    tUserid.requestFocus();
    }

    void bConferma_actionPerformed(ActionEvent e) {
    if (tUserid.getText().trim().equalsIgnoreCase("")) {
    lMessaggio.setText("Digitare una userid");
    tUserid.requestFocus();
    return;
    }
    lMessaggio.setText("");
    try
    {
    t = new Socket("127.0.0.1",1111);
    is = new DataInputStream(t.getInputStream());
    os = new DataOutputStream(t.getOutputStream());
    os.writeBytes(tUserid.getText()+"\n");
    boolean more = true;
    chat = new FinestraChat("Finestra di discussione di "+tUserid.getText());
    chat.setVisible(true);
    chat.addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    chat_windowClosing(e);
    }
    });
    bConferma.setEnabled(false);
    }
    catch (ConnectException ce)
    {lMessaggio.setText("Non c'e' alcun server in attesa");}

    catch(IOException ioe)
    { System.out.println("Error " + ioe); }
    }

    void chat_windowClosing(WindowEvent e) {
    chat.setVisible(false);
    bConferma.setEnabled(true);
    try {
    t.close();
    } catch(IOException ioe)
    { System.out.println("Error " + ioe); }
    tUserid.requestFocus();
    }
    }













    import java.io.*;
    import java.net.*;
    import java.util.Vector;

    public class ChatServer {

    static Vector out = new Vector();
    static Vector userid = new Vector();

    public static void main(String[] args )
    {
    try
    {
    ServerSocket s = new ServerSocket(1111);
    System.out.println( "Server in attesa di amici sulla porta 1111" );

    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 {
    System.out.println( "E' entrato il nuovo amico "+str);
    userid.addElement(str);
    (new ThreadServer(chatIn,chatOut,str)).start();
    }
    } catch (Exception e)
    { System.out.println(e);
    }
    }
    s.close();
    }
    catch (Exception e)
    { System.out.println(e);
    }
    }
    }

    class ThreadServer extends Thread {
    DataInputStream chatIn;
    DataOutputStream chatOut;
    String chatUser;

    ThreadServer(DataInputStream chatIn, DataOutputStream chatOut, String chatUser) {
    this.chatIn = chatIn;
    this.chatOut = chatOut;
    this.chatUser = chatUser;
    }

    public void run() {
    try {
    // avvisa tutti della nuova userid
    for (int i = 0 ; i < ChatServer.out.size() ; i++) {
    ((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("U");
    for (int j = 0 ; j < ChatServer.userid.size() ; j++)
    ((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("<"+(String)ChatServer.userid.elementAt (j)+">");
    ((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("\n");
    ((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("T***** Entra "+chatUser+"\n");
    }
    while (true) {
    String str = chatIn.readLine();
    if (str == null) break;
    // avvisa tutti del nuovo messaggio
    for (int i = 0 ; i < ChatServer.out.size() ; i++) {
    ((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("T<"+chatUser+">"
    +str+"\n");
    }
    }
    } catch (IOException ioe) {System.out.println("errore "+ioe);}

    for (int i = 0 ; i < ChatServer.out.size() ; i++) {
    if (((String)(ChatServer.userid.elementAt(i))).equals IgnoreCase(chatUser)) {
    ChatServer.userid.removeElementAt(i);
    ChatServer.out.removeElementAt(i);
    break;
    }
    }
    try {
    // avvisa tutti del nuovo elenco di userid
    for (int i = 0 ; i < ChatServer.out.size() ; i++) {
    ((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("U");
    for (int j = 0 ; j < ChatServer.userid.size() ; j++)
    ((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("<"+(String)ChatServer.userid.elementAt (j)+">");
    ((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("\n");
    }
    for (int i = 0 ; i < ChatServer.out.size() ; i++) {
    ((DataOutputStream)(ChatServer.out.elementAt(i))). writeBytes("T***** Esce "+chatUser+"\n");
    }
    } catch (IOException ioe) {System.out.println("errore "+ioe);}
    super.run();
    }
    }


    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.StringTokenizer;

    public class FinestraChat extends Frame {
    TextArea taNomi = new TextArea();
    TextField tMessaggio = new TextField();
    TextArea taChat = new TextArea();
    Button button1 = new Button();

    public FinestraChat(String titolo) {
    this();
    setTitle(titolo);
    }

    public FinestraChat() {
    try {
    jbInit();
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }

    private void jbInit() throws Exception {
    this.setBackground(new Color(0, 192, 0));
    this.setSize(new Dimension(571, 535));
    this.setTitle("Finestra di discussione");
    this.setLayout(null);
    taNomi.setBackground(Color.white);
    taNomi.setFont(new Font("TimesRoman", 0, 14));
    taNomi.setBounds(new Rectangle(453, 29, 107, 400));
    tMessaggio.setBackground(Color.white);
    tMessaggio.setBounds(new Rectangle(18, 438, 533, 33));
    taChat.setBackground(Color.white);
    taChat.setFont(new Font("TimesRoman", 1, 14));
    taChat.setBounds(new Rectangle(11, 28, 437, 401));
    button1.setFont(new Font("TimesRoman", 1, 14));
    button1.setBounds(new Rectangle(211, 473, 115, 43));
    button1.setLabel("Invia");
    button1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    button1_actionPerformed(e);
    }
    });
    this.add(taNomi, null);
    this.add(tMessaggio, null);
    this.add(taChat, null);
    this.add(button1, null);
    (new ThreadClient(this)).start();
    tMessaggio.requestFocus();
    }

    void button1_actionPerformed(ActionEvent e) {
    try {
    if (!tMessaggio.getText().trim().equalsIgnoreCase("") ) {
    ChatClient.os.writeBytes(tMessaggio.getText()+"\n" );
    tMessaggio.setText("");
    }
    } catch (IOException ioe) {System.out.println("Errore = "+ioe);}
    tMessaggio.requestFocus();
    }
    }

    class ThreadClient extends Thread {
    FinestraChat fc;

    ThreadClient(FinestraChat fc) {
    this.fc = fc;
    }

    public void run() {
    try {
    while (true) {
    String str = ChatClient.is.readLine();
    if (str == null) break;
    if (str.substring(0,1).equalsIgnoreCase("U")) {
    fc.taNomi.setText("");
    StringTokenizer toke = new StringTokenizer(str.substring(1),">");
    while (toke.hasMoreElements()) {
    fc.taNomi.setText(fc.taNomi.getText()+toke.nextTok en()+">"+"\n");
    }
    }
    else {
    fc.taChat.setText(fc.taChat.getText()+str.substrin g(1)+"\n");
    }
    }
    } catch (IOException ioe) {}
    super.run();
    }
    }

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013

    Re: far partire il client di una chat java

    Originariamente inviato da cobraa
    Salve a tutti ragazzi..ho bisogno di un aiutino...in pratica ho scaricato una chat in java composto tra 3 classi: chatServer, chatClient e FinestraChat. Ora il server ha il main e non ho problemi a lanciarlo (lo lancio con Jcreator o da dos). Lanciato il server il mio problema è lanciare il client con l'interfaccia grafica... lanciadolo da telnet con dos va...pero senza interfaccia naturalmente...con Jcreator mi dice invece che manca il main... come si puo fare...ne ho scaricato 2 chat con la stessa strategia...il client è senza main...mi dite una soluzione...inserisco qui il codice: ( non so se e il giusto modo di inserirlo in caso avvisatemi che modifico):

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.net.*;
    import java.io.*;

    public class ChatClient extends Applet {
    ...
    Nel post che ti è stato chiuso ti avevo detto che sicuramente si trattava di un applet. Non sto nemmeno a leggere il codice perché onestamente non ne ho voglia (e poi se l'hai scaricato da qualche parte, sicuramente ci sarà anche un minimo di spiegazione su come lanciarla ed utilizzarla...). Ad ogni modo avrai bisogno di una pagina web con del codice tipo

    codice:
    <html>
    <body>
    <applet code="ChatClient" width="600" height="400">
      <param name="..." value="..." />
      <param name="..." value="..." />
      <param name="..." value="..." />
    </applet>
    </body>
    </html>
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    Utente di HTML.it
    Registrato dal
    Sep 2007
    Messaggi
    115
    Grazie...ho risolto...creando una semplice pagina html sono riuscito a far partire il tutto...molto gentile a presto!!!

  4. #4
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,328

    Moderazione

    La prossima volta cerca di postare il codice all'interno dei tag [*CODE] e [*/CODE] (senza l'asterisco rosso), in modo che rimanga ben indentato e venga visualizzato in modo da facilitarne la lettura.


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.