Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
import java.net.*;
import java.io.*;
import java.awt.*;
import java.applet.*;
public class chatapplet extends Frame implements Runnable
{
public static final int PORT = 8765;
String ip = 192.168.0.2 //Diese ip adrresse einfach durch die adresse des servers ersetzen
Socket socket;
DataInputStream in;
PrintStream out;
TextField inputfield;
TextArea outputarea;
Thread thread;
public void init()
{
inputfield = new TextField();
outputarea = new TextArea();
outputarea.setFont( new Font("Dialog", Font.PLAIN, 12));
outputarea.setEditable(false);
this.setSize(500,500);
this.setLayout(new BorderLayout());
this.add("South", inputfield);
this.add("Center", outputarea);
this.setBackground(Color.lightGray);
this.setForeground(Color.black);
inputfield.setBackground(Color.white);
outputarea.setBackground(Color.white);
this.setVisible(true);
}
public void start()
{
try
{
socket = new Socket(ip, PORT);
in = new DataInputStream(socket.getInputStream());
out = new PrintStream(socket.getOutputStream());
} catch (IOException e)
{
this.showStatus(e.toString());
say("Verbindung zum Server fehlgeschlagen!");
System.exit(1);
}
say("Verbindung zum Server aufgenommen...");
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) e.arg;
out.println(inp);
inputfield.setText("");
return true;
}
return false;
}
public void say(String msg)
{
outputarea.appendText("*** "+msg+" ***\n");
}
}
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ChatClient extends Frame implements Runnable,ActionListener{
public static final int port = 1024;
Socket socket;
DataInputStream in;
PrintStream out;
TextField inputfield;
TextArea outputarea;
Thread thread;
String ip="192.168.0.23";
ChatClient c;
Button b;
public static void main(String[] args) {
ChatClient c = new ChatClient();
c.init();
c.start();
}
public void init(){
inputfield = new TextField();
outputarea = new TextArea();
b = new Button("senden");
outputarea.setFont(new Font("Dialog",Font.PLAIN, 12));
outputarea.setEditable(false);
this.setSize(500,500);
this.setLayout(new BorderLayout());
this.add("South",inputfield);
this.add("North",outputarea);
this.add("East",b);
b.addActionListener(this);
this.setBackground(Color.lightGray);
this.setForeground(Color.black);
inputfield.setBackground(Color.white);
outputarea.setBackground(Color.white);
this.setVisible(true);
}
public void start() {
try {
socket = new Socket(ip, port);
in = new DataInputStream(socket.getInputStream());
out = new PrintStream(socket.getOutputStream());
}
catch(IOException e) {
say("Verbindung fehlgeschlagen:"+e);
System.out.println("Fehler:"+e);
System.exit(1);
}
say("Verbindung erfolgreich");
if(thread==null){
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void stop() {
try {
socket.close();
}
catch(IOException e) {
}
if (thread!=null && thread.isAlive()) {
thread.stop();
thread = null;
}
}
public void run() {
String line;
try {
while(true) {
line = in.readLine();
System.out.println(line);
if(line!=null) {
outputarea.appendText(line+'\n');
}
}
}
catch(IOException e) {
say("Verbindung zum Server abgebrochen");
}
}
public void say(String msg) {
outputarea.appendText("***"+msg+"***\n");
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b) {
String inp = (String) inputfield.getText();
System.out.println("input:"+inp);
out.println(inp);
inputfield.setText("");
// return true;
}
// return false;
}
}
import java.util.*;
import java.io.IOException;
import java.net.*;
public class Chatserver implements Runnable{
public int port = 1024;
ServerSocket listen;
Vector connections;
Thread connect;
public static void main(String[] args) {
new Chatserver();
}
public Chatserver() {
try {
listen = new ServerSocket(port);
}
catch (Exception e) {
System.err.println("Fehler beim erzeugen des 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 Verb:"+e);
System.exit(1);
}
}
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);
System.out.println(msg);
}
}
}
import java.net.*;
import java.io.*;
public class Connection extends Thread{
public Socket client;
public DataInputStream in;
public PrintStream out;
public 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();
System.out.println(line);
if (line==null) {
server.broadcast(line);
}
}
}
catch (IOException e){
System.err.println("Fehler:"+e);
}
}
}