2019-10-31 11:17:48 +01:00
|
|
|
package Projekt;
|
|
|
|
|
2019-11-07 21:22:20 +01:00
|
|
|
import javax.swing.DefaultListModel;
|
|
|
|
import javax.swing.JButton;
|
|
|
|
import javax.swing.JComboBox;
|
|
|
|
import javax.swing.JFrame;
|
|
|
|
import javax.swing.JLabel;
|
|
|
|
import javax.swing.JList;
|
|
|
|
import javax.swing.JTextField;
|
2019-11-10 13:45:49 +01:00
|
|
|
import java.awt.event.*;
|
|
|
|
import java.io.DataInputStream;
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.Socket;
|
2019-10-31 11:17:48 +01:00
|
|
|
|
2019-11-10 13:45:49 +01:00
|
|
|
/*
|
|
|
|
Lepiej lapac exception!!!
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class App {
|
|
|
|
static Socket socket; // przy evencie musi byc static albo w klasie musi byc final
|
|
|
|
static JTextField ipText, portText, messageT, myUserT;
|
|
|
|
static JFrame frame = new JFrame("Messages");
|
|
|
|
static JButton mSendB, fSendB;
|
|
|
|
static JLabel usersL;
|
|
|
|
static JComboBox<String> usersC;
|
|
|
|
static JList<String> messagesL;
|
|
|
|
static DataOutputStream dos;
|
|
|
|
static DataInputStream dis;
|
|
|
|
static DefaultListModel<String> messages = new DefaultListModel<>();
|
2019-11-07 21:22:20 +01:00
|
|
|
|
2019-11-10 13:45:49 +01:00
|
|
|
public static void main(String[] args) {
|
2019-11-07 21:22:20 +01:00
|
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
2019-11-10 13:45:49 +01:00
|
|
|
frame.setSize(320, 400);
|
2019-11-07 21:22:20 +01:00
|
|
|
|
2019-11-10 13:45:49 +01:00
|
|
|
ipText = new JTextField("Server IP");
|
|
|
|
ipText.setBounds(10, 10, 200, 30);
|
2019-11-07 21:22:20 +01:00
|
|
|
portText = new JTextField("Port");
|
|
|
|
portText.setBounds(210, 10, 100, 30);
|
2019-11-10 13:45:49 +01:00
|
|
|
myUserT = new JTextField("Nick");
|
|
|
|
myUserT.setBounds(10, 50, 100, 30);
|
2019-11-07 21:22:20 +01:00
|
|
|
usersL = new JLabel("Choose user:");
|
2019-11-10 13:45:49 +01:00
|
|
|
usersL.setBounds(15, 90, 100, 30);
|
|
|
|
String users[] = { "Tomus", "Piotrus" };
|
2019-11-07 21:22:20 +01:00
|
|
|
usersC = new JComboBox<>(users);
|
2019-11-10 13:45:49 +01:00
|
|
|
usersC.setBounds(110, 90, 100, 30);
|
2019-11-07 21:22:20 +01:00
|
|
|
messages.addElement("<USER> Elo");
|
|
|
|
messages.addElement("<YOU> No siema");
|
|
|
|
messagesL = new JList<String>(messages);
|
|
|
|
messagesL.setBounds(10, 130, 300, 150);
|
|
|
|
messageT = new JTextField("Message");
|
|
|
|
messageT.setBounds(10, 290, 300, 30);
|
|
|
|
mSendB = new JButton("Send");
|
|
|
|
mSendB.setBounds(55, 330, 100, 30);
|
2019-11-10 13:45:49 +01:00
|
|
|
mSendB.addActionListener(new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
try {
|
|
|
|
InetAddress addr = InetAddress.getByName(ipText.getText());
|
|
|
|
int port = Integer.parseInt(portText.getText());
|
|
|
|
socket = new Socket(addr, port); // laczymy
|
|
|
|
dos = new DataOutputStream(socket.getOutputStream()); // tu wysylamy wiadomosci
|
|
|
|
dis = new DataInputStream(socket.getInputStream()); // tu bedziemy przyjmowac wiadomosci
|
|
|
|
|
|
|
|
byte[] mess = new byte[1024];
|
|
|
|
//UWAGA pewnie dluzsze wiadomosci trzeba jakos rozdzielac
|
|
|
|
String messS = myUserT.getText() + ">" + messageT.getText() + "<kubus";
|
|
|
|
mess = messS.getBytes(); // konwertujemy na bajty
|
|
|
|
dos.write(mess); //wysylamy
|
|
|
|
messageT.setText("");
|
|
|
|
|
|
|
|
//czytamy wiadomosc
|
|
|
|
byte[] rec = new byte[1024];
|
|
|
|
dis.read(rec);
|
|
|
|
String s = new String(rec);
|
|
|
|
//System.out.println("Serwer powiedzial: "+s);
|
|
|
|
messages.addElement(s);
|
|
|
|
messagesL.setModel(messages);
|
|
|
|
} catch (Exception e1) {
|
|
|
|
messageT.setText("CANT SEND");
|
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-11-07 21:22:20 +01:00
|
|
|
fSendB = new JButton("File");
|
|
|
|
fSendB.setBounds(165, 330, 100, 30);
|
|
|
|
|
|
|
|
|
|
|
|
frame.add(ipText);
|
|
|
|
frame.add(portText);
|
|
|
|
frame.add(usersL);
|
|
|
|
frame.add(usersC);
|
|
|
|
frame.add(messagesL);
|
|
|
|
frame.add(messageT);
|
|
|
|
frame.add(mSendB);
|
|
|
|
frame.add(fSendB);
|
2019-11-10 13:45:49 +01:00
|
|
|
frame.add(myUserT);
|
2019-11-07 21:22:20 +01:00
|
|
|
frame.setLayout(null);
|
|
|
|
frame.setVisible(true);
|
|
|
|
|
2019-11-10 13:45:49 +01:00
|
|
|
/*
|
2019-10-31 11:17:48 +01:00
|
|
|
|
|
|
|
// czytaj odpowiedz to jest kod golinskiego
|
|
|
|
//String s = dis.readUTF();
|
|
|
|
// wypisz odpowiedz
|
|
|
|
//System.out.println("Serwer powiedzial: "+s);
|
|
|
|
dis.close();
|
|
|
|
dos.close();
|
|
|
|
socket.close();
|
|
|
|
|
|
|
|
} catch (Exception e) { //kiepskie lapanie wyjatkow!! lepiej bardziej sprecyzowac jaki wyjatek lapiemy
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
System.out.println("Klient zakonczyl dzialanie");
|
2019-11-07 21:22:20 +01:00
|
|
|
*/
|
2019-10-31 11:17:48 +01:00
|
|
|
}
|
|
|
|
}
|