Como rodar o DeitelMessenger?

4 respostas
L

Baixei os arquivos do http://www.inf.ufsc.br/~bosco/downloads/Java%206%20CD/ch24/DeitelMessengerCaseStudy/
e implementei o DeitelMessenger

Já está tudo certinho, sem nenhum erro, mas como eu faço pra rodar ele no eclipse?
Onde eu tenho que ir?

Abraços

4 Respostas

L

Estou com vários códigos de instant messenger, mas não sei como roda nenhum deles =x

Alguem dá uma luz please

botocudo_killer

consegue postar o codigo inteiro aqui ou zipar ele?

L

Um exemplo de instant messenger que encontrei
Como faço para rodar?
Quando vou em Run As tem escrito “Select Type(? = any character, *= any string,TZ=timeZone”
e em baixo o Servidor e o StressClient
o que isso significa?

import java.net.;
import java.io.
;

public class Servidor implements Runnable

{

ServerSocket ss;
public Servidor(int porta) throws Exception{  
      ss = new ServerSocket(porta);  

      new Thread(this).start();  

      System.out.println("Servidor ouvindo na porta:" + porta);  

    }  

 public void run(){  
      try{  
           while(true){  
                new TrataCliente(ss.accept()).start();  
                System.out.println("Mais um cliente atendido!");  

           }  

      }catch(Exception e){  
           e.printStackTrace();  
           System.exit(1);  
      }  
 }  
 public static void main(String[] args){  
      try{  
           new Servidor(1000);  

      }catch(Exception e){  
           e.printStackTrace();  
           System.exit(1);  
      }  
 }

}


import java.net.;
import java.io.
;

public class StressCliente extends Thread

{

private String server;

private int porta;
public StressCliente(String server, int porta){  
     this.server = server;  
     this.porta = porta;  
 }  

 public static void main(String[] args){  
      try{  
           String server = "localhost";  
           int porta = 1000;  
           int numeroDeClientes = 10;  

           for(int i=0; i < numeroDeClientes; i++){  

               new StressCliente(server, porta).start();  

        }  

      }catch(Exception e){  
           e.printStackTrace();  
      }  
 }  


 public void run(){  
      try{  

               while(true){  

                     Socket s = new Socket(server, porta);  

                     System.out.println("Conectado a " + server + ":" + porta);  

                     ObjectOutputStream oo = new ObjectOutputStream(s.getOutputStream());  

                     oo.writeObject("Soh!");  

                     s.close();  
           }  

      }catch(Exception e){  
           e.printStackTrace();  
      }  
 }

}


import java.net.;
import java.io.
;

class TrataCliente extends Thread

{

private Socket client;
public TrataCliente(Socket s){  
      client = s;  
 }  

 public void run(){  
      try{  
           // aqui vai a sua comunicacao com o cliente  
           ObjectInputStream oi = new ObjectInputStream(client.getInputStream());  

           System.out.println("Chegou isso:" + oi.readObject());  

           client.close();
      

      }catch(Exception e){  
           e.printStackTrace();  
           System.exit(1);  
      }  
 }

}

L

Também tem esse, ele até abre um frame:

/**

  • ChatApplet.java 1.00 96/11/01 Merlin Hughes
  • Copyright © 1996 Prominence Dot Com, Inc. All Rights Reserved.
  • Permission to use, copy, modify, and distribute this software
  • for non-commercial purposes and without fee is hereby granted
  • provided that this copyright notice appears in all copies.
  • http://prominence.com/ [email removido]
    */
import <a href="http://java.net">java.net</a>.<em>;

import <a href="http://java.io">java.io</a>.</em>;

import java.awt.<em>;

import java.applet.</em>;

// Applet parameters:
// host = host name
// port = host port

public class ChatApplet extends Applet implements Runnable {

protected DataInputStream i;

protected DataOutputStream o;

protected TextArea output;
protected TextField input;

protected Thread listener;

public void init () {

setLayout (new BorderLayout ());

add (Center, output = new TextArea ());

output.setEditable (false);

add (South, input = new TextField ());

input.setEditable (false);

}
public void start () {

listener = new Thread (this);

listener.start ();

}
public void stop () {

if (listener != null)

listener.stop ();

listener = null;

}
public void run () {

try {

String host = getParameter (host);

if (host == null)

host = getCodeBase ().getHost ();

String port = getParameter (port);

if (port == null)

port = 9830;

output.appendText (Connecting to " + host + “:” + port + “…”);

Socket s = new Socket (host, Integer.parseInt (port));

i = new DataInputStream (new BufferedInputStream (s.getInputStream ()));

o = new DataOutputStream (new BufferedOutputStream (s.getOutputStream ()));

output.appendText (” connected.\n");

input.setEditable (true);

input.requestFocus ();

execute ();

} catch (IOException ex) {

ByteArrayOutputStream out = new ByteArrayOutputStream ();

ex.printStackTrace (new PrintStream (out));

output.appendText ("\n" + out);

}

}
public void execute () {

try {

while (true) {

String line = i.readUTF ();

output.appendText (line + “\n);

}

} catch (IOException ex) {

ByteArrayOutputStream out = new ByteArrayOutputStream ();

ex.printStackTrace (new PrintStream (out));

output.appendText (out.toString ());

} finally {

listener = null;

input.hide ();

validate ();

try {

o.close ();

} catch (IOException ex) {

ex.printStackTrace ();

}

}

}
public boolean handleEvent (Event e) {

if ((e.target == input) && (e.id == Event.ACTION_EVENT)) {

try {

o.writeUTF ((String) e.arg);

o.flush ();

} catch (IOException ex) {

ex.printStackTrace();

listener.stop ();

}

input.setText ("");

return true;

} else if ((e.target == this) && (e.id == Event.WINDOW_DESTROY)) {

if (listener != null)

listener.stop ();

hide ();

return true;

}

return super.handleEvent (e);

}

}

import <a href="http://java.net">java.net</a>.<em>;

import <a href="http://java.io">java.io</a>.</em>;

import java.awt.*;

public class ChatClient extends Frame implements Runnable {

// public ChatClient (String title, InputStream i, OutputStream o) …

// public void run () …

// public boolean handleEvent (Event e) …

// public static void main (String args[]) throws IOException …

protected DataInputStream i;

protected DataOutputStream o;

protected TextArea output;

protected TextField input;

protected Thread listener;

public ChatClient (String title, InputStream i, OutputStream o) {

super (title);

this.i = new DataInputStream (new BufferedInputStream (i));

this.o = new DataOutputStream (new BufferedOutputStream (o));

setLayout (new BorderLayout ());

add (Center, output = new TextArea ());

output.setEditable (false);

add (South, input = new TextField ());

pack ();

show ();

input.requestFocus ();

listener = new Thread (this);

listener.start ();

}

public void run () {

try {

while (true) {

String line = i.readUTF ();

output.appendText (line + “\n);

}

} catch (IOException ex) {

ex.printStackTrace ();

} finally {

listener = null;

input.hide ();

validate ();

try {

o.close ();

} catch (IOException ex) {

ex.printStackTrace ();

}

}

}

public boolean handleEvent (Event e) {

if ((e.target == input) && (e.id == Event.ACTION_EVENT)) {

try {

o.writeUTF ((String) e.arg);

o.flush ();

} catch (IOException ex) {

ex.printStackTrace();

listener.stop ();

}

input.setText ("");

return true;

} else if ((e.target == this) && (e.id == Event.WINDOW_DESTROY)) {

if (listener != null)

listener.stop ();

hide ();

return true;

}

return super.handleEvent (e);

}

public static void main (String args[]) throws IOException {

if (args.length != 2)

throw new RuntimeException ("Syntax: ChatClient  ");

Socket s = new Socket (args[0], Integer.parseInt (args[1]));

new ChatClient ("Chat " + args[0] + : + args[1],

s.getInputStream (), s.getOutputStream ());

}

}

import <a href="http://java.net">java.net</a>.<em>;

import <a href="http://java.io">java.io</a>.</em>;

import java.util.*;
public class ChatHandler extends Thread {

// public ChatHandler (Socket s) throws IOException 

// public void run () 

protected Socket s;

protected DataInputStream i;

protected DataOutputStream o;
public ChatHandler(Socket s) throws IOException {
	this.s = s;
	i = new DataInputStream(new BufferedInputStream(s.getInputStream()));
	o = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
}
protected static Vector handlers = new Vector ();
  public void run () {
    try {
      handlers.addElement (this);
      while (true) {
        String msg = i.readUTF ();
        broadcast (msg);
      }
    } catch (IOException ex) {
      ex.printStackTrace ();
    } finally {
      handlers.removeElement (this);
      try {
        s.close ();
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }
  // protected static void broadcast (String message) ...
  protected static void broadcast (String message) {
	    synchronized (handlers) {
	      Enumeration e = handlers.elements ();
	      while (e.hasMoreElements ()) {
	        ChatHandler c = (ChatHandler) e.nextElement ();
	        try {
	          synchronized (c.o) {
	            c.o.writeUTF (message);
	          }
	          c.o.flush ();
	        } catch (IOException ex) {
	          c.stop ();
	        }
	      }
	    }
	  }

}


import <a href="http://java.net">java.net</a>.<em>;

import <a href="http://java.io">java.io</a>.</em>;

import java.util.*;

public class ChatServer {

// public ChatServer (int port) throws IOException …

// public static void main (String args[]) throws IOException …

public ChatServer (int port) throws IOException {

ServerSocket server = new ServerSocket (port);

while (true) {

Socket client = server.accept ();

System.out.println ("Accepted from " + client.getInetAddress ());

ChatHandler c = new ChatHandler (client);

c.start ();

}

}

public static void main (String args[]) throws IOException {

if (args.length != 1)

throw new RuntimeException ("Syntax: ChatServer ");

new ChatServer (Integer.parseInt (args[0]));

}

}
Criado 29 de maio de 2012
Ultima resposta 30 de mai. de 2012
Respostas 4
Participantes 2