⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servidorchat.java

📁 Example mini chat developed in java.
💻 JAVA
字号:
/* * ServidorChat.java * * Created on 25 de abril de 2006, 10:55 */import java.net.*;import java.io.*;import java.util.*;import java.awt.*;import javax.swing.*;/** * * @author  TeimoyM */public class ServidorChat{    public static void main(String[] args)throws IOException, BindException    {        try        {            ServerSocket sservidor = new ServerSocket(9000);            Socket scliente = null;            while(true)            {                try                {                    scliente = sservidor.accept();                    new ThreadServidor(scliente).start();                }                catch(IOException e1)                {                    try                    {                        scliente.close();                    }                    catch(IOException e2)                    {                                            }                }            }                        }        catch(IOException e3)        {            JOptionPane.showMessageDialog(null,"Error Fatal"+"\n"+"No se puede arrancar el servidor.",            "Informacion para el usuario",JOptionPane.WARNING_MESSAGE);            System.exit(0);        }    }}    class ThreadServidor extends Thread{    private Socket s;    private BufferedReader entrada;    private PrintWriter salida;    private String usuario;    private static Vector clientesActivos = new Vector();        public ThreadServidor(Socket socket) throws IOException    {        s = socket;        salida = new PrintWriter(s.getOutputStream(),true);        entrada = new BufferedReader(new InputStreamReader(s.getInputStream()));        usuario = s.getInetAddress().getHostName() + ":" + s.getPort();        clientesActivos.addElement(this);        try        {            String historial="C:"+File.separatorChar+"historial.txt";            PrintWriter salidaArchivo = new PrintWriter(new BufferedWriter(new FileWriter(historial,true)));            salidaArchivo.println("Conexion desde la direccion: "+ s.getInetAddress().getHostName() +            " por el puerto "+s.getPort()+" en la fecha "+new Date());            salidaArchivo.close();        }        catch(IOException e2)        {            JOptionPane.showMessageDialog(null,"Fallo en el archivo de historial","Informacion para el usuario",JOptionPane.WARNING_MESSAGE);        }    }    private void escribir(String textoUsuario)    {        for(int i=0; i<clientesActivos.size(); i++)        {            System.out.println("Enviado a: "+i+textoUsuario);            ((ThreadServidor)(clientesActivos.elementAt(i))).salida.println(textoUsuario);        }    }    public void run()    {        String textoUsuario;        try        {            while((textoUsuario = entrada.readLine())!=null)                 escribir(usuario+">"+textoUsuario);                        }        catch(Exception e1)        {            if(e1.getMessage().equals("Connction reset by peer. JVM_recv"+            " in socket input stream read"))            {                try                {                    String historial = "C:"+File.separatorChar+"historial.txt";                    PrintWriter salidaArchivo = new PrintWriter(new BufferedWriter(new FileWriter(historial,true)));                    salidaArchivo.println("Desconexion desde direccion: "+ s.getInetAddress().getHostName() +                    " por el puerto "+s.getPort()+" en la fecha "+new Date());                    salidaArchivo.close();                }                catch(IOException e2)                {                    JOptionPane.showMessageDialog(null,"Fallo en archivo de historial", "Informacion para " +                    "el usuario",JOptionPane.WARNING_MESSAGE);                }            }                      }        finally        {            clientesActivos.removeElement(this);            try                            {               s.close();                               }            catch(Exception e3)            {                JOptionPane.showMessageDialog(null,"No se ha podido cerrar el socket",                "Informacion para el usuario",JOptionPane.WARNING_MESSAGE);            }        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -