Ist noch eine Ur-Version..aber die reicht um das Prinzip zu verstehen.
Falls es wer brauchen kann.
InitClient.java
Java-Quellcode
- package client;
- import java.awt.*;
- import java.awt.event.*;
- import java.net.*;
- /**
- * <p>Überschrift: </p>
- * <p>Beschreibung: </p>
- * <p>Copyright: Copyright (c) 2003</p>
- * <p>Organisation: </p>
- * @author unbekannt
- * @version 1.0
- */
- public class InitClient extends Frame implements ActionListener{
- public Label lbl_ip;
- public TextField ip1,ip2,ip3,ip4;
- public Label lbl_port;
- public TextField port;
- public Button start;
- public InitClient() {
- this.setLayout(new FlowLayout());
- this.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e)
- {
- dispose();
- System.exit(0);
- }});
- lbl_port = new Label("Portnummer:");
- port = new TextField(5);
- lbl_ip = new Label(" IP-Nummer:");
- ip1 = new TextField(3);
- ip2 = new TextField(3);
- ip3 = new TextField(3);
- ip4 = new TextField(3);
- start = new Button("Start");
- this.setTitle("Client");
- this.add(lbl_port);
- this.add(port);
- this.add(lbl_ip);
- this.add(ip1);
- this.add(ip2);
- this.add(ip3);
- this.add(ip4);
- this.add(start);
- this.pack();
- this.setResizable(false);
- start.addActionListener(this);
- this.setVisible(true);
- }
- public void actionPerformed(ActionEvent e)
- {
- String ip = new String();
- int prt=0;
- try
- {
- prt = Integer.parseInt(port.getText());
- if (!(prt < 32768 && prt > 128))
- {
- System.out.println("Port muss einen Zahl < 32768 und > 128");
- System.exit(1);
- }
- }
- catch(NumberFormatException nfe)
- {
- System.out.println("Port muss einen Zahl < 32768 und > 128");
- System.exit(1);
- }
- if(isOk() == false)
- {
- System.out.println("IP nicht korrekt");
- System.exit(0);
- }
- ip = ip1.getText() + "." + ip2.getText() + "." + ip3.getText() + "." + ip4.getText();
- ChatClient cc = new ChatClient(ip,prt);
- }
- public static void main(String[] args)
- {
- InitClient ic = new InitClient();
- }
- // Überprüft ob IP korrekt ist
- public boolean isOk()
- {
- if (Integer.parseInt(ip1.getText()) < 255 && Integer.parseInt(ip2.getText()) < 255 && Integer.parseInt(ip3.getText()) < 255 && Integer.parseInt(ip4.getText()) < 255)
- return true;
- else return false;
- }
- }
ChatClient.java
Java-Quellcode
- package client;
- import java.awt.*;
- import java.awt.event.*;
- import java.net.*;
- import java.io.*;
- public class ChatClient extends Frame implements ActionListener, Runnable
- {
- public TextArea oben;
- public TextField unten;
- public Socket s;
- public DataInputStream din;
- public DataOutputStream dout;
- public ChatClient(String ip, int port)
- {
- this.setLayout(new BorderLayout(1,2));
- this.setSize(640,480);
- this.setResizable(false);
- this.setTitle("Client");
- this.setBackground(Color.gray);
- oben = new TextArea();
- unten = new TextField(255);
- this.add(oben,BorderLayout.CENTER);
- this.add(unten,BorderLayout.SOUTH);
- this.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- send("quit");
- dispose();
- System.exit(0);}});
- unten.addActionListener(this);
- this.setVisible(true);
- // Socket erstellen
- try
- {
- s = new Socket(ip,port);
- din = new DataInputStream(s.getInputStream());
- dout = new DataOutputStream(s.getOutputStream());
- }
- catch(UnknownHostException uhe)
- {
- System.out.println("unkown");
- }
- catch(IOException i)
- {
- System.out.println("Initialisierungsfehler: " + i.getMessage());
- }
- // auf Message warten
- Thread th = new Thread(this);
- th.start();
- }
- public void actionPerformed(ActionEvent e)
- {
- oben.append("\n" + unten.getText());
- System.out.println(send(unten.getText()));
- unten.setText("");
- }
- public int send(String msg)
- {
- try
- {
- if (msg == null) return 0;
- dout.writeUTF(msg);
- // dout.close();
- }
- catch(IOException io)
- {
- System.out.println("Sendefehler: " + io.getMessage());
- return -1;
- }
- return 1;
- }
- public void run()
- {
- String msg = new String("");
- while(!msg.equals("quit"))
- {
- try
- {
- msg = din.readUTF();
- if (msg != null) oben.append("\n" + msg);
- }
- catch (IOException io)
- {
- System.out.println("Empfangsfehler: " + io.getMessage());
- }
- }
- }
- }