JChat

Ja, wenn du mir bitte einen gefallen tun könntest.
Kannst du mir eine Klasse namens "Chat.java" schreiben, die diese 3 Klassen verwendet bitte, dann geb ich dir mehr als einen Renomee
 
Hi,
Kerl, war denn alles umsonst? Es kann keine Klasse Chat.java geben, da der Server(2 Klassen) und der Klient(1 Klasse) bis zu einem gewissen Grad unabhängig voneinerander sind, das heißt, dass es 2 verschiedene Programme sind. Wenn du jemanden haben willst, der den Code aus diesem PDF-Tutorial abschreibt, dann bitte ins Forum für unentgeltliche Stellenangebote posten, ich werde ganz sicher kein Code abschreiben; Ich will nur anderen helfen.
Ciao
DosCoder
 
dieser Chat, den du mir gezeigt hast, läuft bei mir auf Eclipse nicht. Egal ich hab nen anderen aber dazu brauch ich deine Hilfe. Also, Applets kann man nicht mit der Eingabeaufforderung starten, wenn ja wie?

Mein Code sieht wie folgt aus, 3 Klassen, ein Applet:
Code:
import java.net.*;
import java.io.*;


class connection extends Thread
{
	protected Socket client;
	protected DataInputStream in;
	protected PrintStream out;
	protected chatserver server;

	public connection(chatserver server, Socket client)
	{
		this.server=server;
		this.client=client;

		try
		{
			in = new DataInputStream(client.getInputStream());
			out = new PrintStream(client.getOutputStream());
		} catch (IOException e)
		{
			try { client.close(); } catch (IOException e2) {} ;
			System.err.println("Fehler beim Erzeugen der Streams: " + e);
			return;
		}

		this.start();
	}


	public void run()
	{
		String line;

		try
		{
			while(true)
			{
				line=in.readLine();
				if(line!=null)
					server.broadcast(line);
			}
		} catch (IOException e)
		{
			System.out.println("Fehler:" + e);
		}
	}
}
Code:
import java.net.*;
import java.io.*;
import java.util.*;


public class chatserver implements Runnable
{
	public static final int PORT = 1;
	protected ServerSocket listen;
	protected Vector connections;
	Thread connect;

	public chatserver()
	{
		try
		{
			listen = new ServerSocket(PORT);
		} catch (IOException e)
		{
			System.err.println("Fehler beim Erzeugen der Sockets:"+e);
			System.exit(1);
		}

		connections = new Vector();

		connect = new Thread(this);
		connect.start();
	}

	public void run()
	{
		try
		{
			while(true)
			{
				Socket client=listen.accept();

				connection c = new connection(this, client);
				connections.addElement(c);
			}
		} catch (IOException e)
		{
			System.err.println("Fehler beim Warten auf Verbindungen:"+e);
			System.exit(1);
		}
	}

	public static void main(String[] args)
	{
		new chatserver();
	}

	public void broadcast(String msg)
	{
		int i;
		connection you;

		for (i=0; i<connections.size(); i++)
		{
			you = (connection) connections.elementAt(i);
			you.out.println(msg);
		}
	}
}
Code:
import java.applet.Applet;
import java.applet.AppletContext;
import java.applet.AppletStub;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
 
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.AudioFormat.Encoding;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;

import com.sun.j3d.utils.applet.MainFrame;

public class JavaChat_1 extends Applet implements Runnable
{
	
		public static final int PORT = 1;
		Socket socket;
		DataInputStream in;
		PrintStream out;
		TextField inputfield;
		TextArea outputarea;
		Thread thread;
		 private static final long serialVersionUID = 4881953554409778241L;
		 private final HashMap<String, InputStream> appStreams = new HashMap<String, InputStream>();
		  private final HashMap<String, String> appParams = new HashMap<String, String>();

	String name;
	
	public JavaChat_1()
	{
		
		File f = new File("F:/FUTUR.PROGRAMS/Chat/Saves.txt");
		name = getContents(f);
		new chatserver();
		inputfield = new TextField();
		outputarea = new TextArea();
		outputarea.setFont( new Font("Dialog", Font.PLAIN, 12));
		outputarea.setEditable(false);
		
		this.setLayout(new BorderLayout());
		this.add("South", inputfield);
		this.add("Center", outputarea);
		
		this.setBackground(Color.cyan);
		this.setForeground(Color.cyan);
		inputfield.setBackground(Color.black);
		outputarea.setBackground(Color.black);		
		
		JMenuBar bar = new JMenuBar();bar.setBackground(Color.black);this.add(bar,BorderLayout.NORTH);
		
		JMenu m = new JMenu("Start");m.setForeground(Color.cyan);bar.add(m);
		
		JMenuItem i = new JMenuItem("Benutzternamen ändern");i.setForeground(Color.cyan);i.setBackground(Color.black);m.add(i);
		i.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				b1ActionPerformed(evt);
			}
			private void b1ActionPerformed(ActionEvent evt)
			{
				new MainFrame(new Correct(),200,250);
			}
	    });
		
				
		JMenuItem i1 = new JMenuItem("Beenden");i1.setForeground(Color.cyan);i1.setBackground(Color.black);m.add(i1);
		i1.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				b1ActionPerformed(evt);
			}
			private void b1ActionPerformed(ActionEvent evt)
			{
				System.exit(0);
			}
	    });
		
		JTextArea a = new JTextArea();
		a.setText(" Java_Chat_1.0");a.setEnabled(false); bar.add(a);a.setBackground(Color.black);
	
		JMenu me = new JMenu("Hilfe");
		me.setForeground(Color.cyan);
		bar.add(me);
		
		JMenuItem m1 = new JMenuItem("Hilfe aufrufen");
		m1.setBackground(Color.black); m1.setForeground(Color.cyan);
		me.add(m1);
		
		m1.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				b1ActionPerformed(evt);
			}
			private void b1ActionPerformed(ActionEvent evt)
			{
            new MainFrame (new Help(),200,250);			
            }
	    });
		
	}

	public void start()
	{
		try
		{
			socket = new Socket(this.getCodeBase().getHost(), PORT);
			in = new DataInputStream(socket.getInputStream());
			out = new PrintStream(socket.getOutputStream());
		} catch (IOException e)
		{
			this.showStatus(e.toString());
			say("Verbindung zum Server fehlgeschlagen!");
		}

		say(name+" ist der ChatSitzung im Port:"+PORT+" beigetreten.");

		if (thread == null)
		{
			thread = new Thread(this);
			thread.setPriority(Thread.MIN_PRIORITY);
			thread.start();
		}
	}


	public void stop()
	{
		try
		{
			socket.close();
		} catch (IOException e)
		{
			this.showStatus(e.toString());
		}

		if ((thread !=null) && thread.isAlive())
		{
			thread.stop();
			thread = null;
		}
	}


	public void run()
	{
		String line;

		try
		{
			while(true)
			{
				line = in.readLine();
				if(line!=null)
					outputarea.appendText(line+'\n' );
			}
		} catch (IOException e) { say("Verbindung zum Server abgebrochen"); }
	}
	
	public boolean action(Event e, Object what)
	{
		if (e.target==inputfield)
		{
			String inp=(String)name+": "+ e.arg;

			out.println(inp);
			inputfield.setText("");
			return true;
		}

		return false;
	}


	public void say(String msg)
	{
		outputarea.appendText("*** "+msg+" ***\n");
	}

	static public String getContents(File aFile) 
	 {
		    StringBuilder contents = new StringBuilder();  
		    try {
		      BufferedReader input =  new BufferedReader(new FileReader(aFile));
		      try {
		        String line = null; 
		        while (( line = input.readLine()) != null){
		          contents.append(line);
		        }
		      }
		      finally {
		        input.close();
		      }
		    }
		    catch (IOException ex){
		      ex.printStackTrace();
		    }
		    
		    return contents.toString();
		  }
	 public static void main(String ... args)
	  {
	    final Frame f = new Frame("Hybrid Application Demo");
	    final JavaChat_1 applet = new JavaChat_1();
	    final Enumeration<Applet> appletEnum = new Enumeration<Applet>()
	    {
	      public boolean hasMoreElements()
	      {
	        return false;
	      }
	 
	      public Applet nextElement()
	      {
	        return null;
	      }
	    };
	    final AppletContext context = new AppletContext()
	    {
	      public Applet getApplet(String name)
	      {
	        return null; // not in application mode
	      }
	 
	      public Enumeration<Applet> getApplets()
	      {
	        return appletEnum;
	      }
	 
	      public AudioClip getAudioClip(URL url)
	      {
	        try {
	          final Clip clip = AudioSystem.getClip();
	          AudioInputStream ais = AudioSystem.getAudioInputStream(url);
	          AudioFormat af = ais.getFormat();
	          if(Encoding.ALAW.equals(af.getEncoding())
	          || Encoding.ULAW.equals(af.getEncoding())) {
	            af = new AudioFormat(
	              Encoding.PCM_SIGNED,
	              af.getSampleRate(),
	              af.getSampleSizeInBits() * 2,
	              af.getChannels(),
	              af.getFrameSize() * 2,
	              af.getFrameRate(),
	              true
	              );
	            ais = AudioSystem.getAudioInputStream(af, ais);
	          }
	          clip.open(ais);
	          return new AudioClip()
	          {
	            public void loop()
	            {
	              clip.loop(-1);
	            }
	 
	            public void play()
	            {
	              clip.start();
	            }
	 
	            public void stop()
	            {
	              clip.stop();
	            }
	          };
	        } catch(LineUnavailableException e) {
	          e.printStackTrace();
	          return null;
	        } catch(IOException e) {
	          e.printStackTrace();
	          return null;
	        } catch (UnsupportedAudioFileException e) {
	          e.printStackTrace();
	          return null;
	        }
	      }
	 
	      public Image getImage(URL url)
	      {
	        return Toolkit.getDefaultToolkit().getImage(url);
	      }
	 
	      public InputStream getStream(String key)
	      {
	        return applet.appStreams.get(key);
	      }
	 
	      public Iterator<String> getStreamKeys()
	      {
	        return applet.appStreams.keySet().iterator();
	      }
	 
	      public void setStream(String key, InputStream stream)
	      throws IOException
	      {
	        if(key != null && key.length() > 0) {
	          applet.appStreams.put(key, stream);
	        }
	      }
	 
	      public void showDocument(URL url)
	      {
	        // no effect
	      }
	 
	      public void showDocument(URL url, String target)
	      {
	        // no effect
	      }
	 
	      public void showStatus(String status)
	      {
	        // no effect
	      }
	      
	    };
	    AppletStub stub = new AppletStub()
	    {
	      public void appletResize(int width, int height)
	      {
	      }
	 
	      public AppletContext getAppletContext()
	      {
	        return context;
	      }
	 
	      public URL getCodeBase()
	      {
	        URL rc = JavaChat_1.class.getResource("HybridApplication.class");
	        return rc;
	      }
	 
	      public URL getDocumentBase()
	      {
	        URL rc = JavaChat_1.class.getResource("HybridApplication.class");
	        if(rc.getProtocol().equalsIgnoreCase("jar")) {
	          try {
	            String tmp = rc.toString().substring(4, rc.toString().indexOf("!/"));
	            tmp = rc.toString().replaceAll("jar:", "").replaceAll("file:/", "file://");
	            tmp = tmp.substring(0, tmp.lastIndexOf("!/"));
	            rc = new URL(null, tmp);
	          } catch(MalformedURLException e) {
	            e.printStackTrace();
	            rc = null;
	          }
	        }
	        return rc;
	      }
	 
	      public String getParameter(String name)
	      {
	        return applet.appParams.get(name);
	      }
	 
	      public boolean isActive()
	      {
	        return f.isActive();
	      }
	      
	    };
	    applet.setStub(stub);
	    if(args != null && args.length > 0) {
	      String[] pars;
	      for(String arg : args) {
	        pars = arg.split("=");
	        if(pars.length == 1) pars = new String[] {pars[0], "true"};
	        applet.appParams.put(pars[0].toLowerCase(), pars[1]);
	      }
	    }
	    f.setLayout(new GridLayout(1, 1, 0, 0));
	    String arg;
	    int width, height;
	    try {
	      width = ((arg = applet.getParameter("width")) != null)? Integer.parseInt(arg) : 800;
	    } catch(NumberFormatException e) {
	      width = 800;
	    }
	    try {
	      height = ((arg = applet.getParameter("height")) != null)? Integer.parseInt(arg) : 600;
	    } catch(NumberFormatException e) {
	      height = 600;
	    }
	    Dimension d = new Dimension(width, height);
	    applet.setPreferredSize(d);
	    applet.setSize(d);
	    f.addWindowListener(new WindowListener()
	    {
	      public void windowActivated(WindowEvent e)
	      {
	        applet.start();
	      }
	 
	      public void windowClosed(WindowEvent e)
	      {
	        System.exit(0);
	      }
	 
	      public void windowClosing(WindowEvent e)
	      {
	        applet.stop();
	        applet.destroy();
	        f.dispose();
	      }
	 
	      public void windowDeactivated(WindowEvent e)
	      {
	        applet.stop();
	      }
	 
	      public void windowDeiconified(WindowEvent e)
	      {
	        applet.start();
	      }
	 
	      public void windowIconified(WindowEvent e)
	      {
	        applet.stop();
	      }
	 
	      public void windowOpened(WindowEvent e)
	      {
	      }
	    });
	    applet.init();
	    f.add(applet);
	    f.pack();
	    f.setVisible(true);
	  }

}
Ich ´weiß, die Klasse help brauchst du jetzt aber nicht,

kannst du mir sagen wie ich die Klasse Java_Chat_1 starten soll? per eingabeaufforderung?

Ich finds einfach net heraus

PS:
Kann das daran liegen, dass man applets nciht starten kann?
Per Eingabeaufforderung?
 
@Maik: Danke fürs Renommee, echt nett!

@Developer_Y:
Mit Eclipse kenne ich mich nicht aus: Aber: Google, dein Freund und Helfer: Hier ist die Lösung.

Wenn du das Applet unbedingt in der Konsole ausführen willst:
Im jdk-Verzeichnis "bin" gibt es ein Programm namens "appletviewer". Allerdings brauchst du dazu die HTML-Seite mit dem Applet. Da sich meine HTML-Kenntnisse aber auf ziehmlich geringen Nivaeu halten, kann ich dir in dieser Hinsicht nicht helfen:(. Aber man kann ja googlen :).

Beispiel:
Code:
DosCoder@linux-rdm0:~> appletviewer myApplet.html

Ciao
DosCoder
 
Zuletzt bearbeitet:

Neue Beiträge

Zurück