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.JFrame;
|
|
|
|
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 {
|
2019-11-11 18:37:06 +01:00
|
|
|
static Socket tSocket; // przy evencie musi byc static albo w klasie musi byc final
|
2019-11-10 13:45:49 +01:00
|
|
|
static JTextField ipText, portText, messageT, myUserT;
|
|
|
|
static JFrame frame = new JFrame("Messages");
|
2019-11-11 18:37:06 +01:00
|
|
|
static JButton mSendB, fSendB, connectB;
|
2019-11-10 13:45:49 +01:00
|
|
|
static JList<String> messagesL;
|
2019-11-11 18:37:06 +01:00
|
|
|
static DataOutputStream tDos;
|
2019-11-10 13:45:49 +01:00
|
|
|
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-11 18:37:06 +01:00
|
|
|
//CZY po wyjsciu z aplikacji zamkaja sie tez inne procesy? chyba tak
|
2019-11-07 21:22:20 +01:00
|
|
|
|
2019-11-11 18:37:06 +01:00
|
|
|
ipText = new JTextField("0.0.0.0");
|
2019-11-10 13:45:49 +01:00
|
|
|
ipText.setBounds(10, 10, 200, 30);
|
2019-11-11 18:37:06 +01:00
|
|
|
portText = new JTextField("44444");
|
2019-11-07 21:22:20 +01:00
|
|
|
portText.setBounds(210, 10, 100, 30);
|
2019-11-11 18:37:06 +01:00
|
|
|
connectB = new JButton("Connect");
|
|
|
|
connectB.setBounds(110, 50, 100, 30);
|
|
|
|
connectB.addActionListener(new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e){
|
|
|
|
//zapobiegac podwojnemu kliknieciu w connect!
|
|
|
|
Thread receiveThread = new Thread() { //watek w ktorym bedziemy tylko odbierac wiadomosci
|
|
|
|
public void run() { //uwazac zeby nie dochodzic jednoczesnie do tej samej zmiennej (synchronize?)
|
|
|
|
Socket rSocket;
|
|
|
|
DataInputStream rDis;
|
|
|
|
DataOutputStream rDos;
|
|
|
|
try {
|
|
|
|
InetAddress rAddr = InetAddress.getByName(ipText.getText());
|
|
|
|
int rPort = Integer.parseInt(portText.getText());
|
|
|
|
rSocket = new Socket(rAddr, rPort); // laczymy
|
|
|
|
rDis = new DataInputStream(rSocket.getInputStream()); // tu bedziemy przyjmowac wiadomosci
|
|
|
|
rDos = new DataOutputStream(rSocket.getOutputStream());
|
|
|
|
rDos.write(convert("R")); //wysyłamy znaczek R
|
|
|
|
ipText.setText("OK1");
|
|
|
|
while (true) {
|
|
|
|
//czytamy wiadomosc
|
|
|
|
byte[] rec = new byte[1024];
|
|
|
|
if (rDis.read(rec) > 0){
|
|
|
|
String s = new String(rec);
|
|
|
|
//System.out.println("Serwer powiedzial: "+s);
|
|
|
|
messages.addElement(s);
|
|
|
|
messagesL.setModel(messages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception ae) {
|
|
|
|
ae.printStackTrace();
|
|
|
|
ipText.setText("REC CANT CONNECT");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
receiveThread.start();
|
|
|
|
//ustanawiamy polaczenie na wysylanie wiadomosci
|
|
|
|
InetAddress tAddr = InetAddress.getByName(ipText.getText());
|
|
|
|
int tPort = Integer.parseInt(portText.getText());
|
|
|
|
tSocket = new Socket(tAddr, tPort); // laczymy
|
|
|
|
tDos = new DataOutputStream(tSocket.getOutputStream()); // tu wysylamy wiadomosci
|
|
|
|
tDos.write(convert("T")); //wysylamy T
|
|
|
|
portText.setText("OK2");
|
|
|
|
|
|
|
|
} catch (Exception ae){
|
|
|
|
ae.printStackTrace();
|
|
|
|
portText.setText("CANT CONNECT");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
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() {
|
2019-11-11 18:37:06 +01:00
|
|
|
public void actionPerformed(ActionEvent e) { //wysylanie wiadomosci
|
2019-11-10 13:45:49 +01:00
|
|
|
try {
|
2019-11-11 18:37:06 +01:00
|
|
|
tDos.write(convert(myUserT.getText() + "> " + messageT.getText())); //wysylamy
|
2019-11-10 13:45:49 +01:00
|
|
|
messageT.setText("");
|
|
|
|
|
|
|
|
} 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(messagesL);
|
|
|
|
frame.add(messageT);
|
|
|
|
frame.add(mSendB);
|
|
|
|
frame.add(fSendB);
|
2019-11-10 13:45:49 +01:00
|
|
|
frame.add(myUserT);
|
2019-11-11 18:37:06 +01:00
|
|
|
frame.add(connectB);
|
2019-11-07 21:22:20 +01:00
|
|
|
frame.setLayout(null);
|
|
|
|
frame.setVisible(true);
|
2019-11-11 18:37:06 +01:00
|
|
|
}
|
2019-11-07 21:22:20 +01:00
|
|
|
|
2019-11-11 18:37:06 +01:00
|
|
|
synchronized static byte[] convert(String wiad){
|
|
|
|
byte[] mess = new byte[1024];
|
|
|
|
//UWAGA pewnie dluzsze wiadomosci trzeba jakos rozdzielac
|
|
|
|
mess = wiad.getBytes(); // konwertujemy na bajty
|
|
|
|
return mess;
|
2019-10-31 11:17:48 +01:00
|
|
|
}
|
2019-11-11 18:37:06 +01:00
|
|
|
|
2019-10-31 11:17:48 +01:00
|
|
|
}
|