Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Aug 2001
    Messaggi
    132

    Java: chat client-server

    Qualcuno potrebbe indicarmi come funziona una chat client-server in Java? Come ci si comporta per l'installazione del server? Ossia semplicemente cosa bisogna fare e quali sono gli accorgimenti e le informazioni iniziali da non trascurare?
    Grazie e ciao a tutti.
    Miriamm

  2. #2
    Forse ti puo' essere utile questo esempio che ho trovato in rete:
    (http://www.planetsourcecode.com)
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;


    public class server extends Frame {
    public static TextArea display;
    public static server a;
    public static int portNumber, maxClients;


    public static void main (String [] args) {
    BufferedReader stdin = new BufferedReader
    (new InputStreamReader (System.in));


    try {
    System.out.print("Give the port number to listen for requests: ");
    System.out.flush();
    portNumber = Integer.parseInt(stdin.readLine());
    System.out.print("Give the maximum number of clients for this server: ");
    System.out.flush();
    maxClients = Integer.parseInt(stdin.readLine());
    } catch (IOException ioe) {System.out.println(ioe);}
    a = new server();
    a.show();
    a.startServer();
    }
    public ServerSocket connect;
    public Vector sockets;
    public TextField input;


    public server () {
    setSize(780,550);
    setTitle("server");
    setLayout(null);
    addWindowListener(new closeListener());
    display = new TextArea();
    display.setEditable(false);
    input = new TextField();
    input.addActionListener(new al());
    add(input);
    add(display);
    display.setBounds(10,30,760,489);
    input.setBounds(10,520,760,20);
    sockets = new Vector();


    try {
    connect = new ServerSocket(portNumber,maxClients);
    } catch (IOException ioe) {System.out.println(ioe);}
    }


    public void startServer () {


    try {


    while (true) {
    sockets.addElement(new ThreadedSocket(connect.accept()));
    Thread.yield();
    }
    } catch (Exception e) {System.out.println(e);}
    }


    public static synchronized void echoToAllClients (String s) {


    try {


    for (int i=0;i<A.sockets.size();i++) {
    ThreadedSocket temp = (ThreadedSocket)a.sockets.elementAt(i);
    temp.out.writeObject(s);
    }
    } catch (Exception e) {}
    }


    private class closeListener extends WindowAdapter {


    public void windowClosing (WindowEvent e) {
    System.exit(0);
    }
    }


    private class al implements ActionListener {


    public void actionPerformed (ActionEvent e) {
    String message = "<Server> "+input.getText();
    input.setText("");
    display.append(message+"\n");
    echoToAllClients(message);
    }
    }
    }


    class ThreadedSocket extends Thread {
    public Socket socket;
    public ObjectInputStream in;
    public ObjectOutputStream out;
    public String nick;


    public ThreadedSocket (Socket s) {
    socket = s;


    try {
    out = new ObjectOutputStream(socket.getOutputStream());
    out.flush();
    in = new ObjectInputStream(socket.getInputStream());
    } catch (Exception e) {System.out.println(e);}
    nick = "";
    start();
    }


    public void run () {
    String message = "";
    int flag = 0;


    while (!message.endsWith("quit")) {


    try {
    message = (String)in.readObject();
    } catch (Exception e) {System.out.println(e);}


    if (flag == 0) {
    nick = message;
    server.display.append("* "+nick+" ["+socket.getInetAddress().getHostName()+"] has joined the server *\n");
    server.echoToAllClients("* "+nick+" ["+socket.getInetAddress().getHostName()+"] has joined the server *");
    server.display.append("* "+nick+" welcome to Ezad's Chat Server (c) 2000-01 Ali Ezad Abbas *\n");
    server.echoToAllClients("* "+nick+" welcome to Ezad's Chat Server (c) 2000-01 Ali Ezad Abbas *");
    flag = 1;


    } else {
    server.display.append(message+"\n");
    server.echoToAllClients(message);
    }
    Thread.yield();
    }


    try{
    server.echoToAllClients("* Connection Closed by "+nick+" *");
    in.close();
    out.close();
    socket.close();
    server.display.append("* Connection Closed by "+nick+" *\n");
    } catch (Exception e) {System.out.println(e);}
    }
    }



  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2001
    Messaggi
    132
    Avendo già client e server, come ci si deve comportare per l'installazione?
    Grazie.
    Miriamm

  4. #4
    Utente di HTML.it L'avatar di Fool
    Registrato dal
    May 2002
    Messaggi
    8,342
    interessa anche a me

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 © 2024 vBulletin Solutions, Inc. All rights reserved.