201 lines
9.5 KiB
Java
201 lines
9.5 KiB
Java
package Projekt;
|
|
|
|
import javax.swing.DefaultListModel;
|
|
import javax.swing.JButton;
|
|
import javax.swing.JFrame;
|
|
import javax.swing.JList;
|
|
import javax.swing.JScrollPane;
|
|
import javax.swing.JTextField;
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.lang3.ArrayUtils;
|
|
|
|
import java.awt.event.*;
|
|
import java.io.DataInputStream;
|
|
import java.io.DataOutputStream;
|
|
import java.io.File;
|
|
import java.net.InetAddress;
|
|
import java.net.Socket;
|
|
import java.nio.file.Files;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/*
|
|
Lepiej lapac exception!!!
|
|
CZY po wyjsciu z aplikacji zamkaja sie tez inne procesy? chyba tak
|
|
*/
|
|
|
|
public class App {
|
|
static JTextField ipText, portText, messageT, myUserT; // przy evencie musi byc static albo w klasie musi byc final
|
|
static JFrame frame = new JFrame("Messages");
|
|
static JButton mSendB, connectB;
|
|
static JList<String> messagesL;
|
|
static DefaultListModel<String> messages = new DefaultListModel<>();
|
|
static JScrollPane sPane;
|
|
|
|
static 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;
|
|
try {
|
|
InetAddress rAddr = InetAddress.getByName(ipText.getText());
|
|
int rPort = Integer.parseInt(portText.getText());
|
|
rSocket = new Socket(rAddr, rPort + 1); // laczymy
|
|
DataInputStream rDis = new DataInputStream(rSocket.getInputStream()); // tu bedziemy przyjmowac wiadomosci
|
|
addMessage("LISTEN CONNECTION OK");
|
|
while (true) { //czytamy wiadomosc
|
|
byte[] rec = new byte[1024];
|
|
if (rDis.read(rec) > 0){
|
|
String s = new String(rec);
|
|
addMessage(s);
|
|
}
|
|
}
|
|
} catch (Exception ae) {
|
|
ae.printStackTrace();
|
|
addMessage("REC CANT CONNECT");
|
|
}
|
|
return;
|
|
}
|
|
};
|
|
|
|
public static void main(String[] args) {
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
frame.setSize(320, 400);
|
|
ipText = new JTextField("0.0.0.0");
|
|
ipText.setBounds(10, 10, 200, 30);
|
|
portText = new JTextField("44444");
|
|
portText.setBounds(210, 10, 100, 30);
|
|
connectB = new JButton("Connect");
|
|
connectB.setBounds(110, 50, 100, 30);
|
|
connectB.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e){
|
|
//zapobiegac podwojnemu kliknieciu w connect!
|
|
receiveThread.start();
|
|
}
|
|
});
|
|
|
|
myUserT = new JTextField("Nick");
|
|
myUserT.setBounds(10, 50, 100, 30);
|
|
messagesL = new JList<String>(messages);
|
|
sPane = new JScrollPane(messagesL);
|
|
sPane.setBounds(10, 100, 300, 180);
|
|
messageT = new JTextField("Message");
|
|
messageT.setBounds(10, 290, 300, 30);
|
|
mSendB = new JButton("Send");
|
|
mSendB.setBounds(110, 330, 100, 30);
|
|
mSendB.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) { //wysylanie wiadomosci
|
|
try {
|
|
InetAddress tAddr = InetAddress.getByName(ipText.getText()); //ustanawiamy polaczenie na wysylanie wiadomosci
|
|
int tPort = Integer.parseInt(portText.getText());
|
|
String messString = messageT.getText();
|
|
DataOutputStream tDos, fDos;
|
|
DataInputStream fDis;
|
|
Socket tSocket, fSocket;
|
|
String toCheckString = messString + "xdddddddd"; //sprawdzany string zawsze bedzie mial min 9 znakow
|
|
|
|
if (toCheckString.substring(0, 5).equals("/FILE")){ //CHYBA OK? wysylanie pliku
|
|
String fileName = messString.substring(6, messString.length()); //uzyskujemy sciezke do pliku
|
|
System.out.println(fileName);
|
|
File sendFile = new File(fileName);
|
|
if (!sendFile.exists() || !sendFile.isFile()) { //zly plik
|
|
addMessage("ZLY PLIK!");
|
|
} else {
|
|
System.out.println(sendFile.length()); //wielkosc pliku
|
|
tSocket = new Socket(tAddr, tPort); // laczymy
|
|
tDos = new DataOutputStream(tSocket.getOutputStream()); // tu wysylamy wiadomosci
|
|
String[] parsed = messString.split("/"); //chcemy pobrac nazwe pliku
|
|
tDos.write(convert(myUserT.getText() + "> /FILE " + parsed[parsed.length-1] + " " + sendFile.length())); //wysylamy wielkosc pliku
|
|
byte[] fileContent = Files.readAllBytes(sendFile.toPath()); //plik do bitow
|
|
tSocket.close();
|
|
fSocket = new Socket(tAddr, tPort + 2);
|
|
fDos = new DataOutputStream(fSocket.getOutputStream()); //polaczenie dla pliku
|
|
int wyslano = 0;
|
|
while (wyslano < sendFile.length()){ //wysylamy po 1024
|
|
int tobyte = 1024; //standardowa ilosc bajtow
|
|
if (wyslano + tobyte > fileContent.length){
|
|
tobyte = fileContent.length - wyslano;
|
|
}
|
|
byte[] bufor = new byte[tobyte];
|
|
System.arraycopy(fileContent, wyslano, bufor, 0, tobyte);
|
|
fDos.write(bufor); //uwaga dodac sprawdzanie czy na pewno wyslalismy dobra liczbe bajtow
|
|
wyslano += tobyte;
|
|
addMessage("WYSLANO: " + wyslano);
|
|
}
|
|
messageT.setText("");
|
|
fSocket.close(); //czy to tez trzeba?
|
|
}
|
|
|
|
} else if(toCheckString.substring(0, 9).equals("/DOWNLOAD")) { //pobieranie pliku
|
|
tSocket = new Socket(tAddr, tPort); // laczymy
|
|
tDos = new DataOutputStream(tSocket.getOutputStream()); // tu wysylamy wiadomosci
|
|
tDos.write(convert(myUserT.getText() + "> /DOWNLOAD " + messString.substring(10, messString.length()))); //wysylamy prosbe
|
|
fSocket = new Socket(tAddr, tPort + 2);
|
|
fDis = new DataInputStream(fSocket.getInputStream());
|
|
byte[] rec = new byte[1024];
|
|
String s = new String();
|
|
if (fDis.read(rec) > 0){
|
|
s = new String(rec); //otrzymana wiadomosc o wielkosci pliku
|
|
System.out.println(s);
|
|
}
|
|
int received = 0;
|
|
List<byte[]> fileBList = new ArrayList<byte[]>(); //plik
|
|
while (received < Integer.parseInt(s.trim())){ //pobieramy zawartosc pliku
|
|
rec = new byte[1024]; //czyscimy tablice
|
|
if (fDis.read(rec) > 0){
|
|
fileBList.add(rec); //dodajemy do listy
|
|
}
|
|
received += 1024;
|
|
addMessage("POBRANO: " + received);
|
|
}
|
|
byte[] fileB = fileBList.get(0);
|
|
for(int i=1; i<fileBList.size(); i++){
|
|
fileB = ArrayUtils.addAll(fileB, fileBList.get(i));
|
|
}
|
|
FileUtils.writeByteArrayToFile(new File(messString.substring(10, messString.length())), fileB); //zapis pliku
|
|
addMessage("ZAPISANO PLIK");
|
|
|
|
} else {
|
|
tSocket = new Socket(tAddr, tPort); // laczymy
|
|
tDos = new DataOutputStream(tSocket.getOutputStream()); // tu wysylamy wiadomosci
|
|
tDos.write(convert(myUserT.getText() + "> " + messString)); //wysylamy
|
|
messageT.setText("");
|
|
tSocket.close(); //czy to tez trzeba?
|
|
}
|
|
} catch (Exception ae){
|
|
ae.printStackTrace();
|
|
addMessage("CANT SEND");
|
|
}
|
|
}
|
|
});
|
|
|
|
frame.add(ipText);
|
|
frame.add(portText);
|
|
frame.add(sPane);
|
|
frame.add(messageT);
|
|
frame.add(mSendB);
|
|
frame.add(myUserT);
|
|
frame.add(connectB);
|
|
frame.setLayout(null);
|
|
frame.setVisible(true);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
synchronized static void addMessage(String mess){ //funkcja do wsadzania nowych wiadomosci
|
|
messages.addElement(mess);
|
|
messagesL.setModel(messages);
|
|
//messagesL.ensureIndexIsVisible(messages.getSize() - 1);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
//byte[] combined = new byte[1024*(i+1)]; //bufor do laczenia
|
|
//System.arraycopy(fileB, 0, combined, 0, fileB.length); //kopiowanie
|
|
//System.arraycopy(fileBList.get(i), 0, combined, fileB.length, fileBList.get(i).length);
|
|
//fileB = new byte[1024*(i+1)];
|
|
//System.arraycopy(combined, 0, fileB, 0, combined.length); //kopiowanie 2 |