Java Chat via Socket (mom. nur 2 user)


  • TheDude
  • 1153 Aufrufe 2 Antworten

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Java Chat via Socket (mom. nur 2 user)

    Hab einen Chat für 2 User über Sockets gecodet.
    Ist noch eine Ur-Version..aber die reicht um das Prinzip zu verstehen.
    Falls es wer brauchen kann.


    InitClient.java

    Java-Quellcode

    1. package client;
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. import java.net.*;
    5. /**
    6. * <p>Überschrift: </p>
    7. * <p>Beschreibung: </p>
    8. * <p>Copyright: Copyright (c) 2003</p>
    9. * <p>Organisation: </p>
    10. * @author unbekannt
    11. * @version 1.0
    12. */
    13. public class InitClient extends Frame implements ActionListener{
    14. public Label lbl_ip;
    15. public TextField ip1,ip2,ip3,ip4;
    16. public Label lbl_port;
    17. public TextField port;
    18. public Button start;
    19. public InitClient() {
    20. this.setLayout(new FlowLayout());
    21. this.addWindowListener(new WindowAdapter(){
    22. public void windowClosing(WindowEvent e)
    23. {
    24. dispose();
    25. System.exit(0);
    26. }});
    27. lbl_port = new Label("Portnummer:");
    28. port = new TextField(5);
    29. lbl_ip = new Label(" IP-Nummer:");
    30. ip1 = new TextField(3);
    31. ip2 = new TextField(3);
    32. ip3 = new TextField(3);
    33. ip4 = new TextField(3);
    34. start = new Button("Start");
    35. this.setTitle("Client");
    36. this.add(lbl_port);
    37. this.add(port);
    38. this.add(lbl_ip);
    39. this.add(ip1);
    40. this.add(ip2);
    41. this.add(ip3);
    42. this.add(ip4);
    43. this.add(start);
    44. this.pack();
    45. this.setResizable(false);
    46. start.addActionListener(this);
    47. this.setVisible(true);
    48. }
    49. public void actionPerformed(ActionEvent e)
    50. {
    51. String ip = new String();
    52. int prt=0;
    53. try
    54. {
    55. prt = Integer.parseInt(port.getText());
    56. if (!(prt < 32768 && prt > 128))
    57. {
    58. System.out.println("Port muss einen Zahl < 32768 und > 128");
    59. System.exit(1);
    60. }
    61. }
    62. catch(NumberFormatException nfe)
    63. {
    64. System.out.println("Port muss einen Zahl < 32768 und > 128");
    65. System.exit(1);
    66. }
    67. if(isOk() == false)
    68. {
    69. System.out.println("IP nicht korrekt");
    70. System.exit(0);
    71. }
    72. ip = ip1.getText() + "." + ip2.getText() + "." + ip3.getText() + "." + ip4.getText();
    73. ChatClient cc = new ChatClient(ip,prt);
    74. }
    75. public static void main(String[] args)
    76. {
    77. InitClient ic = new InitClient();
    78. }
    79. // Überprüft ob IP korrekt ist
    80. public boolean isOk()
    81. {
    82. if (Integer.parseInt(ip1.getText()) < 255 && Integer.parseInt(ip2.getText()) < 255 && Integer.parseInt(ip3.getText()) < 255 && Integer.parseInt(ip4.getText()) < 255)
    83. return true;
    84. else return false;
    85. }
    86. }
    Alles anzeigen


    ChatClient.java

    Java-Quellcode

    1. package client;
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. import java.net.*;
    5. import java.io.*;
    6. public class ChatClient extends Frame implements ActionListener, Runnable
    7. {
    8. public TextArea oben;
    9. public TextField unten;
    10. public Socket s;
    11. public DataInputStream din;
    12. public DataOutputStream dout;
    13. public ChatClient(String ip, int port)
    14. {
    15. this.setLayout(new BorderLayout(1,2));
    16. this.setSize(640,480);
    17. this.setResizable(false);
    18. this.setTitle("Client");
    19. this.setBackground(Color.gray);
    20. oben = new TextArea();
    21. unten = new TextField(255);
    22. this.add(oben,BorderLayout.CENTER);
    23. this.add(unten,BorderLayout.SOUTH);
    24. this.addWindowListener(new WindowAdapter(){
    25. public void windowClosing(WindowEvent e){
    26. send("quit");
    27. dispose();
    28. System.exit(0);}});
    29. unten.addActionListener(this);
    30. this.setVisible(true);
    31. // Socket erstellen
    32. try
    33. {
    34. s = new Socket(ip,port);
    35. din = new DataInputStream(s.getInputStream());
    36. dout = new DataOutputStream(s.getOutputStream());
    37. }
    38. catch(UnknownHostException uhe)
    39. {
    40. System.out.println("unkown");
    41. }
    42. catch(IOException i)
    43. {
    44. System.out.println("Initialisierungsfehler: " + i.getMessage());
    45. }
    46. // auf Message warten
    47. Thread th = new Thread(this);
    48. th.start();
    49. }
    50. public void actionPerformed(ActionEvent e)
    51. {
    52. oben.append("\n" + unten.getText());
    53. System.out.println(send(unten.getText()));
    54. unten.setText("");
    55. }
    56. public int send(String msg)
    57. {
    58. try
    59. {
    60. if (msg == null) return 0;
    61. dout.writeUTF(msg);
    62. // dout.close();
    63. }
    64. catch(IOException io)
    65. {
    66. System.out.println("Sendefehler: " + io.getMessage());
    67. return -1;
    68. }
    69. return 1;
    70. }
    71. public void run()
    72. {
    73. String msg = new String("");
    74. while(!msg.equals("quit"))
    75. {
    76. try
    77. {
    78. msg = din.readUTF();
    79. if (msg != null) oben.append("\n" + msg);
    80. }
    81. catch (IOException io)
    82. {
    83. System.out.println("Empfangsfehler: " + io.getMessage());
    84. }
    85. }
    86. }
    87. }
    Alles anzeigen
  • InitServer.java

    Java-Quellcode

    1. package server;
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. /**
    5. * <p>Überschrift: </p>
    6. * <p>Beschreibung: </p>
    7. * <p>Copyright: Copyright (c) 2003</p>
    8. * <p>Organisation: </p>
    9. * @author unbekannt
    10. * @version 1.0
    11. */
    12. public class InitServer extends Frame implements ActionListener {
    13. public Label lbl_port;
    14. public TextField port;
    15. public Button start;
    16. public InitServer() {
    17. this.setLayout(new BorderLayout());
    18. this.addWindowListener(new WindowAdapter(){
    19. public void windowClosing(WindowEvent e)
    20. {
    21. dispose();
    22. System.exit(0);
    23. }});
    24. lbl_port = new Label("Portnummer:");
    25. start = new Button("Start");
    26. port = new TextField(5);
    27. this.setTitle("SERVER");
    28. this.add(lbl_port,BorderLayout.WEST);
    29. this.add(port,BorderLayout.CENTER);
    30. this.add(start,BorderLayout.EAST);
    31. this.pack();
    32. this.setResizable(false);
    33. start.addActionListener(this);
    34. this.setVisible(true);
    35. }
    36. public void actionPerformed(ActionEvent e)
    37. {
    38. int prt;
    39. try
    40. {
    41. prt = Integer.parseInt(port.getText());
    42. if (!(prt < 32768 && prt > 128))
    43. {
    44. System.out.println("Port muss einen Zahl < 32768 und > 128");
    45. System.exit(0);
    46. }
    47. ChatServer cs = new ChatServer(prt);
    48. }
    49. catch(NumberFormatException nfe)
    50. {
    51. System.out.println("Port muss einen Zahl < 32768 und > 128");
    52. }
    53. //ChatServer cs = new ChatServer(1234);
    54. }
    55. public static void main(String[] args)
    56. {
    57. InitServer is = new InitServer();
    58. }
    59. }
    Alles anzeigen


    ChatServer.java

    Java-Quellcode

    1. package server;
    2. import java.awt.*;
    3. import java.awt.event.*;
    4. import java.net.*;
    5. import java.io.*;
    6. public class ChatServer extends Frame implements ActionListener, Runnable
    7. {
    8. public TextArea oben;
    9. public TextField unten;
    10. public ServerSocket ss;
    11. public Socket s;
    12. public DataInputStream din;
    13. public DataOutputStream dout;
    14. public ChatServer(int port)
    15. {
    16. // Oberfläche
    17. this.setLayout(new BorderLayout(1,2));
    18. this.setSize(640,480);
    19. this.setResizable(false);
    20. this.setTitle("Server");
    21. this.setBackground(Color.gray);
    22. oben = new TextArea();
    23. unten = new TextField(255);
    24. this.add(oben,BorderLayout.CENTER);
    25. this.add(unten,BorderLayout.SOUTH);
    26. this.addWindowListener(new WindowAdapter(){
    27. public void windowClosing(WindowEvent e){
    28. send("quit");
    29. dispose();
    30. System.exit(0);}});
    31. unten.addActionListener(this);
    32. this.setVisible(true);
    33. // Verbindung zum Client herstellen
    34. try
    35. {
    36. ss = new ServerSocket(port);
    37. s = ss.accept();
    38. din = new DataInputStream(s.getInputStream());
    39. dout = new DataOutputStream(s.getOutputStream());
    40. }
    41. catch(UnknownHostException uhe)
    42. {
    43. System.out.println("unkown");
    44. }
    45. catch(IOException i)
    46. {
    47. System.out.println("Initialisierungsfehler: " + i.getMessage());
    48. }
    49. // auf Message warten
    50. Thread th = new Thread(this);
    51. th.start();
    52. }
    53. public void actionPerformed(ActionEvent e)
    54. {
    55. oben.append("\n" + unten.getText());
    56. System.out.println(send(unten.getText()));
    57. unten.setText("");
    58. }
    59. public int send(String msg)
    60. {
    61. try
    62. {
    63. if (msg == null) return 0;
    64. dout.writeUTF(msg);
    65. }
    66. catch(IOException io)
    67. {
    68. System.out.println("Sendefehler: " + io.getMessage());
    69. return -1;
    70. }
    71. return 1;
    72. }
    73. public void run()
    74. {
    75. String msg = new String("");
    76. while(!msg.equals("quit"))
    77. {
    78. try
    79. {
    80. msg = din.readUTF();
    81. if (msg != null) oben.append("\n" + msg);
    82. }
    83. catch (IOException io)
    84. {
    85. System.out.println("Empfangsfehler: " + io.getMessage());
    86. }
    87. }
    88. }
    Alles anzeigen